Class: Moolah::Charge
- Inherits:
-
Object
- Object
- Moolah::Charge
- Defined in:
- lib/moolah/charge.rb
Class Method Summary collapse
Instance Method Summary collapse
- #+(other_charge) ⇒ Object
- #==(other_charge) ⇒ Object
-
#initialize(valid_attributes: [], **kwargs) ⇒ Charge
constructor
A new instance of Charge.
- #total ⇒ Object
Constructor Details
#initialize(valid_attributes: [], **kwargs) ⇒ Charge
Returns a new instance of Charge.
3 4 5 6 7 8 9 10 11 |
# File 'lib/moolah/charge.rb', line 3 def initialize(valid_attributes: [], **kwargs) extend Anemic::Model validate_attributes!(kwargs, valid_attributes) kwargs.each do |component_name, component_value| attribute component_name, Money end self.attributes = kwargs end |
Class Method Details
.sum(charges) ⇒ Object
34 35 36 37 38 |
# File 'lib/moolah/charge.rb', line 34 def self.sum(charges) charges_to_sum = charges.dup charge = charges_to_sum.shift charges_to_sum.inject(charge, &:+) end |
Instance Method Details
#+(other_charge) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/moolah/charge.rb', line 13 def +(other_charge) return self unless other_charge all_component_keys = Set.new(attributes.keys + other_charge.attributes.keys) combined_components = all_component_keys.inject({}) do |com, key| com.tap do |c| values = [attributes[key], other_charge.attributes[key]].compact # will have at least one value first_value, second_value = *values # second value could be nil but adding a nil value to a money will work c[key] = first_value + second_value end end Charge.new(**combined_components) end |
#==(other_charge) ⇒ Object
40 41 42 |
# File 'lib/moolah/charge.rb', line 40 def ==(other_charge) attributes == other_charge.attributes end |