Class: Alchemist::Measurement
- Inherits:
-
Object
- Object
- Alchemist::Measurement
- Includes:
- Comparable
- Defined in:
- lib/alchemist/geospatial.rb,
lib/alchemist/measurement.rb
Instance Attribute Summary collapse
-
#exponent ⇒ Object
readonly
Returns the value of attribute exponent.
-
#unit_name ⇒ Object
readonly
Returns the value of attribute unit_name.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #*(multiplicand) ⇒ Object
- #+(measurement) ⇒ Object
- #-(measurement) ⇒ Object
- #/(measurement) ⇒ Object
- #<=>(other) ⇒ Object
- #base(unit_type) ⇒ Object
- #ceil(*args) ⇒ Object
- #coerce(number) ⇒ Object
- #floor(*args) ⇒ Object
- #geospatial ⇒ Object
-
#initialize(value, unit_name, exponent = 1.0) ⇒ Measurement
constructor
A new instance of Measurement.
- #per ⇒ Object
- #round(*args) ⇒ Object
- #shared_types(other_unit_name) ⇒ Object
- #to(type = nil) ⇒ Object
- #to_f ⇒ Object
- #to_i ⇒ Object
- #to_s ⇒ Object
- #types ⇒ Object
Constructor Details
#initialize(value, unit_name, exponent = 1.0) ⇒ Measurement
Returns a new instance of Measurement.
9 10 11 12 13 |
# File 'lib/alchemist/measurement.rb', line 9 def initialize value, unit_name, exponent = 1.0 @value = value.to_f @unit_name = unit_name @exponent = exponent end |
Instance Attribute Details
#exponent ⇒ Object (readonly)
Returns the value of attribute exponent.
7 8 9 |
# File 'lib/alchemist/measurement.rb', line 7 def exponent @exponent end |
#unit_name ⇒ Object (readonly)
Returns the value of attribute unit_name.
7 8 9 |
# File 'lib/alchemist/measurement.rb', line 7 def unit_name @unit_name end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
7 8 9 |
# File 'lib/alchemist/measurement.rb', line 7 def value @value end |
Instance Method Details
#*(multiplicand) ⇒ Object
49 50 51 52 53 54 55 |
# File 'lib/alchemist/measurement.rb', line 49 def * multiplicand if multiplicand.is_a?(Numeric) remeasure(value * multiplicand) else try_raising_dimension(multiplicand) end end |
#+(measurement) ⇒ Object
27 28 29 30 31 |
# File 'lib/alchemist/measurement.rb', line 27 def + measurement ensure_shared_type!(measurement) converted = measurement.to(unit_name) remeasure(value + converted.value) end |
#-(measurement) ⇒ Object
33 34 35 36 37 |
# File 'lib/alchemist/measurement.rb', line 33 def - measurement ensure_shared_type!(measurement) converted = measurement.to(unit_name) remeasure(value - converted.value) end |
#/(measurement) ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/alchemist/measurement.rb', line 39 def / measurement converted = remeasure(value / dividend(measurement)) if measurement.is_a?(Measurement) converted.value else converted end end |
#<=>(other) ⇒ Object
74 75 76 |
# File 'lib/alchemist/measurement.rb', line 74 def <=>(other) self.to_f <=> other.to(unit_name).to_f end |
#base(unit_type) ⇒ Object
57 58 59 60 |
# File 'lib/alchemist/measurement.rb', line 57 def base unit_type conversion_base = conversion_base_for(unit_type) convert_to_base(conversion_base) end |
#ceil(*args) ⇒ Object
94 95 96 |
# File 'lib/alchemist/measurement.rb', line 94 def ceil(*args) remeasure(value.ceil(*args)) end |
#coerce(number) ⇒ Object
86 87 88 |
# File 'lib/alchemist/measurement.rb', line 86 def coerce(number) [self, number] end |
#floor(*args) ⇒ Object
98 99 100 |
# File 'lib/alchemist/measurement.rb', line 98 def floor(*args) remeasure(value.floor(*args)) end |
#geospatial ⇒ Object
5 6 7 |
# File 'lib/alchemist/geospatial.rb', line 5 def geospatial Alchemist::Earth.new(self).geospatial end |
#per ⇒ Object
15 16 17 |
# File 'lib/alchemist/measurement.rb', line 15 def per CompoundMeasurement.new self end |
#round(*args) ⇒ Object
90 91 92 |
# File 'lib/alchemist/measurement.rb', line 90 def round(*args) remeasure(value.round(*args)) end |
#shared_types(other_unit_name) ⇒ Object
82 83 84 |
# File 'lib/alchemist/measurement.rb', line 82 def shared_types other_unit_name types & library.measurement_for(other_unit_name) end |
#to(type = nil) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/alchemist/measurement.rb', line 19 def to type = nil if type convertor.send(type, exponent) else convertor end end |
#to_f ⇒ Object
70 71 72 |
# File 'lib/alchemist/measurement.rb', line 70 def to_f @value * exponent end |
#to_i ⇒ Object
66 67 68 |
# File 'lib/alchemist/measurement.rb', line 66 def to_i to_f.to_i end |
#to_s ⇒ Object
62 63 64 |
# File 'lib/alchemist/measurement.rb', line 62 def to_s to_f.to_s end |
#types ⇒ Object
78 79 80 |
# File 'lib/alchemist/measurement.rb', line 78 def types library.measurement_for(unit_name) end |