Module: ActsAsDecimal::ClassMethods

Defined in:
lib/acts_as_decimal/acts_as_decimal.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_decimal(attr_name, options = {:decimals => 2}) ⇒ Object

Implements integer handling as floating numbers. Usage:

Inside a model with a price or amount field, simply put

acts_as_decimal field_name, :decimals => 2


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/acts_as_decimal/acts_as_decimal.rb', line 17

def acts_as_decimal(attr_name, options = {:decimals => 2})
  fields = [attr_name] unless attr_name.is_a? Array
  fields.each do |field|
    class_eval <<-EOC
      def #{field}
        # DOES NOT WORK PROPERLY WITH VIM (WTF¿?): (self[:#{field}].nil? ? nil : (BigDecimal.new(self[:#{field}].to_s) / BigDecimal('10').power(#{options[:decimals]})).to_f)
        (self[:#{field}].nil? ? nil : (self[:#{field}] / BigDecimal('10').power(#{options[:decimals]}).to_f))
      end
      def human_#{field}(options = {:thousand_delimiters => true})
        
        return nil if #{field}.blank?

        a = #{field}.to_s.split('.')
        b = a[1].ljust(2,'0')

        if options[:thousand_delimiters] == false
          return a[0] + "." + b
        else
          groups = a[0].reverse.scan(/\\d{3}/) 
          rest = a[0].gsub(groups.join.reverse, '').reverse
          groups << rest unless rest.empty?
          if groups.last == '-'
            groups.reject!{|x| x == '-'}
            negative = true
          end
          humanized_string = negative ? "-" : ""
          humanized_string += groups.join('.').reverse + "," + b
          return humanized_string
        end

      end
      def #{field}=(decimalnum)
        self[:#{field}] = (decimalnum.nil? ? nil : (BigDecimal.new(decimalnum.to_s) * BigDecimal('10').power(#{options[:decimals]})).to_i )
      end

      def #{field}_raw
        self[:#{field}]
      end
      def #{field}_raw=(intnum)
        self[:#{field}] = intnum
      end
    EOC
  end
end