Module: Amountable::ClassMethods
- Defined in:
- lib/amountable.rb
Instance Method Summary collapse
-
#act_as_amountable(options = {}) ⇒ Object
Possible storage values: [:table, :jsonb].
- #allowed_amount_name?(name) ⇒ Boolean
- #amount(name, options = {}) ⇒ Object
- #amount_set(set_name, component) ⇒ Object
Instance Method Details
#act_as_amountable(options = {}) ⇒ Object
Possible storage values: [:table, :jsonb]
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/amountable.rb', line 58 def act_as_amountable( = {}) class_attribute :amount_names class_attribute :amount_sets class_attribute :amounts_column_name self.amount_sets = Hash.new { |h, k| h[k] = Set.new } self.amount_names = Set.new self.amounts_column_name = 'amounts' case ([:storage] || :table).to_sym when :table has_many :amounts, class_name: 'Amountable::Amount', as: :amountable, dependent: :destroy, autosave: false include Amountable::TableMethods when :jsonb self.amounts_column_name = [:column].to_s if [:column] raise MissingColumn.new("You need an amounts jsonb field on the #{self.table_name} table.") unless column_names.include?(self.amounts_column_name) include Amountable::JsonbMethods else raise ArgumentError.new("Please specify a storage: #{ALLOWED_STORAGE}") end validate :validate_amount_names include Amountable::InstanceMethods end |
#allowed_amount_name?(name) ⇒ Boolean
104 105 106 |
# File 'lib/amountable.rb', line 104 def allowed_amount_name?(name) self.amount_names.include?(name.to_sym) end |
#amount(name, options = {}) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/amountable.rb', line 88 def amount(name, = {}) (self.amount_names ||= Set.new) << name define_method name do (find_amount(name) || Amountable::NilAmount.new).value end define_method "#{name}=" do |value| set_amount(name, value) 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
80 81 82 83 84 85 86 |
# File 'lib/amountable.rb', line 80 def amount_set(set_name, component) self.amount_sets[set_name.to_sym] << component.to_sym define_method set_name do get_set(set_name) end end |