Class: Reckon::Money
Instance Attribute Summary collapse
-
#amount ⇒ Object
Returns the value of attribute amount.
-
#currency ⇒ Object
Returns the value of attribute currency.
-
#suffixed ⇒ Object
Returns the value of attribute suffixed.
Class Method Summary collapse
Instance Method Summary collapse
- #-@ ⇒ Object
- #<=>(mon) ⇒ Object
-
#initialize(amount, options = {}) ⇒ Money
constructor
A new instance of Money.
- #pretty(negate = false) ⇒ Object
- #to_f ⇒ Object
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, = {} ) if [:inverse] @amount = -1*amount.to_f else @amount = amount.to_f end @currency = [:currency] || "$" @suffixed = [:suffixed] end |
Instance Attribute Details
#amount ⇒ Object
Returns the value of attribute amount.
7 8 9 |
# File 'lib/reckon/money.rb', line 7 def amount @amount end |
#currency ⇒ Object
Returns the value of attribute currency.
7 8 9 |
# File 'lib/reckon/money.rb', line 7 def currency @currency end |
#suffixed ⇒ Object
Returns the value of attribute suffixed.
7 8 9 |
# File 'lib/reckon/money.rb', line 7 def suffixed @suffixed end |
Class Method Details
.from_s(value, options = {}) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/reckon/money.rb', line 45 def Money::from_s( value, = {} ) # Empty string is treated as money with value 0 return Money.new( 0.00, ) if value.empty? # Remove 1000 separaters and replace , with . if comma_separates_cents # 1.000,00 -> 1000.00 value = value.gsub(/\./, '').gsub(/,/, '.') if [:comma_separates_cents] value = value.gsub(/,/, '') money_format_regex = /^(.*?)(\d+\.\d\d)/ # Money has two decimal precision any_number_regex = /^(.*?)([\d\.]+)/ # Prefer matching the money_format, match any number otherwise m = value.match( money_format_regex ) || value.match( any_number_regex ) if m amount = m[2].to_f # Check whether the money had a - or (, which indicates negative amounts if (m[1].match( /^[\(-]/ ) || m[1].match( /-$/ )) amount *= -1 end return Money.new( amount, ) else return nil end end |
.likelihood(entry) ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/reckon/money.rb', line 72 def Money::likelihood( entry ) money_score = 0 money_score += 20 if entry[/^[\-\+\(]{0,2}\$/] 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 > 8 money_score -= 20 if entry !~ /^[\$\+\.\-,\d\(\)]+$/ money_score end |
Instance Method Details
#-@ ⇒ Object
22 23 24 |
# File 'lib/reckon/money.rb', line 22 def -@ Money.new( -@amount, :currency => @currency, :suffixed => @suffixed ) end |
#<=>(mon) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/reckon/money.rb', line 26 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
37 38 39 40 41 42 43 |
# File 'lib/reckon/money.rb', line 37 def pretty( negate = false ) if @suffixed (@amount >= 0 ? " " : "") + sprintf("%0.2f #{@currency}", @amount * (negate ? -1 : 1)) else (@amount >= 0 ? " " : "") + sprintf("%0.2f", @amount * (negate ? -1 : 1)).gsub(/^((\-)|)(?=\d)/, "\\1#{@currency}") end end |
#to_f ⇒ Object
18 19 20 |
# File 'lib/reckon/money.rb', line 18 def to_f return @amount end |