Class: ActiveMocker::CollectionAssociation
- Inherits:
-
Object
- Object
- ActiveMocker::CollectionAssociation
- Includes:
- Enumerable
- Defined in:
- lib/active_mocker/collection_association.rb
Instance Method Summary collapse
- #<<(*records) ⇒ Object
- #==(other_ary) ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(collection = []) ⇒ CollectionAssociation
constructor
A new instance of CollectionAssociation.
- #sum(attribute = nil) ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize(collection = []) ⇒ CollectionAssociation
Returns a new instance of CollectionAssociation.
7 8 9 |
# File 'lib/active_mocker/collection_association.rb', line 7 def initialize(collection=[]) @association = [*collection] end |
Instance Method Details
#<<(*records) ⇒ Object
11 12 13 |
# File 'lib/active_mocker/collection_association.rb', line 11 def <<(*records) association.concat(records.flatten) end |
#==(other_ary) ⇒ Object
23 24 25 |
# File 'lib/active_mocker/collection_association.rb', line 23 def ==(other_ary) association == other_ary end |
#each(&block) ⇒ Object
27 28 29 30 31 |
# File 'lib/active_mocker/collection_association.rb', line 27 def each(&block) association.each do |item| block.call(item) end end |
#sum(attribute = nil) ⇒ Object
18 19 20 21 |
# File 'lib/active_mocker/collection_association.rb', line 18 def sum(attribute=nil) values = association.map { |obj| obj.send(attribute) } values.inject { |sum, n| sum + n } end |
#to_a ⇒ Object
33 34 35 |
# File 'lib/active_mocker/collection_association.rb', line 33 def to_a @association end |