Sha256: 1766ee0077e7aa3f304a950b6ed62097d7aa13d08d9ecb2a582e9224af32c4f7

Contents?: true

Size: 509 Bytes

Versions: 6

Compression:

Stored size: 509 Bytes

Contents

# encoding: utf-8
require 'attr_enumerable/attr_enumerable_helper'

# AttrEnumerable
module AttrEnumerable
  private
    def reduce_attr(attribute, method_name, *args, &block)
      col = collection
      init_value = args.size == 0 ? 0 : args.first
      return init_value if col.empty?
      fail ArgumentError, "invalid attribute #{attribute}" unless include_attr?(col.first, attribute)
      attrs = col.map { |e|e.send(attribute) }
      attrs.reduce(init_value) { |a, e|a = yield(a, e); a }
    end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
tbpgr_utils-0.0.151 lib/attr_enumerable/reduce_attr.rb
tbpgr_utils-0.0.150 lib/attr_enumerable/reduce_attr.rb
tbpgr_utils-0.0.149 lib/attr_enumerable/reduce_attr.rb
tbpgr_utils-0.0.148 lib/attr_enumerable/reduce_attr.rb
tbpgr_utils-0.0.147 lib/attr_enumerable/reduce_attr.rb
tbpgr_utils-0.0.146 lib/attr_enumerable/reduce_attr.rb