Class: Data::Temperature

Inherits:
Units
  • Object
show all
Defined in:
lib/barometer/data/temperature.rb

Overview

A simple Temperature class

Think of this like the Integer class. Enhancement is that you can create a number (in a certain unit), then get that number back already converted to another unit.

All comparison operations will be done in the absolute scale of Kelvin (K)

Constant Summary collapse

METRIC_UNITS =
"C"
IMPERIAL_UNITS =
"F"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(metric = true) ⇒ Temperature

Returns a new instance of Temperature.



19
20
21
22
23
24
# File 'lib/barometer/data/temperature.rb', line 19

def initialize(metric=true)
  @celsius = nil
  @fahrenheit = nil
  @kelvin = nil
  super(metric)
end

Instance Attribute Details

#celsiusObject

Returns the value of attribute celsius.



17
18
19
# File 'lib/barometer/data/temperature.rb', line 17

def celsius
  @celsius
end

#fahrenheitObject

Returns the value of attribute fahrenheit.



17
18
19
# File 'lib/barometer/data/temperature.rb', line 17

def fahrenheit
  @fahrenheit
end

#kelvinObject

Returns the value of attribute kelvin.



17
18
19
# File 'lib/barometer/data/temperature.rb', line 17

def kelvin
  @kelvin
end

Class Method Details

.c_to_f(c) ⇒ Object

Tf = (9/5)*Tc+32



40
41
42
43
# File 'lib/barometer/data/temperature.rb', line 40

def self.c_to_f(c)
  return nil unless c && (c.is_a?(Integer) || c.is_a?(Float))
   ((9.0/5.0)*c.to_f)+32.0
end

.c_to_k(c) ⇒ Object

CONVERTERS



33
34
35
36
# File 'lib/barometer/data/temperature.rb', line 33

def self.c_to_k(c)
  return nil unless c && (c.is_a?(Integer) || c.is_a?(Float))
  273.15 + c.to_f
end

.f_to_c(f) ⇒ Object

Tc = (5/9)*(Tf-32)



47
48
49
50
# File 'lib/barometer/data/temperature.rb', line 47

def self.f_to_c(f)
  return nil unless f && (f.is_a?(Integer) || f.is_a?(Float))
  (5.0/9.0)*(f.to_f-32.0)
end

.f_to_k(f) ⇒ Object



52
53
54
55
56
# File 'lib/barometer/data/temperature.rb', line 52

def self.f_to_k(f)
  return nil unless f && (f.is_a?(Integer) || f.is_a?(Float))
  c = self.f_to_c(f.to_f)
  self.c_to_k(c)
end

.k_to_c(k) ⇒ Object



58
59
60
61
# File 'lib/barometer/data/temperature.rb', line 58

def self.k_to_c(k)
  return nil unless k && (k.is_a?(Integer) || k.is_a?(Float))
  k.to_f - 273.15
end

.k_to_f(k) ⇒ Object



63
64
65
66
67
# File 'lib/barometer/data/temperature.rb', line 63

def self.k_to_f(k)
  return nil unless k && (k.is_a?(Integer) || k.is_a?(Float))
  c = self.k_to_c(k.to_f)
  self.c_to_f(c)
end

Instance Method Details

#<=>(other) ⇒ Object

OPERATORS



118
119
120
# File 'lib/barometer/data/temperature.rb', line 118

def <=>(other)
  @kelvin <=> other.kelvin
end

#c(as_integer = true) ⇒ Object

return the stored celsius or convert from Kelvin



102
103
104
105
# File 'lib/barometer/data/temperature.rb', line 102

def c(as_integer=true)
  c = (@celsius || Data::Temperature.k_to_c(@kelvin))
  c ? (as_integer ? c.to_i : (100*c).round/100.0) : nil
end

#c=(c) ⇒ Object

store celsius and kelvin



75
76
77
78
79
80
# File 'lib/barometer/data/temperature.rb', line 75

def c=(c)
  return if !c || !(c.is_a?(Integer) || c.is_a?(Float))
  @celsius = c.to_f
  @kelvin = Data::Temperature.c_to_k(c.to_f)
  self.update_fahrenheit(c.to_f)
