module GxG::Units

Public Class Methods

interpret_units(params={}) click to toggle source
# File lib/gxg/gxg_units.rb, line 40
def self.interpret_units(params={})
  # {:text => quanta,:categories => categories,:locale => locale}
  quanta_string = (params[:text] || "")
  categories = (params[:categories] || :any)
  locale = (params[:locale] || :en_US)
  numeric_base = (params[:base] || 10)
  unless categories == :any
    unless categories.is_a?(Array)
      categories = [(categories)]
    end
  end
  results = []
  @@interpreters.to_enum.each do |interpreter|
    entry = nil
    if categories == :any
      entry = interpreter::interpret(quanta_string.to_s,locale,numeric_base)
    else
      categories.to_enum.each do |category|
        if interpreter::categories.include?(category)
          entry = interpreter.interpret(quanta_string.to_s,locale,numeric_base)
        end
      end
    end
    if entry
      results << entry
    end
  end
  {:result => results}
end
refresh_units_registry() click to toggle source
# File lib/gxg/gxg_units.rb, line 35
def self.refresh_units_registry()
  # SOMEDAY: Thread-safety?
  @@interpreters = self.unit_interpreters()
end
unit_interpreters(categories=:any) click to toggle source

this module is used for interpreting text into denominations and multipliers., and general support for various units of measure

# File lib/gxg/gxg_units.rb, line 10
def self.unit_interpreters(categories=:any)
  unless categories == :any
    unless categories.is_a?(Array)
      categories = [(categories)]
    end
  end
  unit_list = []
  GxG::Units.constants.to_enum.each do |unit_handler|
    unit_handler = GxG::Units.const_get(unit_handler)
    if (unit_handler.is_any?([Class, Module]) && unit_handler.respond_to?(:interpret) && unit_handler.respond_to?(:categories))
      if categories == :any
        unit_list << unit_handler
      else
        categories.to_enum.each do |category|
          if unit_handler::categories.include?(category)
            unit_list << unit_handler
          end
        end
      end
    end
  end
  #
  unit_list
end