Sha256: 69e07222ea76536d2c554a048c924727579125284b8bb57663588f4b8341afa8

Contents?: true

Size: 451 Bytes

Versions: 4

Compression:

Stored size: 451 Bytes

Contents

# Utility methods to create hashes from collections. 
# origin: RM
module Enumerable

  def collect_hash(&pairer)
    collect_into_hash({}, &pairer)
  end

  def collect_ordered_hash(&pairer)
    collect_into_hash(ActiveSupport::OrderedHash.new, &pairer)
  end

  private

  def collect_into_hash(hash, &pairer)
    inject(hash) do |hash, key|
      pair = pairer.call(key)
      hash[pair[0]] = pair[1] unless pair.nil?
      hash
    end
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
serum-rails-0.2.1 spec/test_apps/rails-2-3/config/initializers/collect_hash.rb
serum-rails-0.2.0 spec/test_apps/rails-2-3/config/initializers/collect_hash.rb
serum-rails-0.1.1 spec/test_app/config/initializers/collect_hash.rb
serum-rails-0.1.0 spec/test_app/config/initializers/collect_hash.rb