Module: Amountable::ClassMethods
- Defined in:
- lib/amountable.rb
Instance Method Summary collapse
- #allowed_amount_name?(name) ⇒ Boolean
- #amount(name, options = {}) ⇒ Object
- #amount_set(set_name, component) ⇒ Object
Instance Method Details
#allowed_amount_name?(name) ⇒ Boolean
117 118 119 |
# File 'lib/amountable.rb', line 117 def allowed_amount_name?(name) self.amount_names.include?(name.to_sym) end |
#amount(name, options = {}) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/amountable.rb', line 97 def amount(name, = {}) (self.amount_names ||= Set.new) << name define_method name do (find_amount(name) || NilAmount.new).value end define_method "#{name}=" do |value| return Money.zero if value.zero? amount = find_amount(name) || amounts.build(name: name) amount.value = value.to_money all_amounts << amount if amount.new_record? (@amounts_by_name ||= {})[name.to_sym] = amount end Array([:summable] || [:summables] || [:set] || [:sets] || [:amount_set] || [:amount_sets]).each do |set| amount_set(set, name) end end |
#amount_set(set_name, component) ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/amountable.rb', line 89 def amount_set(set_name, component) self.amount_sets[set_name.to_sym] << component.to_sym define_method set_name do find_amounts(self.amount_sets[set_name.to_sym]).sum(Money.zero, &:value) end end |