Class: Alchemist::Measurement

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/alchemist/geospatial.rb,
lib/alchemist/measurement.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, unit_name, exponent = 1.0, options = {}) ⇒ Measurement

Returns a new instance of Measurement.



10
11
12
13
14
15
# File 'lib/alchemist/measurement.rb', line 10

def initialize value, unit_name, exponent = 1.0, options = {}
  @value = value.to_f
  @unprefixed_unit_name = unit_name.to_sym
  @exponent = exponent
  @prefix = options[:prefix] || ""
end

Instance Attribute Details

#exponentObject (readonly)

Returns the value of attribute exponent.



8
9
10
# File 'lib/alchemist/measurement.rb', line 8

def exponent
  @exponent
end

#prefixObject (readonly)

Returns the value of attribute prefix.



8
9
10
# File 'lib/alchemist/measurement.rb', line 8

def prefix
  @prefix
end

#unprefixed_unit_nameObject (readonly)

Returns the value of attribute unprefixed_unit_name.



8
9
10
# File 'lib/alchemist/measurement.rb', line 8

def unprefixed_unit_name
  @unprefixed_unit_name
end

#valueObject (readonly)

Returns the value of attribute value.



8
9
10
# File 'lib/alchemist/measurement.rb', line 8

def value
  @value
end

Instance Method Details

#*(multiplicand) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/alchemist/measurement.rb', line 57

def * multiplicand
  if multiplicand.is_a?(Numeric)
    remeasure(value * multiplicand)
  else
    try_raising_dimension(multiplicand)
  end
end

#+(measurement) ⇒ Object



33
34
35
36
37
38
# File 'lib/alchemist/measurement.rb', line 33

def + measurement
  ensure_shared_type!(measurement)
  converted = measurement.to(unit_name)
  addend = converted.value / exponent
  remeasure(value + addend)
end

#-(measurement) ⇒ Object



40
41
42
43
44
45
# File 'lib/alchemist/measurement.rb', line 40

def - measurement
  ensure_shared_type!(measurement)
  converted = measurement.to(unit_name)
  subtrahend = converted.value / exponent
  remeasure(value - subtrahend)
end

#/(measurement) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'lib/alchemist/measurement.rb', line 47

def / measurement
  converted = remeasure(value / dividend(measurement))

  if measurement.is_a?(Measurement)
    converted.value
  else
    converted
  end
end

#<=>(other) ⇒ Object



82
83
84
# File 'lib/alchemist/measurement.rb', line 82

def <=> other
  to_f <=> other.to(unit_name).to_f
end

#==(other) ⇒ Object



86
87
88
# File 'lib/alchemist/measurement.rb', line 86

def == other
  to_f <=> other.to(unit_name).to_f
end

#base(unit_type) ⇒ Object



65
66
67
68
# File 'lib/alchemist/measurement.rb', line 65

def base unit_type
  conversion_base = conversion_base_for(unit_type)
  convert_to_base(conversion_base)
end

#ceil(*args) ⇒ Object



106
107
108
# File 'lib/alchemist/measurement.rb', line 106

def ceil(*args)
  remeasure(value.ceil(*args))
end

#coerce(number) ⇒ Object



98
99
100
# File 'lib/alchemist/measurement.rb', line 98

def coerce(number)
  [self, number]
end

#floor(*args) ⇒ Object



110
111
112
# File 'lib/alchemist/measurement.rb', line 110

def floor(*args)
  remeasure(value.floor(*args))
end

#geospatialObject



5
6
7
# File 'lib/alchemist/geospatial.rb', line 5

def geospatial
  Alchemist::Earth.new(self).geospatial
end

#perObject



21
22
23
# File 'lib/alchemist/measurement.rb', line 21

def per
  CompoundMeasurement.new self
end

#round(*args) ⇒ Object



102
103
104
# File 'lib/alchemist/measurement.rb', line 102

def round(*args)
  remeasure(value.round(*args))
end

#shared_types(other_unit_name) ⇒ Object



94
95
96
# File 'lib/alchemist/measurement.rb', line 94

def shared_types other_unit_name
  types & library.measurement_for(other_unit_name)
end

#to(type = nil) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/alchemist/measurement.rb', line 25

def to type = nil
  if type
    convertor.send(type, exponent)
  else
    convertor
  end
end

#to_fObject



78
79
80
# File 'lib/alchemist/measurement.rb', line 78

def to_f
  (precise_value * exponent).to_f
end

#to_iObject



74
75
76
# File 'lib/alchemist/measurement.rb', line 74

def to_i
  to_f.to_i
end

#to_sObject



70
71
72
# File 'lib/alchemist/measurement.rb', line 70

def to_s
  to_f.to_s
end

#typesObject



90
91
92
# File 'lib/alchemist/measurement.rb', line 90

def types
  library.measurement_for(unprefixed_unit_name)
end

#unit_nameObject



17
18
19
# File 'lib/alchemist/measurement.rb', line 17

def unit_name
  "#{prefix}#{unprefixed_unit_name}"
end