Class: CommerceUnits::Converter
- Inherits:
-
Object
- Object
- CommerceUnits::Converter
- Defined in:
- lib/commerce_units/converter.rb
Defined Under Namespace
Classes: MismatchDimension, WrongType
Instance Attribute Summary collapse
-
#origin_value ⇒ Object
Returns the value of attribute origin_value.
-
#target_unit ⇒ Object
Returns the value of attribute target_unit.
Class Method Summary collapse
Instance Method Summary collapse
Instance Attribute Details
#origin_value ⇒ Object
Returns the value of attribute origin_value.
12 13 14 |
# File 'lib/commerce_units/converter.rb', line 12 def origin_value @origin_value end |
#target_unit ⇒ Object
Returns the value of attribute target_unit.
12 13 14 |
# File 'lib/commerce_units/converter.rb', line 12 def target_unit @target_unit end |
Class Method Details
.convert(value: value, unit: unit) ⇒ Object
3 4 5 6 7 8 |
# File 'lib/commerce_units/converter.rb', line 3 def convert(value: value, unit: unit) new.tap do |c| c.target_unit = unit c.origin_value = value end.coerce end |
Instance Method Details
#coerce ⇒ Object
14 15 16 17 18 |
# File 'lib/commerce_units/converter.rb', line 14 def coerce return origin_value if target_unit == origin_unit raise MismatchDimension, "Unable to convert #{origin_unit} to #{target_unit}" if _mismatch_dimension? _base_to_target_transform _origin_to_base_transform origin_value end |
#origin_unit ⇒ Object
20 21 22 |
# File 'lib/commerce_units/converter.rb', line 20 def origin_unit origin_value.unit end |