Class: Alchemist::Library
- Inherits:
-
Object
- Object
- Alchemist::Library
- Includes:
- Singleton
- 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
- #categories ⇒ Object
- #conversion_base_for(unit_type, unit_name) ⇒ Object
- #conversions ⇒ Object
- #has_measurement?(name) ⇒ Boolean
-
#initialize ⇒ Library
constructor
A new instance of Library.
- #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
Constructor Details
#initialize ⇒ Library
Returns a new instance of Library.
13 14 15 16 17 18 |
# File 'lib/alchemist/library.rb', line 13 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) end |
Instance Attribute Details
#binary_prefixes ⇒ Object (readonly)
Returns the value of attribute binary_prefixes.
11 12 13 |
# File 'lib/alchemist/library.rb', line 11 def binary_prefixes @binary_prefixes end |
#conversion_table ⇒ Object (readonly)
Returns the value of attribute conversion_table.
11 12 13 |
# File 'lib/alchemist/library.rb', line 11 def conversion_table @conversion_table end |
#si_units ⇒ Object (readonly)
Returns the value of attribute si_units.
11 12 13 |
# File 'lib/alchemist/library.rb', line 11 def si_units @si_units end |
#unit_prefixes ⇒ Object (readonly)
Returns the value of attribute unit_prefixes.
11 12 13 |
# File 'lib/alchemist/library.rb', line 11 def unit_prefixes @unit_prefixes end |
Instance Method Details
#categories ⇒ Object
20 21 22 |
# File 'lib/alchemist/library.rb', line 20 def categories @conversion_table.keys end |
#conversion_base_for(unit_type, unit_name) ⇒ Object
36 37 38 |
# File 'lib/alchemist/library.rb', line 36 def conversion_base_for(unit_type, unit_name) @conversion_table[unit_type][unit_name] end |
#conversions ⇒ Object
32 33 34 |
# File 'lib/alchemist/library.rb', line 32 def conversions @conversions ||= load_conversions end |
#has_measurement?(name) ⇒ Boolean
40 41 42 |
# File 'lib/alchemist/library.rb', line 40 def has_measurement? name conversions.keys.include? name.to_sym end |
#load_conversion_table(filename = Configuration::DEFAULT_UNITS_FILE) ⇒ Object
53 54 55 56 57 58 59 |
# File 'lib/alchemist/library.rb', line 53 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
28 29 30 |
# File 'lib/alchemist/library.rb', line 28 def measurement_for unit_name conversions[ unit_name.to_sym ] end |
#operator_actions ⇒ Object
49 50 51 |
# File 'lib/alchemist/library.rb', line 49 def operator_actions @operator_actions ||= {} end |
#register(type, names, value) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/alchemist/library.rb', line 61 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 end |
#register_operation_conversions(type, other_type, operation, converted_type) ⇒ Object
44 45 46 47 |
# File 'lib/alchemist/library.rb', line 44 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
24 25 26 |
# File 'lib/alchemist/library.rb', line 24 def unit_names category @conversion_table[category.to_sym].map { |values| values[0] } end |