lib/eco/language/models/collection.rb in eco-helpers-2.0.24 vs lib/eco/language/models/collection.rb in eco-helpers-2.0.25
- old
+ new
@@ -132,11 +132,14 @@
def group_by(attr = nil, &block)
return to_h(attr) if attr
to_a.group_by(&block) if block
end
- def to_h(attr)
- return {} if !attr
+ # By a specific `attr` or a block
+ # @note either one or the other should be present
+ def to_h(attr, &block)
+ return to_a.group_by(&block) if block
+ raise "And attr or a block are required. Given attr: #{attr}" unless attr
to_a.group_by { |object| object.method(attr).call }
end
# @!endgroup
# @!group `attr` presence methods