Sha256: 8f2411ffc0adcfe9732f6e9f58d9a3f936831f0f259aef7ccf462999a9264cb3

Contents?: true

Size: 1.37 KB

Versions: 6

Compression:

Stored size: 1.37 KB

Contents

module DataMapper
  # The Support module adds functionality to Make Things Easier(tm):
  # * grouping by attributes of objects in an array (returns a hash, see #DataMapper::Support::Enumerable)
  # * adds symbols for operators like <= (lte), like, in, select, etc (see #DataMapper::Support::Symbol)
  # * adds methods for strings, allowing us to ensure strings are wrapped with content (see #DataMapper::Support::String)
  # * pulls in ActiveSupport's Inflector module
  # * loads #DataMapper::Database and #DataMapper::Base
  module Support
    
    # Extends Array to include an instance method for grouping objects
    module Enumerable
      
      # Group a collection of elements into groups within a
      # Hash. The value returned by the block passed to group_by
      # is the key, and the value is an Array of items matching
      # that key.
      #
      # === Example
      #   names = %w{ sam scott amy robert betsy }
      #   names.group_by { |name| name.size }
      #   => { 3 => [ "sam", "amy" ], 5 => [ "scott", "betsy" ], 6 => [ "robert" ]}  
      def group_by
        inject(Hash.new { |h,k| h[k] = [] }) do |memo,item|
          memo[yield(item)] << item; memo
        end
      end
  
    end # module Enumerable
  end # module Support
end # module DataMapper

# Extend Array with DataMapper::Support::Enumerable
class Array #:nodoc:
  include DataMapper::Support::Enumerable
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
datamapper-0.2.0 lib/data_mapper/support/enumerable.rb
datamapper-0.2.1 lib/data_mapper/support/enumerable.rb
datamapper-0.2.2 lib/data_mapper/support/enumerable.rb
datamapper-0.2.3 lib/data_mapper/support/enumerable.rb
datamapper-0.2.4 lib/data_mapper/support/enumerable.rb
datamapper-0.2.5 lib/data_mapper/support/enumerable.rb