class Ingreedy::Rationalizer

Public Class Methods

new(options) click to toggle source
# File lib/ingreedy/rationalizer.rb, line 7
def initialize(options)
  @integer  = options.fetch(:integer, nil)
  @float    = options.fetch(:float, nil)
  @fraction = options.fetch(:fraction, nil)
  @word     = options.fetch(:word, nil)
end
rationalize(options) click to toggle source
# File lib/ingreedy/rationalizer.rb, line 3
def self.rationalize(options)
  new(options).rationalize
end

Public Instance Methods

rationalize() click to toggle source
# File lib/ingreedy/rationalizer.rb, line 14
def rationalize
  if Ingreedy.preserve_amounts
    (normalized_word || compound_fraction || @float || @integer)
  else
    (normalized_word || rationalized_fraction || rationalized_float || @integer).to_r
  end
end

Private Instance Methods

compound_fraction() click to toggle source
# File lib/ingreedy/rationalizer.rb, line 44
def compound_fraction
  return unless @fraction
  "#{@integer} #{normalized_fraction}".strip
end
normalized_fraction() click to toggle source
# File lib/ingreedy/rationalizer.rb, line 29
def normalized_fraction
  @fraction.tap do |fraction|
    Ingreedy.dictionaries.current.vulgar_fractions.each do |char, amount|
      fraction.gsub!(char, amount.to_s)
    end
  end
end
normalized_word() click to toggle source
# File lib/ingreedy/rationalizer.rb, line 24
def normalized_word
  return unless @word
  Ingreedy.dictionaries.current.numbers[@word.downcase]
end
rationalized_float() click to toggle source
# File lib/ingreedy/rationalizer.rb, line 49
def rationalized_float
  return unless @float
  @float.tr(",", ".")
end
rationalized_fraction() click to toggle source
# File lib/ingreedy/rationalizer.rb, line 37
def rationalized_fraction
  return unless @fraction
  result = normalized_fraction
  result = result.to_r + @integer.to_i
  result
end