Class: Reckon::Money

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/reckon/money.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(amount, options = {}) ⇒ Money

Returns a new instance of Money.



8
9
10
11
12
13
14
15
16
17
# 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

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



7
8
9
# File 'lib/reckon/money.rb', line 7

def amount
  @amount
end

#currencyObject

Returns the value of attribute currency.



7
8
9
# File 'lib/reckon/money.rb', line 7

def currency
  @currency
end

#suffixedObject

Returns the value of attribute suffixed.



7
8
9
# File 'lib/reckon/money.rb', line 7

def suffixed
  @suffixed
end

Class Method Details

.likelihood(entry) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# 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

Instance Method Details

#-@Object

unary minus ex m = Money.new -m



31
32
33
# File 'lib/reckon/money.rb', line 31

def -@
  Money.new(-@amount, :currency => @currency, :suffixed => @suffixed)
end

#<=>(mon) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# 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 = {}) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
# 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) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# 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) ⇒ Object



63
64
65
# File 'lib/reckon/money.rb', line 63

def pretty_amount(amount)
  sprintf("%0.2f", amount).reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
end

#to_fObject



19
20
21
# File 'lib/reckon/money.rb', line 19

def to_f
  return @amount
end

#to_sObject



23
24
25
# File 'lib/reckon/money.rb', line 23

def to_s
  return @options[:raw] ? "#{@amount_raw} | #{@amount}" : @amount
end