A simple tool for those who love clean DSL when working with arrays
Ruby
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
spec
.gitignore
README.textile
TODO

README.textile

Scopes-n-Groups

A simple tool for those who love clean DSL when working with arrays.

Scope class

Usually it behaves like a normal array:

scope = Scope.new [1, 2]
scope << 3
scope += [4]
scope # => [1, 2, 3, 4]

Scoping

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]

Mapping

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 # => ["$", ")", "%", "\\"]

Grouping (coming soon…)

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