Class: Metamorphosis::Engine
- Inherits:
-
Object
- Object
- Metamorphosis::Engine
- Includes:
- Singleton
- Defined in:
- lib/metamorphosis/engine.rb
Instance Attribute Summary collapse
-
#registry ⇒ Object
readonly
Returns the value of attribute registry.
Instance Method Summary collapse
- #common_unit_category(source, dest) ⇒ Object
- #convert_to(dest_unit_name, source) ⇒ Object
- #from_base_value(category, dest_unit_symbol, value, exponent) ⇒ Object
-
#initialize ⇒ Engine
constructor
A new instance of Engine.
- #numeric_to_unit_value(numeric, unit_name) ⇒ Object
- #register_unit(category, symbols, value) ⇒ Object
- #reset! ⇒ Object
- #to_base_value(category, source_unit) ⇒ Object
Constructor Details
#initialize ⇒ Engine
Returns a new instance of Engine.
15 16 17 |
# File 'lib/metamorphosis/engine.rb', line 15 def initialize reset! end |
Instance Attribute Details
#registry ⇒ Object (readonly)
Returns the value of attribute registry.
7 8 9 |
# File 'lib/metamorphosis/engine.rb', line 7 def registry @registry end |
Instance Method Details
#common_unit_category(source, dest) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/metamorphosis/engine.rb', line 24 def common_unit_category(source, dest) source_categories = @registry.unit_categories(source) dest_categories = @registry.unit_categories(dest) category = (source_categories & dest_categories).first end |
#convert_to(dest_unit_name, source) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/metamorphosis/engine.rb', line 31 def convert_to(dest_unit_name, source) return source if dest_unit_name.to_sym == source.unit_symbol exponent, dest_unit_symbol = @parser.parse(dest_unit_name) return unless @registry.include?(dest_unit_symbol) category = common_unit_category(source.unit_symbol, dest_unit_symbol) unless category raise IncompatibleUnitsError, "Cannot convert from #{source.unit_symbol} to #{dest_unit_symbol}" end value = to_base_value(category, source) from_base_value(category, dest_unit_symbol, value, exponent) end |
#from_base_value(category, dest_unit_symbol, value, exponent) ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/metamorphosis/engine.rb', line 48 def from_base_value(category, dest_unit_symbol, value, exponent) conversion_factor = @registry.conversion_factor(category, dest_unit_symbol) if conversion_factor.is_a?(Array) new_unit_value = conversion_factor[1].call(value) else new_unit_value = value / (exponent * conversion_factor) end UnitValue.new(dest_unit_symbol, new_unit_value) end |
#numeric_to_unit_value(numeric, unit_name) ⇒ Object
9 10 11 12 13 |
# File 'lib/metamorphosis/engine.rb', line 9 def numeric_to_unit_value(numeric, unit_name) exponent, unit_symbol = @parser.parse(unit_name) return nil unless @registry.include?(unit_symbol) UnitValue.new(unit_symbol, numeric, exponent) end |
#register_unit(category, symbols, value) ⇒ Object
67 68 69 70 71 72 73 |
# File 'lib/metamorphosis/engine.rb', line 67 def register_unit(category, symbols, value) value = value.is_a?(UnitValue) ? to_base_value(category, value) : value symbols = Array(symbols) symbols.each do |unit_symbol| @registry.register(category, unit_symbol, value) end end |
#reset! ⇒ Object
19 20 21 22 |
# File 'lib/metamorphosis/engine.rb', line 19 def reset! @registry = UnitRegistry.new @parser = PrefixParser.new(DEFAULT_PREFIXES, @registry) end |
#to_base_value(category, source_unit) ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/metamorphosis/engine.rb', line 58 def to_base_value(category, source_unit) conversion_factor = @registry.conversion_factor(category, source_unit.unit_symbol) if(conversion_factor.is_a?(Array)) conversion_factor[0].call(source_unit.value) * source_unit.exponent else source_unit.to_f * conversion_factor end end |