class Alchemist::Library

Attributes

binary_prefixes[R]
conversion_table[R]
si_units[R]
unit_prefixes[R]

Public Class Methods

new() click to toggle source
# File lib/alchemist/library.rb, line 6
def initialize
  @conversion_table = load_conversion_table
  @si_units = YAML.load_file(Configuration::DEFAULT_SI_UNITS_FILE)
  @unit_prefixes = YAML.load_file(Configuration::DEFAULT_UNIT_PREFIXES_FILE)
  @binary_prefixes = YAML.load_file(Configuration::DEFAULT_BINARY_PREFIXES_FILE)
  @loaded_modules = {}
end

Public Instance Methods

binary_unit?(name) click to toggle source
# File lib/alchemist/library.rb, line 22
def binary_unit?(name)
  using_binary? && measurement_for(name).include?(:information_storage)
end
categories() click to toggle source
# File lib/alchemist/library.rb, line 30
def categories
  @conversion_table.keys
end
conversion_base_for(unit_type, unit_name) click to toggle source
# File lib/alchemist/library.rb, line 57
def conversion_base_for(unit_type, unit_name)
  @conversion_table[unit_type][unit_name]
end
conversions() click to toggle source
# File lib/alchemist/library.rb, line 53
def conversions
  @conversions ||= load_conversions
end
exponent_for(name, prefix) click to toggle source
# File lib/alchemist/library.rb, line 14
def exponent_for(name, prefix)
  if binary_unit?(name)
    binary_prefixes[prefix]
  else
    unit_prefixes[prefix]
  end
end
has_measurement?(name) click to toggle source
# File lib/alchemist/library.rb, line 61
def has_measurement? name
  conversions.keys.include? name.to_sym
end
load_all_categories() click to toggle source
# File lib/alchemist/library.rb, line 39
def load_all_categories
  categories.each do |category|
    load_category category
  end
end
load_category(category) click to toggle source
# File lib/alchemist/library.rb, line 34
def load_category(category)
  @loaded_modules[category] ||= ModuleBuilder.new(category)
  Numeric.send :include, @loaded_modules[category]
end
load_conversion_table(filename=Configuration::DEFAULT_UNITS_FILE) click to toggle source
# File lib/alchemist/library.rb, line 74
def load_conversion_table(filename=Configuration::DEFAULT_UNITS_FILE)
  if new_table = ConversionTable.new.load_all(filename)
    @conversion_table = new_table
  else
    @conversion_table ||= load_conversion_table
  end
end
measurement_for(unit_name) click to toggle source
# File lib/alchemist/library.rb, line 49
def measurement_for unit_name
  conversions[ unit_name.to_sym ]
end
operator_actions() click to toggle source
# File lib/alchemist/library.rb, line 70
def operator_actions
  @operator_actions ||= {}
end
register(type, names, value) click to toggle source
# File lib/alchemist/library.rb, line 82
def register(type, names, value)
  names = Array(names)
  value = value.is_a?(Measurement) ? value.base(type) : value
  conversion_table[type] ||= {}

  names.each do |name|
    conversions[name] ||= []
    conversions[name] << type
    conversion_table[type][name] = value
  end

  @loaded_modules[type].define_unit_method(names)
end
register_operation_conversions(type, other_type, operation, converted_type) click to toggle source
# File lib/alchemist/library.rb, line 65
def register_operation_conversions type, other_type, operation, converted_type
  operator_actions[operation] ||= []
  operator_actions[operation] << [type, other_type, converted_type]
end
unit_names(category) click to toggle source
# File lib/alchemist/library.rb, line 45
def unit_names category
  @conversion_table[category.to_sym].map(&:first)
end
using_binary?() click to toggle source
# File lib/alchemist/library.rb, line 26
def using_binary?
  !Alchemist.config.use_si?
end

Private Instance Methods

load_conversions() click to toggle source
# File lib/alchemist/library.rb, line 98
def load_conversions
  conversions = {}
  conversion_table.each do |type, table_conversions|
    table_conversions.each do |name, value|
      conversions[name] ||= []
      conversions[name] << type
    end
  end
  conversions
end