Class: Metamorphosis::UnitValue
- Inherits:
-
Object
- Object
- Metamorphosis::UnitValue
show all
- Includes:
- Comparable
- Defined in:
- lib/metamorphosis/unit_value.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(unit_symbol, value, exponent = 1.0) ⇒ UnitValue
Returns a new instance of UnitValue.
7
8
9
10
11
|
# File 'lib/metamorphosis/unit_value.rb', line 7
def initialize(unit_symbol, value, exponent = 1.0)
@unit_symbol = unit_symbol.to_sym
@value = value
@exponent = exponent.to_f
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
48
49
50
51
|
# File 'lib/metamorphosis/unit_value.rb', line 48
def method_missing method_name, *args, &block
self.to(method_name) ||
super( method_name, *args, &block )
end
|
Instance Attribute Details
#exponent ⇒ Object
Returns the value of attribute exponent.
5
6
7
|
# File 'lib/metamorphosis/unit_value.rb', line 5
def exponent
@exponent
end
|
#unit_symbol ⇒ Object
Returns the value of attribute unit_symbol.
5
6
7
|
# File 'lib/metamorphosis/unit_value.rb', line 5
def unit_symbol
@unit_symbol
end
|
#value ⇒ Object
Returns the value of attribute value.
5
6
7
|
# File 'lib/metamorphosis/unit_value.rb', line 5
def value
@value
end
|
Instance Method Details
#<=>(other_unit) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/metamorphosis/unit_value.rb', line 25
def <=>(other_unit)
unless other_unit.is_a?(UnitValue) || other_unit.is_a?(Numeric)
raise ArgumentError,
"Unable to compare #{self.to_s} with #{other_unit.class}"
end
return self.to_f <=> other_unit if other_unit.is_a?(Numeric)
unless @unit_symbol == other_unit.unit_symbol
other_unit = other_unit.to(@unit_symbol)
end
self.to_f <=> other_unit.to_f
end
|
#to(unit_symbol = nil) ⇒ Object
Also known as:
as
42
43
44
45
|
# File 'lib/metamorphosis/unit_value.rb', line 42
def to(unit_symbol = nil)
return self unless unit_symbol
Metamorphosis.engine.convert_to(unit_symbol, self)
end
|
#to_f ⇒ Object
17
18
19
|
# File 'lib/metamorphosis/unit_value.rb', line 17
def to_f
@exponent * @value
end
|
#to_i ⇒ Object
21
22
23
|
# File 'lib/metamorphosis/unit_value.rb', line 21
def to_i
to_f.to_i
end
|
#to_s ⇒ Object
13
14
15
|
# File 'lib/metamorphosis/unit_value.rb', line 13
def to_s
"#{self.to_f} #{unit_symbol}"
end
|