end

#f(as_integer = true) ⇒ Object

return the stored fahrenheit or convert from Kelvin



109
110
111
112
# File 'lib/barometer/data/temperature.rb', line 109

def f(as_integer=true)
  f = (@fahrenheit || Data::Temperature.k_to_f(@kelvin))
  f ? (as_integer ? f.to_i : (100*f).round/100.0) : nil
end

#f=(f) ⇒ Object

store fahrenheit and kelvin



84
85
86
87
88
89
# File 'lib/barometer/data/temperature.rb', line 84

def f=(f)
  return if !f || !(f.is_a?(Integer) || f.is_a?(Float))
  @fahrenheit = f.to_f
  @kelvin = Data::Temperature.f_to_k(f.to_f)
  self.update_celsius(f.to_f)
end

#imperial_default=(value) ⇒ Object



27
# File 'lib/barometer/data/temperature.rb', line 27

def imperial_default=(value); self.f = value; end

#k=(k) ⇒ Object

store kelvin, convert to all



93
94
95
96
97
98
# File 'lib/barometer/data/temperature.rb', line 93

def k=(k)
  return if !k || !(k.is_a?(Integer) || k.is_a?(Float))
  @kelvin = k.to_f
  @celsius = Data::Temperature.k_to_c(k.to_f)
  @fahrenheit = Data::Temperature.k_to_f(k.to_f)
end

#metric_default=(value) ⇒ Object



26
# File 'lib/barometer/data/temperature.rb', line 26

def metric_default=(value); self.c = value; end

#nil?Boolean

Returns:

  • (Boolean)


172
173
174
# File 'lib/barometer/data/temperature.rb', line 172

def nil?
  (@celsius || @fahrenheit || @kelvin) ? false : true
end

#to_f(metric = nil) ⇒ Object

will just return the value (no units) with more precision



134
135
136
# File 'lib/barometer/data/temperature.rb', line 134

def to_f(metric=nil)
  (metric || (metric.nil? && self.metric?)) ? self.c(false) : self.f(false)
end

#to_i(metric = nil) ⇒ Object

will just return the value (no units)



128
129
130
# File 'lib/barometer/data/temperature.rb', line 128

def to_i(metric=nil)
  (metric || (metric.nil? && self.metric?)) ? self.c : self.f
end

#to_s(metric = nil) ⇒ Object

will return the value with units



140
141
142
# File 'lib/barometer/data/temperature.rb', line 140

def to_s(metric=nil)
  (metric || (metric.nil? && self.metric?)) ? "#{self.c} #{METRIC_UNITS}" : "#{self.f} #{IMPERIAL_UNITS}"
end

#units(metric = nil) ⇒ Object

will just return the units (no value)



146
147
148
# File 'lib/barometer/data/temperature.rb', line 146

def units(metric=nil)
  (metric || (metric.nil? && self.metric?)) ? METRIC_UNITS : IMPERIAL_UNITS
end

#update_celsius(f) ⇒ Object

when we set fahrenheit, it is possible the a non-equivalent value of celsius remains. if so, clear it.



153
154
155
156
157
158
159
# File 'lib/barometer/data/temperature.rb', line 153

def update_celsius(f)
  return unless @celsius
  difference = Data::Temperature.f_to_c(f.to_f) - @celsius
  # only clear celsius if the stored celsius is off be more then 1 degree
  # then the conversion of fahrenheit
  @celsius = nil unless difference.abs <= 1.0
end

#update_fahrenheit(c) ⇒ Object

when we set celsius, it is possible the a non-equivalent value of fahrenheit remains. if so, clear it.



164
165
166
167
168
169
170
# File 'lib/barometer/data/temperature.rb', line 164

def update_fahrenheit(c)
  return unless @fahrenheit
  difference = Data::Temperature.c_to_f(c.to_f) - @fahrenheit
  # only clear fahrenheit if the stored fahrenheit is off be more then 1 degree
  # then the conversion of celsius
  @fahrenheit = nil unless difference.abs <= 1.0
end