Class: Data::Pressure
- Inherits:
-
Units
- Object
- Units
- Data::Pressure
- Defined in:
- lib/barometer/data/pressure.rb
Overview
A simple Pressure 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 metric
NOTE: the metric units for pressure aren’t commonly used, except
that this class was designed for storing weather data,
and it seems that it is more common in this case
Constant Summary collapse
- METRIC_UNITS =
"mb"
- IMPERIAL_UNITS =
"in"
Instance Attribute Summary collapse
-
#inches ⇒ Object
Returns the value of attribute inches.
-
#millibars ⇒ Object
Returns the value of attribute millibars.
Class Method Summary collapse
- .in_to_mb(inches) ⇒ Object
-
.mb_to_in(mb) ⇒ Object
CONVERTERS.
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
OPERATORS.
- #imperial_default=(value) ⇒ Object
-
#in(as_integer = true) ⇒ Object
return the stored inches or convert from millibars.
-
#in=(inches) ⇒ Object
store inches.
-
#initialize(metric = true) ⇒ Pressure
constructor
A new instance of Pressure.
-
#mb(as_integer = true) ⇒ Object
return the stored millibars or convert from inches.
-
#mb=(mb) ⇒ Object
store millibars.
- #metric_default=(value) ⇒ Object
- #nil? ⇒ Boolean
-
#to_f(metric = nil) ⇒ Object
will just return the value (no units) with more precision.
-
#to_i(metric = nil) ⇒ Object
will just return the value (no units).
-
#to_s(metric = nil) ⇒ Object
will return the value with units.
-
#units(metric = nil) ⇒ Object
will just return the units (no value).
-
#update_inches(mb) ⇒ Object
when we set millibars, it is possible the a non-equivalent value of inches remains.
-
#update_millibars(inches) ⇒ Object
when we set inches, it is possible the a non-equivalent value of millibars remains.
Constructor Details
#initialize(metric = true) ⇒ Pressure
Returns a new instance of Pressure.
22 23 24 25 26 |
# File 'lib/barometer/data/pressure.rb', line 22 def initialize(metric=true) @millibars = nil @inches = nil super(metric) end |
Instance Attribute Details
#inches ⇒ Object
Returns the value of attribute inches.
20 21 22 |
# File 'lib/barometer/data/pressure.rb', line 20 def inches @inches end |
#millibars ⇒ Object
Returns the value of attribute millibars.
20 21 22 |
# File 'lib/barometer/data/pressure.rb', line 20 def @millibars end |
Class Method Details
.in_to_mb(inches) ⇒ Object
40 41 42 43 44 |
# File 'lib/barometer/data/pressure.rb', line 40 def self.in_to_mb(inches) return nil unless inches && (inches.is_a?(Integer) || inches.is_a?(Float)) inches.to_f * 33.8639 end |
.mb_to_in(mb) ⇒ Object
CONVERTERS
35 36 37 38 |
# File 'lib/barometer/data/pressure.rb', line 35 def self.mb_to_in(mb) return nil unless mb && (mb.is_a?(Integer) || mb.is_a?(Float)) mb.to_f * 0.02953 end |
Instance Method Details
#<=>(other) ⇒ Object
OPERATORS
84 85 86 |
# File 'lib/barometer/data/pressure.rb', line 84 def <=>(other) self.mb <=> other.mb end |
#imperial_default=(value) ⇒ Object
29 |
# File 'lib/barometer/data/pressure.rb', line 29 def imperial_default=(value); self.in = value; end |
#in(as_integer = true) ⇒ Object
return the stored inches or convert from millibars
75 76 77 78 |
# File 'lib/barometer/data/pressure.rb', line 75 def in(as_integer=true) inches = (@inches || Data::Pressure.mb_to_in(@millibars)) inches ? (as_integer ? inches.to_i : (100*inches).round/100.0) : nil end |
#in=(inches) ⇒ Object
store inches
60 61 62 63 64 |
# File 'lib/barometer/data/pressure.rb', line 60 def in=(inches) return if !inches || !(inches.is_a?(Integer) || inches.is_a?(Float)) @inches = inches.to_f self.(inches.to_f) end |
#mb(as_integer = true) ⇒ Object
return the stored millibars or convert from inches
68 69 70 71 |
# File 'lib/barometer/data/pressure.rb', line 68 def mb(as_integer=true) mb = (@millibars || Data::Pressure.in_to_mb(@inches)) mb ? (as_integer ? mb.to_i : (100*mb).round/100.0) : nil end |
#mb=(mb) ⇒ Object
store millibars
52 53 54 55 56 |
# File 'lib/barometer/data/pressure.rb', line 52 def mb=(mb) return if !mb || !(mb.is_a?(Integer) || mb.is_a?(Float)) @millibars = mb.to_f self.update_inches(mb.to_f) end |
#metric_default=(value) ⇒ Object
28 |
# File 'lib/barometer/data/pressure.rb', line 28 def metric_default=(value); self.mb = value; end |
#nil? ⇒ Boolean
138 139 140 |
# File 'lib/barometer/data/pressure.rb', line 138 def nil? (@millibars || @inches) ? false : true end |
#to_f(metric = nil) ⇒ Object
will just return the value (no units) with more precision
100 101 102 |
# File 'lib/barometer/data/pressure.rb', line 100 def to_f(metric=nil) (metric || (metric.nil? && self.metric?)) ? self.mb(false) : self.in(false) end |
#to_i(metric = nil) ⇒ Object
will just return the value (no units)
94 95 96 |
# File 'lib/barometer/data/pressure.rb', line 94 def to_i(metric=nil) (metric || (metric.nil? && self.metric?)) ? self.mb : self.in end |
#to_s(metric = nil) ⇒ Object
will return the value with units
106 107 108 |
# File 'lib/barometer/data/pressure.rb', line 106 def to_s(metric=nil) (metric || (metric.nil? && self.metric?)) ? "#{self.mb} #{METRIC_UNITS}" : "#{self.in} #{IMPERIAL_UNITS}" end |
#units(metric = nil) ⇒ Object
will just return the units (no value)
112 113 114 |
# File 'lib/barometer/data/pressure.rb', line 112 def units(metric=nil) (metric || (metric.nil? && self.metric?)) ? METRIC_UNITS : IMPERIAL_UNITS end |
#update_inches(mb) ⇒ Object
when we set millibars, it is possible the a non-equivalent value of inches remains. if so, clear it.
130 131 132 133 134 135 136 |
# File 'lib/barometer/data/pressure.rb', line 130 def update_inches(mb) return unless @inches difference = Data::Pressure.mb_to_in(mb.to_f) - @inches # only clear inches if the stored inches is off be more then 1 unit # then the conversion of millibars @inches = nil unless difference.abs <= 1.0 end |
#update_millibars(inches) ⇒ Object
when we set inches, it is possible the a non-equivalent value of millibars remains. if so, clear it.
119 120 121 122 123 124 125 |
# File 'lib/barometer/data/pressure.rb', line 119 def (inches) return unless @millibars difference = Data::Pressure.in_to_mb(inches.to_f) - @millibars # only clear millibars if the stored millibars is off be more then 1 unit # then the conversion of inches @millibars = nil unless difference.abs <= 1.0 end |