Class: CurrencyInWords::Texterizer

Inherits:
Object
  • Object
show all
Defined in:
lib/currency-in-words.rb

Overview

:nodoc: all This is the context class for texterizers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(texterizer, splitted_number, options = {}) ⇒ Texterizer

Returns a new instance of Texterizer.



108
109
110
111
112
# File 'lib/currency-in-words.rb', line 108

def initialize texterizer, splitted_number, options = {}
  @texterizer   = texterizer
  @number_parts = splitted_number
  @options      = options
end

Instance Attribute Details

#number_partsObject (readonly)

Returns the value of attribute number_parts.



106
107
108
# File 'lib/currency-in-words.rb', line 106

def number_parts
  @number_parts
end

#optionsObject (readonly)

Returns the value of attribute options.



106
107
108
# File 'lib/currency-in-words.rb', line 106

def options
  @options
end

#texterizerObject (readonly)

Returns the value of attribute texterizer.



106
107
108
# File 'lib/currency-in-words.rb', line 106

def texterizer
  @texterizer
end

Instance Method Details

#texterizeObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/currency-in-words.rb', line 114

def texterize
  if @texterizer.respond_to?('texterize')
    texterized_number = @texterizer.texterize self
    if texterized_number.is_a?(String)
      return texterized_number
    else
      raise TypeError, "a texterizer must return a String" if @options[:raise]
    end
  else
    raise NoMethodError, "a texterizer must provide a 'texterize' method" if @options[:raise]
  end
  # Fallback on EnTexterizer
  unless @texterizer.instance_of?(EnTexterizer)
    @texterizer = EnTexterizer.new
    self.texterize
  else
    raise RuntimeError, "you should use the option ':raise => true' to see what goes wrong"
  end
end