Class: Alchemist::Library
- Inherits:
-
Object
- Object
- Alchemist::Library
- Defined in:
- lib/alchemist/library.rb
Instance Attribute Summary collapse
-
#binary_prefixes ⇒ Object
readonly
Returns the value of attribute binary_prefixes.
-
#conversion_table ⇒ Object
readonly
Returns the value of attribute conversion_table.
-
#si_units ⇒ Object
readonly
Returns the value of attribute si_units.
-
#unit_prefixes ⇒ Object
readonly
Returns the value of attribute unit_prefixes.
Instance Method Summary collapse
- #binary_unit?(name) ⇒ Boolean
- #categories ⇒ Object
- #conversion_base_for(unit_type, unit_name) ⇒ Object
- #conversions ⇒ Object
- #exponent_for(name, prefix) ⇒ Object
- #has_measurement?(name) ⇒ Boolean
-
#initialize ⇒ Library
constructor
A new instance of Library.
- #load_all_categories ⇒ Object
- #load_category(category) ⇒ Object
- #load_conversion_table(filename = Configuration::DEFAULT_UNITS_FILE) ⇒ Object
- #measurement_for(unit_name) ⇒ Object
- #operator_actions ⇒ Object
- #register(type, names, value) ⇒ Object
- #register_operation_conversions(type, other_type, operation, converted_type) ⇒ Object
- #unit_names(category) ⇒ Object
- #using_binary? ⇒ Boolean
Constructor Details
#initialize ⇒ Library
Returns a new instance of Library.
6 7 8 9 10 11 12 |
# 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 |
Instance Attribute Details
#binary_prefixes ⇒ Object (readonly)
Returns the value of attribute binary_prefixes.
4 5 6 |
# File 'lib/alchemist/library.rb', line 4 def binary_prefixes @binary_prefixes end |
#conversion_table ⇒ Object (readonly)
Returns the value of attribute conversion_table.
4 5 6 |
# File 'lib/alchemist/library.rb', line 4 def conversion_table @conversion_table end |
#si_units ⇒ Object (readonly)
Returns the value of attribute si_units.
4 5 6 |
# File 'lib/alchemist/library.rb', line 4 def si_units @si_units end |
#unit_prefixes ⇒ Object (readonly)
Returns the value of attribute unit_prefixes.
4 5 6 |
# File 'lib/alchemist/library.rb', line 4 def unit_prefixes @unit_prefixes end |
Instance Method Details
#binary_unit?(name) ⇒ Boolean
22 23 24 |
# File 'lib/alchemist/library.rb', line 22 def binary_unit?(name) using_binary? && measurement_for(name).include?(:information_storage) end |
#categories ⇒ Object
30 31 32 |
# File 'lib/alchemist/library.rb', line 30 def categories @conversion_table.keys end |
#conversion_base_for(unit_type, unit_name) ⇒ Object
57 58 59 |
# File 'lib/alchemist/library.rb', line 57 def conversion_base_for(unit_type, unit_name) @conversion_table[unit_type][unit_name] end |
#conversions ⇒ Object
53 54 55 |
# File 'lib/alchemist/library.rb', line 53 def conversions @conversions ||= load_conversions end |
#exponent_for(name, prefix) ⇒ Object
14 15 16 17 18 19 20 |
# 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) ⇒ Boolean
61 62 63 |
# File 'lib/alchemist/library.rb', line 61 def has_measurement? name conversions.keys.include? name.to_sym end |
#load_all_categories ⇒ Object
39 40 41 42 43 |
# File 'lib/alchemist/library.rb', line 39 def load_all_categories categories.each do |category| load_category category end end |
#load_category(category) ⇒ Object
34 35 36 37 |
# 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) ⇒ Object
74 75 76 77 78 79 80 |
# 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) ⇒ Object
49 50 51 |
# File 'lib/alchemist/library.rb', line 49 def measurement_for unit_name conversions[ unit_name.to_sym ] end |
#operator_actions ⇒ Object
70 71 72 |
# File 'lib/alchemist/library.rb', line 70 def operator_actions @operator_actions ||= {} end |
#register(type, names, value) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# 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) ⇒ Object
65 66 67 68 |
# 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) ⇒ Object
45 46 47 |
# File 'lib/alchemist/library.rb', line 45 def unit_names category @conversion_table[category.to_sym].map(&:first) end |