module EncapsulateAsMoney

Constants

VERSION

Public Instance Methods

encapsulate_as_money(*attributes) click to toggle source
# File lib/encapsulate_as_money.rb, line 7
def encapsulate_as_money(*attributes)
  options = extract_options(attributes)
  attributes.each do |attribute|
    encapsulate_attribute_as_money(attribute, options[:preserve_nil])
  end
end

Private Instance Methods

encapsulate_attribute_as_money(attribute, preserve_nil = true) click to toggle source
Calls superclass method
# File lib/encapsulate_as_money.rb, line 16
def encapsulate_attribute_as_money(attribute, preserve_nil = true)
  if preserve_nil
    define_method attribute do
      Money.new(super()) if super()
    end

    define_method "#{attribute}=" do |money|
      super(money && money.fractional)
    end
  else
    define_method attribute do
      Money.new(super() || 0)
    end

    define_method "#{attribute}=" do |money|
      num = (money && money.fractional) || 0
      super(num)
    end
  end
end
extract_options(args) click to toggle source
# File lib/encapsulate_as_money.rb, line 37
def extract_options(args)
  args.last.is_a?(Hash) ? args.pop : {}
end