class Money
Constants
- FORMATS
- REMOVE_RE
- ROUND_MODES
- VALID_RE
- YAML_TYPE_NAME
- YAML_TYPE_ROOT
Public Class Methods
[](value)
click to toggle source
# File lib/bd_money/bd_money.rb, line 295 def [](value) new value end
clean(value)
click to toggle source
# File lib/bd_money/bd_money.rb, line 283 def clean(value) value.to_s.gsub REMOVE_RE, '' end
convert(value, precision = nil, round_mode = nil)
click to toggle source
# File lib/bd_money/bd_money.rb, line 279 def convert(value, precision = nil, round_mode = nil) new value, precision, round_mode end
format()
click to toggle source
# File lib/bd_money/bd_money.rb, line 275 def format @format || :default end
format=(value)
click to toggle source
# File lib/bd_money/bd_money.rb, line 270 def format=(value) raise "Unknown format options [#{value}]" unless FORMATS.key?(value) @format = value end
new(value, precision = nil, round_mode = nil, format = nil)
click to toggle source
# File lib/bd_money/bd_money.rb, line 34 def initialize(value, precision = nil, round_mode = nil, format = nil) self.amount = value self.precision = precision if precision self.round_mode = round_mode if round_mode self.format = format if format end
precision()
click to toggle source
# File lib/bd_money/bd_money.rb, line 257 def precision @precision || 2 end
precision=(value)
click to toggle source
# File lib/bd_money/bd_money.rb, line 252 def precision=(value) raise "Unknown precision [#{value}]" unless value.is_a?(Integer) @precision = value end
round_mode()
click to toggle source
# File lib/bd_money/bd_money.rb, line 266 def round_mode @round_mode || :half_up end
round_mode=(value)
click to toggle source
# File lib/bd_money/bd_money.rb, line 261 def round_mode=(value) raise "Unknown rounding mode [#{value}]" unless ROUND_MODES.key?(value) @round_mode = value end
valid?(value)
click to toggle source
# File lib/bd_money/bd_money.rb, line 287 def valid?(value) !!value.to_s.match(VALID_RE) end
zero()
click to toggle source
# File lib/bd_money/bd_money.rb, line 291 def zero new 0 end
Public Instance Methods
%(other)
click to toggle source
# File lib/bd_money/bd_money.rb, line 116 def %(other) convert amount % convert(other).amount end
*(other)
click to toggle source
# File lib/bd_money/bd_money.rb, line 104 def *(other) convert amount * convert(other).amount end
**(other)
click to toggle source
# File lib/bd_money/bd_money.rb, line 112 def **(other) convert amount ** convert(other).amount.to_i end
+(other)
click to toggle source
# File lib/bd_money/bd_money.rb, line 96 def +(other) convert amount + convert(other).amount end
-(other)
click to toggle source
# File lib/bd_money/bd_money.rb, line 100 def -(other) convert amount - convert(other).amount end
/(other)
click to toggle source
# File lib/bd_money/bd_money.rb, line 108 def /(other) convert amount / convert(other).amount end
<=>(other)
click to toggle source
# File lib/bd_money/bd_money.rb, line 92 def <=>(other) amount <=> convert(other).amount end
^(other)
click to toggle source
# File lib/bd_money/bd_money.rb, line 120 def ^(other) convert amount ^ convert(other).amount end
amount()
click to toggle source
# File lib/bd_money/bd_money.rb, line 53 def amount @amount end
amount=(value)
click to toggle source
# File lib/bd_money/bd_money.rb, line 41 def amount=(value) if value.respond_to?(:to_big_decimal) @amount = value.to_big_decimal elsif value.is_a?(BigDecimal) @amount = value else str = self.class.clean value raise MoneyError, "Invalid value [#{str}] (#{value.class.name})" unless self.class.valid?(str) @amount = BigDecimal.new str.gsub REMOVE_RE, '' end end
as_json(options = nil)
click to toggle source
For better json decoding
# File lib/bd_money/rails.rb, line 16 def as_json(options = nil) to_s end
convert(value, this_precision = precision, this_round_mode = round_mode)
click to toggle source
# File lib/bd_money/bd_money.rb, line 84 def convert(value, this_precision = precision, this_round_mode = round_mode) self.class.convert value, this_precision, this_round_mode end
credit?()
click to toggle source
# File lib/bd_money/bd_money.rb, line 133 def credit? amount >= 0 end
debit?()
click to toggle source
# File lib/bd_money/bd_money.rb, line 146 def debit? amount < 0 end
eql?(other)
click to toggle source
# File lib/bd_money/bd_money.rb, line 88 def eql?(other) amount == convert(other).amount end
format()
click to toggle source
# File lib/bd_money/bd_money.rb, line 80 def format @format || self.class.format end
format=(value)
click to toggle source
# File lib/bd_money/bd_money.rb, line 75 def format=(value) raise "Unknown format options [#{value}]" unless FORMATS.key?(value) @format = value end
formatted(*args)
click to toggle source
# File lib/bd_money/bd_money.rb, line 197 def formatted(*args) defaults = args.first.is_a?(::Symbol) ? FORMATS[args.shift] : FORMATS[:default] options = args.last.is_a?(::Hash) ? args.pop : { } unit = options[:unit] || defaults[:unit] spacer = options[:spacer] || defaults[:spacer] spacer = '' if unit.to_s.empty? delimiter = options[:delimiter] || defaults[:delimiter] separator = options[:separator] || defaults[:separator] separator = '' if precision == 0 precision = options[:precision] || defaults[:precision] last = options[:last] || defaults[:last] number = to_s precision return number if number == 'NaN' begin parts = number.to_s.split('.') parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1#{delimiter}") number = parts.join(separator) "#{unit}#{spacer}#{number}#{last}" rescue number end end
method_missing(meth, *args, &blk)
click to toggle source
Calls superclass method
# File lib/bd_money/bd_money.rb, line 226 def method_missing(meth, *args, &blk) if amount.respond_to? meth result = amount.send meth, *args, &blk result.is_a?(::BigDecimal) ? convert(result) : result else super end end
precision()
click to toggle source
# File lib/bd_money/bd_money.rb, line 62 def precision @precision || self.class.precision end
precision=(value)
click to toggle source
# File lib/bd_money/bd_money.rb, line 57 def precision=(value) raise "Unknown precision [#{value}]" unless value.is_a?(Integer) @precision = value end
quoted_id()
click to toggle source
Terrible hack to allow to quote money correctly
# File lib/bd_money/rails.rb, line 6 def quoted_id amount end
respond_to?(meth)
click to toggle source
Calls superclass method
# File lib/bd_money/bd_money.rb, line 222 def respond_to?(meth) amount.respond_to?(meth) || super end
round(this_precision = precision, this_round_mode = round_mode)
click to toggle source
# File lib/bd_money/bd_money.rb, line 167 def round(this_precision = precision, this_round_mode = round_mode) convert round_amount(this_precision, this_round_mode) end
round_amount(this_precision = precision, this_round_mode = round_mode)
click to toggle source
# File lib/bd_money/bd_money.rb, line 162 def round_amount(this_precision = precision, this_round_mode = round_mode) this_round_mode = BigDecimal.const_get("ROUND_#{this_round_mode.to_s.upcase}") if this_round_mode.is_a?(Symbol) amount.round this_precision, this_round_mode end
round_mode()
click to toggle source
# File lib/bd_money/bd_money.rb, line 71 def round_mode @round_mode || self.class.round_mode end
round_mode=(value)
click to toggle source
# File lib/bd_money/bd_money.rb, line 66 def round_mode=(value) raise "Unknown rounding mode [#{value}]" unless ROUND_MODES.key?(value) @round_mode = value end
to_big_decimal()
click to toggle source
# File lib/bd_money/bd_money.rb, line 158 def to_big_decimal amount end
to_credit()
click to toggle source
# File lib/bd_money/bd_money.rb, line 124 def to_credit convert amount.abs end
to_credit!()
click to toggle source
# File lib/bd_money/bd_money.rb, line 128 def to_credit! self.amount = amount.abs self end
to_d()
click to toggle source
This will help to save money objects correctly
# File lib/bd_money/rails.rb, line 11 def to_d amount end
to_debit()
click to toggle source
# File lib/bd_money/bd_money.rb, line 137 def to_debit convert amount.abs * -1 end
to_debit!()
click to toggle source
# File lib/bd_money/bd_money.rb, line 141 def to_debit! self.amount = amount.abs * -1 self end
to_f(this_precision = precision, this_round_mode = round_mode)
click to toggle source
# File lib/bd_money/bd_money.rb, line 175 def to_f(this_precision = precision, this_round_mode = round_mode) round_amount(this_precision, this_round_mode).to_f end
to_i(this_round_mode = round_mode)
click to toggle source
# File lib/bd_money/bd_money.rb, line 171 def to_i(this_round_mode = round_mode) round_amount(0, this_round_mode).to_i end
to_json(options = nil)
click to toggle source
# File lib/bd_money/bd_money.rb, line 193 def to_json(options = nil) to_s end
to_money()
click to toggle source
# File lib/bd_money/bd_money.rb, line 154 def to_money self end
to_s(this_precision = precision, this_round_mode = round_mode)
click to toggle source
# File lib/bd_money/bd_money.rb, line 179 def to_s(this_precision = precision, this_round_mode = round_mode) amount_str = round_amount(this_precision, this_round_mode).to_s('F') return amount_str if amount_str == "NaN" dollars, cents = amount_str.split('.') return dollars if this_precision == 0 || cents.nil? if cents.size >= this_precision "#{dollars}.#{cents[0, this_precision]}" else "#{dollars}.#{cents}#{'0' * (this_precision - cents.size)}" end end
Also aliased as: inspect
to_yaml(options = { })
click to toggle source
# File lib/bd_money/bd_money.rb, line 239 def to_yaml(options = { }) YAML.quick_emit(self.object_id, options) do |out| out.map(taguri, to_yaml_style) do |map| map.add 'amount', amount.to_s('F') map.add 'precision', @precision unless @precision.nil? map.add 'round_mode', @round_mode unless @round_mode.nil? map.add 'format', @format unless @format.nil? end end end
to_yaml_type()
click to toggle source
# File lib/bd_money/bd_money.rb, line 235 def to_yaml_type "!#{YAML_TYPE_ROOT}/#{YAML_TYPE_NAME}" end
zero?()
click to toggle source
# File lib/bd_money/bd_money.rb, line 150 def zero? amount == 0 end