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
# File 'lib/reckon/money.rb', line 8

def initialize(amount, options = {})
  @amount_raw = amount
  @raw = options[:raw]

  @amount = parse(amount, options[:comma_separates_cents])
  @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



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/reckon/money.rb', line 62

def self.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



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

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

#<=>(mon) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/reckon/money.rb', line 34

def <=>(mon)
  other_amount = mon.to_f
  if @amount < other_amount
    -1
  elsif @amount > other_amount
    1
  else
    0
  end
end

#pretty(negate = false) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/reckon/money.rb', line 45

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

#to_fObject



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

def to_f
  return @amount
end

#to_sObject



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

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