class Reckon::Money

Attributes

amount[RW]
currency[RW]
suffixed[RW]

Public Class Methods

likelihood(entry) click to toggle source
# File lib/reckon/money.rb, line 80
def Money::likelihood(entry)
  money_score = 0
  # digits separated by , or . with no more than 2 trailing digits
  money_score += 40 if entry.match(/\d+[,.]\d{2}[^\d]*$/)
  money_score += 10 if entry[/^\$?\-?\$?\d+[\.,\d]*?[\.,]\d\d$/]
  money_score += 10 if entry[/\d+[\.,\d]*?[\.,]\d\d$/]
  money_score += entry.gsub(/[^\d\.\-\+,\(\)]/, '').length if entry.length < 7
  money_score -= entry.length if entry.length > 12
  money_score -= 20 if (entry !~ /^[\$\+\.\-,\d\(\)]+$/) && entry.length > 0
  money_score
end
new(amount, options = {}) click to toggle source
# File lib/reckon/money.rb, line 8
def initialize(amount, options = {})
  @options = options
  @amount_raw = amount
  @raw = options[:raw]

  @amount = parse(amount, options)
  @amount = -@amount if options[:inverse]
  @currency = options[:currency] || "$"
  @suffixed = options[:suffixed]
end

Public Instance Methods

-@() click to toggle source

unary minus ex m = Money.new -m

# File lib/reckon/money.rb, line 31
def -@
  Money.new(-@amount, :currency => @currency, :suffixed => @suffixed)
end
<=>(mon) click to toggle source
# File lib/reckon/money.rb, line 35
def <=>(mon)
  other_amount = mon.to_f
  if @amount < other_amount
    -1
  elsif @amount > other_amount
    1
  else
    0
  end
end
parse(value, options = {}) click to toggle source
# File lib/reckon/money.rb, line 67
def parse(value, options = {})
  value = value.to_s
  # Empty string is treated as money with value 0
  return value.to_f if value.to_s.empty?

  invert = value.match(/^\(.*\)$/)
  value = value.gsub(/[^0-9,.-]/, '')
  value = value.tr('.', '').tr(',', '.') if options[:comma_separates_cents]
  value = value.tr(',', '')
  value = value.to_f
  return invert ? -value : value
end
pretty(negate = false) click to toggle source
# File lib/reckon/money.rb, line 46
def pretty(negate = false)
  if @raw
    return @amount_raw unless negate

    return @amount_raw[0] == '-' ? @amount_raw[1..-1] : "-#{@amount_raw}"
  end

  amt = pretty_amount(@amount * (negate ? -1 : 1))
  amt = if @suffixed
          "#{amt} #{@currency}"
        else
          amt.gsub(/^((-)|)(?=\d)/, "\\1#{@currency}")
        end

  return (@amount >= 0 ? " " : "") + amt
end
pretty_amount(amount) click to toggle source
# File lib/reckon/money.rb, line 63
def pretty_amount(amount)
  sprintf("%0.2f", amount).reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
end
to_f() click to toggle source
# File lib/reckon/money.rb, line 19
def to_f
  return @amount
end
to_s() click to toggle source
# File lib/reckon/money.rb, line 23
def to_s
  return @options[:raw] ? "#{@amount_raw} | #{@amount}" : @amount
end