A simple tool for those who love clean DSL when working with arrays.
Usually it behaves like a normal array:
scope = Scope.new [1, 2] scope << 3 scope += [4] scope # => [1, 2, 3, 4]
Allows you to define named scopes for array just like for Rails ActiveRecord model.
class Numbers < Scope define_scope :even, proc { |number| number % 2 == 0 } define_scope :positive, proc { |number| number > 0 } end numbers = Numbers.new((-5..5).to_a) numbers.even # => [-4, -2, 0, 2, 4] numbers.positive # => [1, 2, 3, 4, 5] numbers.even.positive # => [2, 4]
Allows to define named mappings for array.
class Tokens < Scope define_mapping :unescaped, proc { |token| (token[0] == ?\\) ? token[1..-1] : token } end tokens = Tokens.new ['\\$', '\\)', '%', '\\\\', '\\1', '\\a'] tokens.unescaped # => ["$", ")", "%", "\\", "1", "a"]
Mappings can be even combined with scopes:
class Tokens define_scope :special, proc { |token| token =~ /\W/ } end tokens.unescaped.special # => ["$", ")", "%", "\\"]
Allows to group array elements by any property.
class Fixnum def modulo_three self % 3 end end numbers = Scope.new((1..10).to_a) numbers.group_by(:modulo_three) # => { 0 => [3, 6, 9], 1 => [1, 4, 7, 10], 2 => [2, 5, 9] }
this feature is in development now