module Amountable::InstanceMethods

Public Instance Methods

all_amounts() click to toggle source
# File lib/amountable.rb, line 24
def all_amounts
  @all_amounts ||= amounts.to_set
end
find_amount(name) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
Calls superclass method
# 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() click to toggle source
# 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