class Smartdown::Model::Answer::Money

Constants

FORMAT_REGEX

Public Instance Methods

humanize() click to toggle source
# File lib/smartdown/model/answer/money.rb, line 19
def humanize
  whole, decimal = separate_by_comma(value)
  if decimal == '00'
    "£#{whole}"
  else
    "£#{whole}.#{decimal}"
  end
end
to_s() click to toggle source
# File lib/smartdown/model/answer/money.rb, line 15
def to_s
  ('%.2f' % value).chomp('.00')
end
value_type() click to toggle source
# File lib/smartdown/model/answer/money.rb, line 11
def value_type
  ::Float
end

Private Instance Methods

parse_value(value) click to toggle source
# File lib/smartdown/model/answer/money.rb, line 30
def parse_value value
  if value.is_a?(Float)
    value
  elsif value.is_a?(Fixnum)
    Float value
  else
    matched_value = value.strip.match FORMAT_REGEX
    if matched_value
      Float matched_value[1].gsub(',','').chomp('.')
    else
      @error = 'Invalid format'
      return
    end
  end
end
separate_by_comma(number) click to toggle source
# File lib/smartdown/model/answer/money.rb, line 46
def separate_by_comma(number)
  left, right = ('%.2f' % number).split('.')
  left.gsub!(/(\d)(?=(\d\d\d)+(?!\d))/) do |digit_to_delimit|
    "#{digit_to_delimit},"
  end
  [left, right]
end