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
24 25 26 |
# File 'lib/amountable.rb', line 24 def all_amounts @all_amounts ||= amounts.to_set end |
#find_amount(name) ⇒ Object
28 29 30 |
# File 'lib/amountable.rb', line 28 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
32 33 34 |
# File 'lib/amountable.rb', line 32 def find_amounts(names) amounts.to_set.select { |am| names.include?(am.name.to_sym) } end |
#serializable_hash(opts = nil) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/amountable.rb', line 42 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
36 37 38 39 40 |
# File 'lib/amountable.rb', line 36 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 |