Sha256: bdfb73ea8f580d55136677a60380297ecfe830eb42d3e0cf752cdce582586a92

Contents?: true

Size: 537 Bytes

Versions: 7

Compression:

Stored size: 537 Bytes

Contents

module Radiosonde::Utils
  def collect_to_hash(collection, *key_attrs)
    opts = key_attrs.last.kind_of?(Hash) ? key_attrs.pop : {}
    hash = {}

    collection.each do |item|
      if block_given?
        key = yield(item)
      else
        key = key_attrs.map {|k| item.send(k) }
        key = key.first if key_attrs.length == 1
      end

      if opts[:has_many]
        hash[key] ||= []
        hash[key] << item
      else
        hash[key] = item
      end
    end

    return hash
  end
  module_function :collect_to_hash
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
radiosonde-0.0.7 lib/radiosonde/utils.rb
radiosonde-0.0.6 lib/radiosonde/utils.rb
radiosonde-0.0.5 lib/radiosonde/utils.rb
radiosonde-0.0.4 lib/radiosonde/utils.rb
radiosonde-0.0.3 lib/radiosonde/utils.rb
radiosonde-0.0.2 lib/radiosonde/utils.rb
radiosonde-0.0.1 lib/radiosonde/utils.rb