Module: Meteorlog::Utils
- Included in:
- Client
- Defined in:
- lib/meteorlog/utils.rb
Class Method Summary collapse
Class Method Details
.collect_to_hash(collection, *key_attrs) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/meteorlog/utils.rb', line 2 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 |