Module: Amountable::InstanceMethods
- Defined in:
- lib/amountable.rb
Instance Method Summary collapse
- #all_amounts ⇒ Object
- #find_amount(name) ⇒ Object
- #find_amounts(names) ⇒ Object
- #serializable_hash(opts = nil) ⇒ Object
- #validate_amount_names ⇒ Object
Instance Method Details
#all_amounts ⇒ Object
23 24 25 |
# File 'lib/amountable.rb', line 23 def all_amounts @all_amounts ||= amounts.to_set end |
#find_amount(name) ⇒ Object
27 28 29 |
# File 'lib/amountable.rb', line 27 def find_amount(name) (@amounts_by_name ||= {})[name.to_sym] ||= amounts.to_set.find { |am| am.name == name.to_s } end |
#find_amounts(names) ⇒ Object
31 32 33 |
# File 'lib/amountable.rb', line 31 def find_amounts(names) amounts.to_set.select { |am| names.include?(am.name.to_sym) } end |
#serializable_hash(opts = nil) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/amountable.rb', line 41 def serializable_hash(opts = nil) opts ||= {} super(opts).tap do |base| unless opts[:except].to_a.include?(:amounts) amounts_json = (self.class.amount_names + self.class.amount_sets.keys).inject({}) do |mem, name| mem.merge!(name.to_s => send(name).to_f) unless opts[:except].to_a.include?(name.to_sym) mem end base.merge!(amounts_json) end end end |
#validate_amount_names ⇒ Object
35 36 37 38 39 |
# File 'lib/amountable.rb', line 35 def validate_amount_names amounts.each do |amount| errors.add(:amounts, "#{amount.name} is not an allowed amount name.") unless self.class.allowed_amount_name?(amount.name) end end |