Class: BasicTemperature
- Inherits:
-
Object
show all
- Defined in:
- lib/basic_temperature.rb,
lib/basic_temperature/version.rb
Overview
Value Object for basic temperature operations like conversions from Celcius to Fahrenhait or Kelvin etc. rubocop:disable Metrics/ClassLength
Defined Under Namespace
Classes: CoersionError, InitializationArgumentsError, InvalidDegreesError, InvalidOtherError, InvalidScaleError
Constant Summary
collapse
- SCALES =
[
CELCIUS = 'celcius',
FAHRENHEIT = 'fahrenheit',
KELVIN = 'kelvin'
]
.freeze
- VERSION =
'0.2.1'
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(*positional_arguments, **keyword_arguments) ⇒ BasicTemperature
Returns a new instance of BasicTemperature.
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/basic_temperature.rb', line 24
def initialize(*positional_arguments, **keyword_arguments)
raise_initialization_arguments_error if positional_arguments.any? && keyword_arguments.any?
raise_initialization_arguments_error if positional_arguments.none? && keyword_arguments.none?
if keyword_arguments.any?
initialize_via_keywords_arguments(keyword_arguments)
else
initialize_via_positional_arguments(positional_arguments)
end
end
|
Instance Attribute Details
#degrees ⇒ Object
Returns the value of attribute degrees.
22
23
24
|
# File 'lib/basic_temperature.rb', line 22
def degrees
@degrees
end
|
#scale ⇒ Object
Returns the value of attribute scale.
22
23
24
|
# File 'lib/basic_temperature.rb', line 22
def scale
@scale
end
|
Instance Method Details
#+(other) ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/basic_temperature.rb', line 114
def +(other)
degrees, scale =
case other
when Numeric
[self.degrees + other, self.scale]
when BasicTemperature
[self.to_scale(other.scale).degrees + other.degrees, other.scale]
else
raise_invalid_other_error(other)
end
BasicTemperature.new(degrees, scale)
end
|
#-(other) ⇒ Object
128
129
130
|
# File 'lib/basic_temperature.rb', line 128
def -(other)
self + -other
end
|
#-@ ⇒ Object
132
133
134
|
# File 'lib/basic_temperature.rb', line 132
def -@
BasicTemperature.new(-self.degrees, self.scale)
end
|
#<=>(other) ⇒ Object
146
147
148
149
150
|
# File 'lib/basic_temperature.rb', line 146
def <=>(other)
return unless other.instance_of?(BasicTemperature)
self.to_scale(other.scale).degrees <=> other.degrees
end
|
#==(other) ⇒ Object
108
109
110
111
112
|
# File 'lib/basic_temperature.rb', line 108
def ==(other)
return false unless other.instance_of?(BasicTemperature)
self.degrees == other.degrees && self.scale == other.scale
end
|
#coerce(numeric) ⇒ Object
136
137
138
139
140
|
# File 'lib/basic_temperature.rb', line 136
def coerce(numeric)
assert_numeric!(numeric)
[BasicTemperature.new(numeric, self.scale), self]
end
|
#inspect ⇒ Object
142
143
144
|
# File 'lib/basic_temperature.rb', line 142
def inspect
"#{degrees.to_i} #{scale.capitalize}"
end
|
#set_degrees(degrees) ⇒ Object
rubocop:disable Naming/AccessorMethodName
36
37
38
|
# File 'lib/basic_temperature.rb', line 36
def set_degrees(degrees)
BasicTemperature.new(degrees, scale)
end
|
#set_scale(scale) ⇒ Object
rubocop:disable Naming/AccessorMethodName
42
43
44
|
# File 'lib/basic_temperature.rb', line 42
def set_scale(scale)
self.to_scale(scale)
end
|
#to_celsius ⇒ Object
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/basic_temperature.rb', line 60
def to_celsius
return @to_celsius unless @to_celsius.nil?
return @to_celsius = self if self.scale == CELCIUS
degrees =
case self.scale
when FAHRENHEIT
(self.degrees - 32) * (5 / 9r)
when KELVIN
self.degrees - 273.15
end
@to_celsius = BasicTemperature.new(degrees, CELCIUS)
end
|
#to_fahrenheit ⇒ Object
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/basic_temperature.rb', line 76
def to_fahrenheit
return @to_fahrenheit unless @to_fahrenheit.nil?
return @to_fahrenheit = self if self.scale == FAHRENHEIT
degrees =
case self.scale
when CELCIUS
self.degrees * (9 / 5r) + 32
when KELVIN
self.degrees * (9 / 5r) - 459.67
end
@to_fahrenheit = BasicTemperature.new(degrees, FAHRENHEIT)
end
|
#to_kelvin ⇒ Object
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/basic_temperature.rb', line 92
def to_kelvin
return @to_kelvin unless @to_kelvin.nil?
return @to_kelvin = self if self.scale == KELVIN
degrees =
case self.scale
when CELCIUS
self.degrees + 273.15
when FAHRENHEIT
(self.degrees + 459.67) * (5 / 9r)
end
@to_kelvin = BasicTemperature.new(degrees, KELVIN)
end
|
#to_scale(scale) ⇒ Object
rubocop:enable Naming/AccessorMethodName
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'lib/basic_temperature.rb', line 47
def to_scale(scale)
case cast_scale(scale)
when CELCIUS
to_celsius
when FAHRENHEIT
to_fahrenheit
when KELVIN
to_kelvin
else
raise_invalid_scale_error
end
end
|