module Subledger::Domain::Value

Constants

ERROR_TEXT

Attributes

amount[R]

Public Class Methods

included(base) click to toggle source
# File lib/subledger/domain/value.rb, line 9
def self.included base
  base.extend ValueClass
end
new(arg_amount=0) click to toggle source
# File lib/subledger/domain/value.rb, line 25
def initialize arg_amount=0
  if arg_amount.nil?
    raise ValueError, 'amount may not be nil'
  end

  validate_amount(arg_amount) if arg_amount.respond_to?(:match)

  big_amount = BigDecimal arg_amount

  validate_sign big_amount

  validate_amount( amount_to_s big_amount )

  @amount = big_amount
end

Public Instance Methods

+(other) click to toggle source
# File lib/subledger/domain/value.rb, line 41
def + other
  (Balance.new + value + other.value).value
end
==(other) click to toggle source
# File lib/subledger/domain/value.rb, line 45
def == other
  self.value.class == other.value.class and amount == other.amount
end
inspect()
Alias for: to_s
to_s() click to toggle source
# File lib/subledger/domain/value.rb, line 53
def to_s
  "#{self.class.type}: #{amount_to_s @amount}"
end
Also aliased as: inspect
value() click to toggle source
# File lib/subledger/domain/value.rb, line 49
def value
  self
end

Private Instance Methods

amount_to_s(arg_amount) click to toggle source
# File lib/subledger/domain/value.rb, line 61
def amount_to_s arg_amount
  if arg_amount.zero?
    '0'
  else
    arg_amount.to_s( 'F' ).chomp( '.0' ).sub( /^0+/, '' )
  end
end
validate_amount(arg_amount) click to toggle source
# File lib/subledger/domain/value.rb, line 77
def validate_amount arg_amount
  unless arg_amount.match /^\d*[.]?\d*$/
    raise ValueError, "amount (#{arg_amount}) must be numeric"
  end

  return if BigDecimal(arg_amount).zero?

  dupped_arg_amount = arg_amount.dup

  dupped_arg_amount.sub! /^0+/, ''
  dupped_arg_amount.sub! /[.](\d)*0+$/, '.\\1'

  if dupped_arg_amount.include? '.'
    whole, fraction = dupped_arg_amount.split '.'

    whole    ||= ''
    fraction ||= ''

    if whole.length > 15 or fraction.length > 12
      raise ValueError, ERROR_TEXT
    end
  else
    if dupped_arg_amount.length > 15
      raise ValueError, ERROR_TEXT
    end
  end

end
validate_sign(arg_amount) click to toggle source
# File lib/subledger/domain/value.rb, line 69
def validate_sign arg_amount
  unless arg_amount > 0
    raise ValueError, 'amount must be positive'
  end
end