Class: Moneyc::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/moneyc/converter.rb

Defined Under Namespace

Classes: ConverterError

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Converter

Returns a new instance of Converter.


4
5
6
7
8
9
# File 'lib/moneyc/converter.rb', line 4

def initialize(options = {})
  @money_in_reference_currency  = options[:money]
  @reference_currency           = options.fetch(:from)
  @target_currency              = options[:to].present? ? options[:to] : @reference_currency
  @conversion_date              = options[:at].present? ? options[:at] : Time.now
end

Instance Method Details

#convertObject


15
16
17
18
19
20
21
# File 'lib/moneyc/converter.rb', line 15

def convert
  if @money_in_reference_currency.present?
    @money_in_reference_currency.to_f * rate
  else
    raise ConverterError.new("You need enter an value to convert")
  end
end

#rateObject


11
12
13
# File 'lib/moneyc/converter.rb', line 11

def rate
  reference_and_target_is_same? ? 1 : retrieve_taget_currency_rate
end