basic_temperature 0.2.2 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.inch.yml +3 -0
- data/.rubocop.yml +3 -0
- data/CHANGELOG.md +68 -7
- data/README.md +41 -61
- data/bin/console +1 -1
- data/docs/classes/BasicTemperature.html +27 -1129
- data/docs/classes/BasicTemperature/Temperature.html +1239 -0
- data/docs/classes/BasicTemperature/Temperature/AdditionalHelpers.html +78 -0
- data/docs/classes/BasicTemperature/Temperature/Assertions.html +78 -0
- data/docs/classes/BasicTemperature/Temperature/Casting.html +78 -0
- data/docs/classes/BasicTemperature/Temperature/Errors.html +109 -0
- data/docs/classes/BasicTemperature/Temperature/Errors/InitializationArguments.html +94 -0
- data/docs/classes/BasicTemperature/Temperature/Errors/InvalidDegrees.html +94 -0
- data/docs/classes/BasicTemperature/Temperature/Errors/InvalidNumeric.html +94 -0
- data/docs/classes/BasicTemperature/Temperature/Errors/InvalidNumericOrTemperature.html +94 -0
- data/docs/classes/BasicTemperature/Temperature/Errors/InvalidScale.html +94 -0
- data/docs/classes/BasicTemperature/Temperature/Initialization.html +78 -0
- data/docs/classes/BasicTemperature/Temperature/Memoization.html +78 -0
- data/docs/classes/BasicTemperature/Temperature/Rounding.html +78 -0
- data/docs/classes/Object.html +3 -175
- data/docs/created.rid +12 -4
- data/docs/files/lib/basic_temperature/alias_rb.html +73 -0
- data/docs/files/lib/basic_temperature/temperature/additional_helpers_rb.html +94 -0
- data/docs/files/lib/basic_temperature/temperature/assertions_rb.html +94 -0
- data/docs/files/lib/basic_temperature/temperature/casting_rb.html +94 -0
- data/docs/files/lib/basic_temperature/temperature/errors_rb.html +119 -0
- data/docs/files/lib/basic_temperature/temperature/initialization_rb.html +94 -0
- data/docs/files/lib/basic_temperature/temperature/memoization_rb.html +94 -0
- data/docs/files/lib/basic_temperature/temperature/rounding_rb.html +94 -0
- data/docs/files/lib/basic_temperature/temperature_rb.html +17 -1
- data/docs/files/lib/basic_temperature/version_rb.html +2 -2
- data/docs/files/lib/basic_temperature_rb.html +2 -30
- data/docs/js/navigation.js.gz +0 -0
- data/docs/js/search_index.js +1 -1
- data/docs/js/search_index.js.gz +0 -0
- data/docs/js/searcher.js.gz +0 -0
- data/docs/panel/links.html +16 -0
- data/docs/panel/tree.js +1 -1
- data/lib/basic_temperature.rb +3 -700
- data/lib/basic_temperature/alias.rb +5 -0
- data/lib/basic_temperature/temperature.rb +515 -2
- data/lib/basic_temperature/temperature/additional_helpers.rb +17 -0
- data/lib/basic_temperature/temperature/assertions.rb +30 -0
- data/lib/basic_temperature/temperature/casting.rb +19 -0
- data/lib/basic_temperature/temperature/errors.rb +50 -0
- data/lib/basic_temperature/temperature/initialization.rb +32 -0
- data/lib/basic_temperature/temperature/memoization.rb +26 -0
- data/lib/basic_temperature/temperature/rounding.rb +13 -0
- data/lib/basic_temperature/version.rb +2 -2
- metadata +32 -2
@@ -24,7 +24,7 @@
|
|
24
24
|
lib/basic_temperature/temperature.rb
|
25
25
|
|
26
26
|
</li>
|
27
|
-
<li>Last modified: 2020-
|
27
|
+
<li>Last modified: 2020-04-16 17:45:21 +0300</li>
|
28
28
|
</ul>
|
29
29
|
</div>
|
30
30
|
|
@@ -40,6 +40,22 @@
|
|
40
40
|
|
41
41
|
|
42
42
|
|
43
|
+
|
44
|
+
<!-- Namespace -->
|
45
|
+
<div class="sectiontitle">Namespace</div>
|
46
|
+
<ul>
|
47
|
+
|
48
|
+
<li>
|
49
|
+
<span class="type">MODULE</span>
|
50
|
+
<a href="../../../classes/BasicTemperature.html">BasicTemperature</a>
|
51
|
+
</li>
|
52
|
+
|
53
|
+
<li>
|
54
|
+
<span class="type">CLASS</span>
|
55
|
+
<a href="../../../classes/BasicTemperature/Temperature.html">BasicTemperature::Temperature</a>
|
56
|
+
</li>
|
57
|
+
|
58
|
+
</ul>
|
43
59
|
|
44
60
|
|
45
61
|
|
@@ -24,7 +24,7 @@
|
|
24
24
|
lib/basic_temperature/version.rb
|
25
25
|
|
26
26
|
</li>
|
27
|
-
<li>Last modified: 2020-03-
|
27
|
+
<li>Last modified: 2020-03-26 17:27:15 +0200</li>
|
28
28
|
</ul>
|
29
29
|
</div>
|
30
30
|
|
@@ -46,7 +46,7 @@
|
|
46
46
|
<ul>
|
47
47
|
|
48
48
|
<li>
|
49
|
-
<span class="type">
|
49
|
+
<span class="type">MODULE</span>
|
50
50
|
<a href="../../../classes/BasicTemperature.html">BasicTemperature</a>
|
51
51
|
</li>
|
52
52
|
|
@@ -24,7 +24,7 @@
|
|
24
24
|
lib/basic_temperature.rb
|
25
25
|
|
26
26
|
</li>
|
27
|
-
<li>Last modified: 2020-03-26
|
27
|
+
<li>Last modified: 2020-03-26 17:27:15 +0200</li>
|
28
28
|
</ul>
|
29
29
|
</div>
|
30
30
|
|
@@ -34,14 +34,6 @@
|
|
34
34
|
|
35
35
|
|
36
36
|
|
37
|
-
<!-- File only: requires -->
|
38
|
-
<div class="sectiontitle">Required Files</div>
|
39
|
-
<ul>
|
40
|
-
|
41
|
-
<li>basic_temperature/version</li>
|
42
|
-
|
43
|
-
</ul>
|
44
|
-
|
45
37
|
|
46
38
|
|
47
39
|
|
@@ -54,30 +46,10 @@
|
|
54
46
|
<ul>
|
55
47
|
|
56
48
|
<li>
|
57
|
-
<span class="type">
|
49
|
+
<span class="type">MODULE</span>
|
58
50
|
<a href="../../classes/BasicTemperature.html">BasicTemperature</a>
|
59
51
|
</li>
|
60
52
|
|
61
|
-
<li>
|
62
|
-
<span class="type">CLASS</span>
|
63
|
-
<a href="../../classes/BasicTemperature/InitializationArgumentsError.html">BasicTemperature::InitializationArgumentsError</a>
|
64
|
-
</li>
|
65
|
-
|
66
|
-
<li>
|
67
|
-
<span class="type">CLASS</span>
|
68
|
-
<a href="../../classes/BasicTemperature/InvalidDegreesError.html">BasicTemperature::InvalidDegreesError</a>
|
69
|
-
</li>
|
70
|
-
|
71
|
-
<li>
|
72
|
-
<span class="type">CLASS</span>
|
73
|
-
<a href="../../classes/BasicTemperature/InvalidNumericOrTemperatureError.html">BasicTemperature::InvalidNumericOrTemperatureError</a>
|
74
|
-
</li>
|
75
|
-
|
76
|
-
<li>
|
77
|
-
<span class="type">CLASS</span>
|
78
|
-
<a href="../../classes/BasicTemperature/InvalidScaleError.html">BasicTemperature::InvalidScaleError</a>
|
79
|
-
</li>
|
80
|
-
|
81
53
|
</ul>
|
82
54
|
|
83
55
|
|
data/docs/js/navigation.js.gz
CHANGED
Binary file
|
data/docs/js/search_index.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
var search_data = {"index":{"searchIndex":["temperature","
|
1
|
+
var search_data = {"index":{"searchIndex":["basictemperature","temperature","additionalhelpers","assertions","casting","errors","initializationarguments","invaliddegrees","invalidnumeric","invalidnumericortemperature","invalidscale","initialization","memoization","rounding","object","<=>()","[]()","boil_water?()","freeze_water?()","new()","set_degrees()","set_scale()","to_celsius()","to_fahrenheit()","to_kelvin()","to_rankine()","to_scale()"],"longSearchIndex":["basictemperature","basictemperature::temperature","basictemperature::temperature::additionalhelpers","basictemperature::temperature::assertions","basictemperature::temperature::casting","basictemperature::temperature::errors","basictemperature::temperature::errors::initializationarguments","basictemperature::temperature::errors::invaliddegrees","basictemperature::temperature::errors::invalidnumeric","basictemperature::temperature::errors::invalidnumericortemperature","basictemperature::temperature::errors::invalidscale","basictemperature::temperature::initialization","basictemperature::temperature::memoization","basictemperature::temperature::rounding","object","basictemperature::temperature#<=>()","basictemperature::temperature::[]()","basictemperature::temperature#boil_water?()","basictemperature::temperature#freeze_water?()","basictemperature::temperature::new()","basictemperature::temperature#set_degrees()","basictemperature::temperature#set_scale()","basictemperature::temperature#to_celsius()","basictemperature::temperature#to_fahrenheit()","basictemperature::temperature#to_kelvin()","basictemperature::temperature#to_rankine()","basictemperature::temperature#to_scale()"],"info":[["BasicTemperature","","classes/BasicTemperature.html","",""],["BasicTemperature::Temperature","","classes/BasicTemperature/Temperature.html","","<p>Temperature is a simple Value Object for basic temperature operations like conversions from <code>Celsius</code> to …\n"],["BasicTemperature::Temperature::AdditionalHelpers","","classes/BasicTemperature/Temperature/AdditionalHelpers.html","",""],["BasicTemperature::Temperature::Assertions","","classes/BasicTemperature/Temperature/Assertions.html","",""],["BasicTemperature::Temperature::Casting","","classes/BasicTemperature/Temperature/Casting.html","",""],["BasicTemperature::Temperature::Errors","","classes/BasicTemperature/Temperature/Errors.html","",""],["BasicTemperature::Temperature::Errors::InitializationArguments","","classes/BasicTemperature/Temperature/Errors/InitializationArguments.html","","<p>Raised when <code>Temperature.new</code> is called with mixed positional and keyword arguments or without arguments …\n"],["BasicTemperature::Temperature::Errors::InvalidDegrees","","classes/BasicTemperature/Temperature/Errors/InvalidDegrees.html","","<p>Raised when <code>degrees</code> is not a Numeric.\n"],["BasicTemperature::Temperature::Errors::InvalidNumeric","","classes/BasicTemperature/Temperature/Errors/InvalidNumeric.html","","<p>Raised when <code>other</code> is not a Numeric in math operations.\n"],["BasicTemperature::Temperature::Errors::InvalidNumericOrTemperature","","classes/BasicTemperature/Temperature/Errors/InvalidNumericOrTemperature.html","","<p>Raised when <code>other</code> is neither Numeric nor Temperature in math operations.\n"],["BasicTemperature::Temperature::Errors::InvalidScale","","classes/BasicTemperature/Temperature/Errors/InvalidScale.html","","<p>Raised when <code>scale</code> can not be casted to any possible scale value. See SCALES.\n"],["BasicTemperature::Temperature::Initialization","","classes/BasicTemperature/Temperature/Initialization.html","",""],["BasicTemperature::Temperature::Memoization","","classes/BasicTemperature/Temperature/Memoization.html","",""],["BasicTemperature::Temperature::Rounding","","classes/BasicTemperature/Temperature/Rounding.html","",""],["Object","","classes/Object.html","",""],["<=>","BasicTemperature::Temperature","classes/BasicTemperature/Temperature.html#method-i-3C-3D-3E","(other)","<p>Compares temperture with <code>other</code> temperature.\n<p>Returns <code>0</code> if they are considered as equal.\n<p>Two temperatures …\n"],["[]","BasicTemperature::Temperature","classes/BasicTemperature/Temperature.html#method-c-5B-5D","(*args, **kwargs)","<p>Creates a new instance of Temperature. Alias for <code>new</code>.\n"],["boil_water?","BasicTemperature::Temperature","classes/BasicTemperature/Temperature.html#method-i-boil_water-3F","()","<p>Returns true when temperature boils water (is greater than or equal to 100 °C), false otherwise.\n"],["freeze_water?","BasicTemperature::Temperature","classes/BasicTemperature/Temperature.html#method-i-freeze_water-3F","()","<p>Returns true when temperature freezes water (is less than or equal to 0 °C), false otherwise.\n"],["new","BasicTemperature::Temperature","classes/BasicTemperature/Temperature.html#method-c-new","(*positional_arguments, **keyword_arguments)","<p>Creates a new instance of Temperature. Is aliased as <code>[]</code>.\n"],["set_degrees","BasicTemperature::Temperature","classes/BasicTemperature/Temperature.html#method-i-set_degrees","(degrees)","<p>Returns a new Temperature with updated <code>degrees</code>.\n\n<pre><code>temperature = Temperature[0, :celsius]\n# => 0 °C\n\nnew_temperature ...\n</code></pre>\n"],["set_scale","BasicTemperature::Temperature","classes/BasicTemperature/Temperature.html#method-i-set_scale","(scale)","<p>Returns a new Temperature with updated <code>scale</code>.\n\n<pre><code>temperature = Temperature[0, :celsius]\n# => 0 °C\n\nnew_temperature ...\n</code></pre>\n"],["to_celsius","BasicTemperature::Temperature","classes/BasicTemperature/Temperature.html#method-i-to_celsius","()","<p>Converts temperature to Celsius scale. If temperature is already in Celsius, returns current temperature …\n"],["to_fahrenheit","BasicTemperature::Temperature","classes/BasicTemperature/Temperature.html#method-i-to_fahrenheit","()","<p>Converts temperature to Fahrenheit scale. If temperature is already in Fahrenheit, returns current temperature …\n"],["to_kelvin","BasicTemperature::Temperature","classes/BasicTemperature/Temperature.html#method-i-to_kelvin","()","<p>Converts temperature to Kelvin scale. If temperature is already in Kelvin, returns current\n\n<pre><code>temperature ...\n</code></pre>\n"],["to_rankine","BasicTemperature::Temperature","classes/BasicTemperature/Temperature.html#method-i-to_rankine","()","<p>Converts temperature to Rankine scale. If temperature is already in Rankine, returns current temperature …\n"],["to_scale","BasicTemperature::Temperature","classes/BasicTemperature/Temperature.html#method-i-to_scale","(scale)","<p>Converts temperature to specific <code>scale</code>. If temperature is already in desired <code>scale</code>, returns current temperature …\n"]]}}
|
data/docs/js/search_index.js.gz
CHANGED
Binary file
|
data/docs/js/searcher.js.gz
CHANGED
Binary file
|
data/docs/panel/links.html
CHANGED
@@ -4,8 +4,24 @@
|
|
4
4
|
|
5
5
|
<a href="../files/lib/basic_temperature_rb.html">lib/basic_temperature.rb</a>
|
6
6
|
|
7
|
+
<a href="../files/lib/basic_temperature/alias_rb.html">lib/basic_temperature/alias.rb</a>
|
8
|
+
|
7
9
|
<a href="../files/lib/basic_temperature/temperature_rb.html">lib/basic_temperature/temperature.rb</a>
|
8
10
|
|
11
|
+
<a href="../files/lib/basic_temperature/temperature/additional_helpers_rb.html">lib/basic_temperature/temperature/additional_helpers.rb</a>
|
12
|
+
|
13
|
+
<a href="../files/lib/basic_temperature/temperature/assertions_rb.html">lib/basic_temperature/temperature/assertions.rb</a>
|
14
|
+
|
15
|
+
<a href="../files/lib/basic_temperature/temperature/casting_rb.html">lib/basic_temperature/temperature/casting.rb</a>
|
16
|
+
|
17
|
+
<a href="../files/lib/basic_temperature/temperature/errors_rb.html">lib/basic_temperature/temperature/errors.rb</a>
|
18
|
+
|
19
|
+
<a href="../files/lib/basic_temperature/temperature/initialization_rb.html">lib/basic_temperature/temperature/initialization.rb</a>
|
20
|
+
|
21
|
+
<a href="../files/lib/basic_temperature/temperature/memoization_rb.html">lib/basic_temperature/temperature/memoization.rb</a>
|
22
|
+
|
23
|
+
<a href="../files/lib/basic_temperature/temperature/rounding_rb.html">lib/basic_temperature/temperature/rounding.rb</a>
|
24
|
+
|
9
25
|
<a href="../files/lib/basic_temperature/version_rb.html">lib/basic_temperature/version.rb</a>
|
10
26
|
|
11
27
|
</body>
|
data/docs/panel/tree.js
CHANGED
@@ -1 +1 @@
|
|
1
|
-
var tree = [["","","files",[["","","lib",[["","","basic_temperature",[["temperature.rb","files/lib/basic_temperature/temperature_rb.html","",[]],["version.rb","files/lib/basic_temperature/version_rb.html","",[]]]],["basic_temperature.rb","files/lib/basic_temperature_rb.html","",[]]]]]],["Temperature","classes/BasicTemperature.html"," < Object",[]],["
|
1
|
+
var tree = [["","","files",[["","","lib",[["","","basic_temperature",[["alias.rb","files/lib/basic_temperature/alias_rb.html","",[]],["","","temperature",[["additional_helpers.rb","files/lib/basic_temperature/temperature/additional_helpers_rb.html","",[]],["assertions.rb","files/lib/basic_temperature/temperature/assertions_rb.html","",[]],["casting.rb","files/lib/basic_temperature/temperature/casting_rb.html","",[]],["errors.rb","files/lib/basic_temperature/temperature/errors_rb.html","",[]],["initialization.rb","files/lib/basic_temperature/temperature/initialization_rb.html","",[]],["memoization.rb","files/lib/basic_temperature/temperature/memoization_rb.html","",[]],["rounding.rb","files/lib/basic_temperature/temperature/rounding_rb.html","",[]]]],["temperature.rb","files/lib/basic_temperature/temperature_rb.html","",[]],["version.rb","files/lib/basic_temperature/version_rb.html","",[]]]],["basic_temperature.rb","files/lib/basic_temperature_rb.html","",[]]]]]],["BasicTemperature","classes/BasicTemperature.html","",[["Temperature","classes/BasicTemperature/Temperature.html"," < Object",[["AdditionalHelpers","classes/BasicTemperature/Temperature/AdditionalHelpers.html","",[]],["Assertions","classes/BasicTemperature/Temperature/Assertions.html","",[]],["Casting","classes/BasicTemperature/Temperature/Casting.html","",[]],["Errors","classes/BasicTemperature/Temperature/Errors.html","",[["InitializationArguments","classes/BasicTemperature/Temperature/Errors/InitializationArguments.html"," < StandardError",[]],["InvalidDegrees","classes/BasicTemperature/Temperature/Errors/InvalidDegrees.html"," < StandardError",[]],["InvalidNumeric","classes/BasicTemperature/Temperature/Errors/InvalidNumeric.html"," < StandardError",[]],["InvalidNumericOrTemperature","classes/BasicTemperature/Temperature/Errors/InvalidNumericOrTemperature.html"," < StandardError",[]],["InvalidScale","classes/BasicTemperature/Temperature/Errors/InvalidScale.html"," < StandardError",[]]]],["Initialization","classes/BasicTemperature/Temperature/Initialization.html","",[]],["Memoization","classes/BasicTemperature/Temperature/Memoization.html","",[]],["Rounding","classes/BasicTemperature/Temperature/Rounding.html","",[]]]]]],["Object","classes/Object.html"," < BasicObject",[]]]
|
data/lib/basic_temperature.rb
CHANGED
@@ -1,703 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
require_relative 'basic_temperature/version'
|
4
|
+
require_relative 'basic_temperature/temperature'
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
##
|
8
|
-
# Temperature is a simple {Value Object}[https://martinfowler.com/bliki/ValueObject.html] for basic
|
9
|
-
# temperature operations like conversions from <tt>Celsius</tt> to <tt>Fahrenhait</tt> or <tt>Kelvin</tt>
|
10
|
-
# etc.
|
11
|
-
#
|
12
|
-
# Supported scales: <tt>Celsius</tt>, <tt>Fahrenheit</tt>, <tt>Kelvin</tt> and <tt>Rankine</tt>.
|
13
|
-
#
|
14
|
-
# == Creating Temperatures
|
15
|
-
#
|
16
|
-
# A new temperature can be created in multiple ways:
|
17
|
-
#
|
18
|
-
# - Using keyword arguments:
|
19
|
-
#
|
20
|
-
# Temperature.new(degrees: 0, scale: :celsius)
|
21
|
-
#
|
22
|
-
# - Using positional arguments:
|
23
|
-
#
|
24
|
-
# Temperature.new(0, :celsius)
|
25
|
-
#
|
26
|
-
# - Even more concise way using <tt>Temperature.[]</tt> (an alias of <tt>Temperature.new</tt>):
|
27
|
-
#
|
28
|
-
# Temperature[0, :celsius]
|
29
|
-
#
|
30
|
-
#
|
31
|
-
# == Creating Temperatures from already existing temperature objects
|
32
|
-
#
|
33
|
-
# Sometimes it is useful to create a new temperature from already existing one.
|
34
|
-
#
|
35
|
-
# For such cases, there are {set_degrees}[rdoc-ref:BasicTemperature#set_degrees and
|
36
|
-
# {set_scale}[rdoc-ref:BasicTemperature#set_scale].
|
37
|
-
#
|
38
|
-
# Since temperatures are {Value Objects}[https://martinfowler.com/bliki/ValueObject.html], both methods
|
39
|
-
# returns new instances.
|
40
|
-
#
|
41
|
-
# Examples:
|
42
|
-
#
|
43
|
-
# temperature = Temperature[0, :celsius]
|
44
|
-
# # => 0 °C
|
45
|
-
#
|
46
|
-
# new_temperature = temperature.set_degrees(15)
|
47
|
-
# # => 15 °C
|
48
|
-
#
|
49
|
-
# temperature = Temperature[0, :celsius]
|
50
|
-
# # => 0 °C
|
51
|
-
#
|
52
|
-
# new_temperature = temperature.set_scale(:kelvin)
|
53
|
-
# # => 0 K
|
54
|
-
#
|
55
|
-
# == Conversions
|
56
|
-
#
|
57
|
-
# Temperatures can be converted to diffirent scales.
|
58
|
-
#
|
59
|
-
# Currently, the following scales are supported: <tt>Celsius</tt>, <tt>Fahrenheit</tt>, <tt>Kelvin</tt> and
|
60
|
-
# <tt>Rankine</tt>.
|
61
|
-
#
|
62
|
-
# Temperature[20, :celsius].to_celsius
|
63
|
-
# # => 20 °C
|
64
|
-
#
|
65
|
-
# Temperature[20, :celsius].to_fahrenheit
|
66
|
-
# # => 68 °F
|
67
|
-
#
|
68
|
-
# Temperature[20, :celsius].to_kelvin
|
69
|
-
# # => 293.15 K
|
70
|
-
#
|
71
|
-
# Temperature[20, :celsius].to_rankine
|
72
|
-
# # => 527.67 °R
|
73
|
-
#
|
74
|
-
# If it is necessary to convert scale dynamically, {to_scale}[rdoc-ref:BasicTemperature#to_scale] method is
|
75
|
-
# available.
|
76
|
-
#
|
77
|
-
# Temperature[20, :celsius].to_scale(scale)
|
78
|
-
#
|
79
|
-
# All conversion formulas are taken from
|
80
|
-
# {RapidTables}[https://www.rapidtables.com/convert/temperature/index.html].
|
81
|
-
#
|
82
|
-
# Conversion precision: 2 accurate digits after the decimal dot.
|
83
|
-
#
|
84
|
-
# == Comparison
|
85
|
-
#
|
86
|
-
# Temperature implements idiomatic {<=> spaceship operator}[https://ruby-doc.org/core/Comparable.html] and
|
87
|
-
# mixes in {Comparable}[https://ruby-doc.org/core/Comparable.html] module.
|
88
|
-
#
|
89
|
-
# As a result, all methods from Comparable are available, e.g:
|
90
|
-
#
|
91
|
-
# Temperature[20, :celsius] < Temperature[25, :celsius]
|
92
|
-
# # => true
|
93
|
-
#
|
94
|
-
# Temperature[20, :celsius] <= Temperature[25, :celsius]
|
95
|
-
# # => true
|
96
|
-
#
|
97
|
-
# Temperature[20, :celsius] == Temperature[25, :celsius]
|
98
|
-
# # => false
|
99
|
-
#
|
100
|
-
# Temperature[20, :celsius] > Temperature[25, :celsius]
|
101
|
-
# # => false
|
102
|
-
#
|
103
|
-
# Temperature[20, :celsius] >= Temperature[25, :celsius]
|
104
|
-
# # => false
|
105
|
-
#
|
106
|
-
# Temperature[20, :celsius].between?(Temperature[15, :celsius], Temperature[25, :celsius])
|
107
|
-
# # => true
|
108
|
-
#
|
109
|
-
# # Starting from Ruby 2.4.6
|
110
|
-
# Temperature[20, :celsius].clamp(Temperature[20, :celsius], Temperature[25, :celsius])
|
111
|
-
# # => 20 °C
|
112
|
-
#
|
113
|
-
# Please note, if <tt>other</tt> temperature has a different scale, temperature is automatically converted
|
114
|
-
# to that scale before comparison.
|
115
|
-
#
|
116
|
-
# Temperature[20, :celsius] == Temperature[293.15, :kelvin]
|
117
|
-
# # => true
|
118
|
-
#
|
119
|
-
# IMPORTANT !!!
|
120
|
-
#
|
121
|
-
# <tt>degrees</tt> are rounded to the nearest value with a precision of 2 decimal digits before comparison.
|
122
|
-
#
|
123
|
-
# This means the following temperatures are considered as equal:
|
124
|
-
#
|
125
|
-
# Temperature[20.020, :celsius] == Temperature[20.024, :celsius]
|
126
|
-
# # => true
|
127
|
-
#
|
128
|
-
# Temperature[20.025, :celsius] == Temperature[20.029, :celsius]
|
129
|
-
# # => true
|
130
|
-
#
|
131
|
-
# while these ones are treated as NOT equal:
|
132
|
-
#
|
133
|
-
# Temperature[20.024, :celsius] == Temperature[20.029, :celsius]
|
134
|
-
# # => false
|
135
|
-
#
|
136
|
-
# == Math
|
137
|
-
#
|
138
|
-
# ==== Addition/Subtraction.
|
139
|
-
#
|
140
|
-
# Temperature[20, :celsius] + Temperature[10, :celsius]
|
141
|
-
# # => 30 °C
|
142
|
-
#
|
143
|
-
# Temperature[20, :celsius] - Temperature[10, :celsius]
|
144
|
-
# # => 10 °C
|
145
|
-
#
|
146
|
-
# If second temperature has a different scale, first temperature is automatically converted to that scale
|
147
|
-
# before <tt>degrees</tt> addition/subtraction.
|
148
|
-
#
|
149
|
-
# Temperature[283.15, :kelvin] + Temperature[10, :celsius]
|
150
|
-
# # => 10 °C
|
151
|
-
#
|
152
|
-
# Returned temperature will have the same scale as the second temperature.
|
153
|
-
#
|
154
|
-
# It is possible to add/subtract numerics.
|
155
|
-
#
|
156
|
-
# Temperature[20, :celsius] + 10
|
157
|
-
# # => 30 °C
|
158
|
-
#
|
159
|
-
# Temperature[20, :celsius] - 10
|
160
|
-
# # => 10 °C
|
161
|
-
#
|
162
|
-
# In such cases, returned temperature will have the same scale as the first temperature.
|
163
|
-
#
|
164
|
-
# Also {Ruby coersion mechanism}[https://ruby-doc.org/core/Numeric.html#method-i-coerce] is supported.
|
165
|
-
#
|
166
|
-
# 10 + Temperature[20, :celsius]
|
167
|
-
# # => 30 °C
|
168
|
-
#
|
169
|
-
# 10 - Temperature[20, :celsius]
|
170
|
-
# # => -10 °C
|
171
|
-
#
|
172
|
-
# ==== Negation
|
173
|
-
#
|
174
|
-
# -Temperature[20, :celsius]
|
175
|
-
# # => -20 °C
|
176
|
-
#
|
177
|
-
class BasicTemperature
|
178
|
-
include Comparable
|
179
|
-
|
180
|
-
# Raised when <tt>Temperature.new</tt> is called with mixed positional and keyword arguments or without
|
181
|
-
# arguments at all.
|
182
|
-
class InitializationArgumentsError < StandardError; end
|
183
|
-
|
184
|
-
# Raised when <tt>degrees</tt> is not a Numeric.
|
185
|
-
class InvalidDegreesError < StandardError; end
|
186
|
-
|
187
|
-
# Raised when <tt>scale</tt> can not be casted to any possible scale value.
|
188
|
-
# See {SCALES}[rdoc-ref:BasicTemperature::SCALES].
|
189
|
-
class InvalidScaleError < StandardError; end
|
190
|
-
|
191
|
-
# Raised when <tt>other</tt> is not a Numeric or Temperature in math operations.
|
192
|
-
class InvalidNumericOrTemperatureError < StandardError; end
|
193
|
-
|
194
|
-
CELSIUS = 'celsius'
|
195
|
-
FAHRENHEIT = 'fahrenheit'
|
196
|
-
KELVIN = 'kelvin'
|
197
|
-
RANKINE = 'rankine'
|
198
|
-
|
199
|
-
# A list of all currently supported scale values.
|
200
|
-
SCALES = [CELSIUS, FAHRENHEIT, KELVIN, RANKINE].freeze
|
201
|
-
|
202
|
-
# Degrees of the temperature.
|
203
|
-
attr_reader :degrees
|
204
|
-
|
205
|
-
# Scale of the temperature. Look at {SCALES}[rdoc-ref:BasicTemperature::SCALES] for possible values.
|
206
|
-
attr_reader :scale
|
207
|
-
|
208
|
-
##
|
209
|
-
# Creates a new instance of Temperature. Alias for <tt>new</tt>.
|
210
|
-
#
|
211
|
-
# :call-seq:
|
212
|
-
# [](degrees:, scale:)
|
213
|
-
# [](degrees, scale)
|
214
|
-
#
|
215
|
-
def self.[](*args, **kwargs)
|
216
|
-
new(*args, **kwargs)
|
217
|
-
end
|
218
|
-
|
219
|
-
##
|
220
|
-
# Creates a new instance of Temperature. Is aliased as <tt>[]</tt>.
|
221
|
-
#
|
222
|
-
# :call-seq:
|
223
|
-
# new(degrees:, scale:)
|
224
|
-
# new(degrees, scale)
|
225
|
-
#
|
226
|
-
def initialize(*positional_arguments, **keyword_arguments)
|
227
|
-
assert_either_positional_arguments_or_keyword_arguments!(positional_arguments, keyword_arguments)
|
228
|
-
|
229
|
-
if keyword_arguments.any?
|
230
|
-
initialize_via_keywords_arguments(keyword_arguments)
|
231
|
-
else # positional_arguments.any?
|
232
|
-
initialize_via_positional_arguments(positional_arguments)
|
233
|
-
end
|
234
|
-
end
|
235
|
-
|
236
|
-
# rubocop:disable Naming/AccessorMethodName
|
237
|
-
|
238
|
-
# Returns a new Temperature with updated <tt>degrees</tt>.
|
239
|
-
#
|
240
|
-
# temperature = Temperature[0, :celsius]
|
241
|
-
# # => 0 °C
|
242
|
-
#
|
243
|
-
# new_temperature = temperature.set_degrees(15)
|
244
|
-
# # => 15 °C
|
245
|
-
#
|
246
|
-
def set_degrees(degrees)
|
247
|
-
BasicTemperature.new(degrees, scale)
|
248
|
-
end
|
249
|
-
# rubocop:enable Naming/AccessorMethodName
|
250
|
-
|
251
|
-
# rubocop:disable Naming/AccessorMethodName
|
252
|
-
|
253
|
-
# Returns a new Temperature with updated <tt>scale</tt>.
|
254
|
-
#
|
255
|
-
# temperature = Temperature[0, :celsius]
|
256
|
-
# # => 0 °C
|
257
|
-
#
|
258
|
-
# new_temperature = temperature.set_scale(:kelvin)
|
259
|
-
# # => 0 K
|
260
|
-
#
|
261
|
-
def set_scale(scale)
|
262
|
-
BasicTemperature.new(degrees, scale)
|
263
|
-
end
|
264
|
-
# rubocop:enable Naming/AccessorMethodName
|
265
|
-
|
266
|
-
##
|
267
|
-
# Converts temperature to specific <tt>scale</tt>.
|
268
|
-
# If temperature is already in desired <tt>scale</tt>, returns current temperature object.
|
269
|
-
#
|
270
|
-
# Raises {InvalidScaleError}[rdoc-ref:BasicTemperature::InvalidScaleError]
|
271
|
-
# when <tt>scale</tt> can not be casted to any possible scale value
|
272
|
-
# (see {SCALES}[rdoc-ref:BasicTemperature::SCALES]).
|
273
|
-
#
|
274
|
-
# Temperature[60, :fahrenheit].to_scale(:celsius)
|
275
|
-
# # => 15.56 °C
|
276
|
-
#
|
277
|
-
def to_scale(scale)
|
278
|
-
casted_scale = cast_scale(scale)
|
279
|
-
|
280
|
-
assert_valid_scale!(casted_scale)
|
281
|
-
|
282
|
-
case casted_scale
|
283
|
-
when CELSIUS
|
284
|
-
to_celsius
|
285
|
-
when FAHRENHEIT
|
286
|
-
to_fahrenheit
|
287
|
-
when KELVIN
|
288
|
-
to_kelvin
|
289
|
-
when RANKINE
|
290
|
-
to_rankine
|
291
|
-
end
|
292
|
-
end
|
293
|
-
|
294
|
-
##
|
295
|
-
# Converts temperature to Celsius scale. If temperature is already in Celsius, returns current
|
296
|
-
# temperature object.
|
297
|
-
#
|
298
|
-
# Memoizes subsequent calls.
|
299
|
-
#
|
300
|
-
# Conversion formulas are taken from {RapidTables}[https://www.rapidtables.com/]:
|
301
|
-
# 1. {Celsius to Fahrenheit}[https://www.rapidtables.com/convert/temperature/celsius-to-fahrenheit.html].
|
302
|
-
# 2. {Celsius to Kelvin}[https://www.rapidtables.com/convert/temperature/celsius-to-kelvin.html].
|
303
|
-
# 3. {Celsius to Rankine}[https://www.rapidtables.com/convert/temperature/celsius-to-rankine.html].
|
304
|
-
#
|
305
|
-
# Temperature[0, :fahrenheit].to_celsius
|
306
|
-
# # => -17.78 °C
|
307
|
-
#
|
308
|
-
def to_celsius
|
309
|
-
memoized(:to_celsius) || memoize(:to_celsius, -> {
|
310
|
-
return self if self.scale == CELSIUS
|
311
|
-
|
312
|
-
degrees =
|
313
|
-
case self.scale
|
314
|
-
when FAHRENHEIT
|
315
|
-
(self.degrees - 32) * (5 / 9r)
|
316
|
-
when KELVIN
|
317
|
-
self.degrees - 273.15
|
318
|
-
when RANKINE
|
319
|
-
(self.degrees - 491.67) * (5 / 9r)
|
320
|
-
end
|
321
|
-
|
322
|
-
BasicTemperature.new(degrees, CELSIUS)
|
323
|
-
})
|
324
|
-
end
|
325
|
-
|
326
|
-
##
|
327
|
-
# Converts temperature to Fahrenheit scale. If temperature is already in Fahrenheit, returns current
|
328
|
-
# temperature object.
|
329
|
-
#
|
330
|
-
# Memoizes subsequent calls.
|
331
|
-
#
|
332
|
-
# Conversion formulas are taken from {RapidTables}[https://www.rapidtables.com/]:
|
333
|
-
# 1. {Fahrenheit to Celsius}[https://www.rapidtables.com/convert/temperature/fahrenheit-to-celsius.html].
|
334
|
-
# 2. {Fahrenheit to Kelvin}[https://www.rapidtables.com/convert/temperature/fahrenheit-to-kelvin.html].
|
335
|
-
# 3. {Fahrenheit to Rankine}[https://www.rapidtables.com/convert/temperature/fahrenheit-to-rankine.html].
|
336
|
-
#
|
337
|
-
# Temperature[0, :celsius].to_fahrenheit
|
338
|
-
# # => 32 °F
|
339
|
-
#
|
340
|
-
def to_fahrenheit
|
341
|
-
memoized(:to_fahrenheit) || memoize(:to_fahrenheit, -> {
|
342
|
-
return self if self.scale == FAHRENHEIT
|
343
|
-
|
344
|
-
degrees =
|
345
|
-
case self.scale
|
346
|
-
when CELSIUS
|
347
|
-
self.degrees * (9 / 5r) + 32
|
348
|
-
when KELVIN
|
349
|
-
self.degrees * (9 / 5r) - 459.67
|
350
|
-
when RANKINE
|
351
|
-
self.degrees - 459.67
|
352
|
-
end
|
353
|
-
|
354
|
-
BasicTemperature.new(degrees, FAHRENHEIT)
|
355
|
-
})
|
356
|
-
end
|
357
|
-
|
358
|
-
##
|
359
|
-
# Converts temperature to Kelvin scale. If temperature is already in Kelvin, returns current
|
360
|
-
# temperature object.
|
361
|
-
#
|
362
|
-
# Memoizes subsequent calls.
|
363
|
-
#
|
364
|
-
# Conversion formulas are taken from {RapidTables}[https://www.rapidtables.com/]:
|
365
|
-
# 1. {Kelvin to Celsius}[https://www.rapidtables.com/convert/temperature/kelvin-to-celsius.html].
|
366
|
-
# 2. {Kelvin to Fahrenheit}[https://www.rapidtables.com/convert/temperature/kelvin-to-fahrenheit.html].
|
367
|
-
# 3. {Kelvin to Rankine}[https://www.rapidtables.com/convert/temperature/kelvin-to-rankine.html].
|
368
|
-
#
|
369
|
-
# Temperature[0, :kelvin].to_rankine
|
370
|
-
# # => 0 °R
|
371
|
-
#
|
372
|
-
def to_kelvin
|
373
|
-
memoized(:to_kelvin) || memoize(:to_kelvin, -> {
|
374
|
-
return self if self.scale == KELVIN
|
375
|
-
|
376
|
-
degrees =
|
377
|
-
case self.scale
|
378
|
-
when CELSIUS
|
379
|
-
self.degrees + 273.15
|
380
|
-
when FAHRENHEIT
|
381
|
-
(self.degrees + 459.67) * (5 / 9r)
|
382
|
-
when RANKINE
|
383
|
-
self.degrees * (5 / 9r)
|
384
|
-
end
|
385
|
-
|
386
|
-
BasicTemperature.new(degrees, KELVIN)
|
387
|
-
})
|
388
|
-
end
|
389
|
-
|
390
|
-
##
|
391
|
-
# Converts temperature to Rankine scale. If temperature is already in Rankine, returns current
|
392
|
-
# temperature object.
|
393
|
-
#
|
394
|
-
# Memoizes subsequent calls.
|
395
|
-
#
|
396
|
-
# Conversion formulas are taken from {RapidTables}[https://www.rapidtables.com/]:
|
397
|
-
# 1. {Rankine to Celsius}[https://www.rapidtables.com/convert/temperature/rankine-to-celsius.html].
|
398
|
-
# 2. {Rankine to Fahrenheit}[https://www.rapidtables.com/convert/temperature/rankine-to-fahrenheit.html].
|
399
|
-
# 3. {Rankine to Kelvin}[https://www.rapidtables.com/convert/temperature/rankine-to-kelvin.html].
|
400
|
-
#
|
401
|
-
# Temperature[0, :rankine].to_kelvin
|
402
|
-
# # => 0 K
|
403
|
-
#
|
404
|
-
def to_rankine
|
405
|
-
memoized(:to_rankine) || memoize(:to_rankine, -> {
|
406
|
-
return self if self.scale == RANKINE
|
407
|
-
|
408
|
-
degrees =
|
409
|
-
case self.scale
|
410
|
-
when CELSIUS
|
411
|
-
(self.degrees + 273.15) * (9 / 5r)
|
412
|
-
when FAHRENHEIT
|
413
|
-
self.degrees + 459.67
|
414
|
-
when KELVIN
|
415
|
-
self.degrees * (9 / 5r)
|
416
|
-
end
|
417
|
-
|
418
|
-
BasicTemperature.new(degrees, RANKINE)
|
419
|
-
})
|
420
|
-
end
|
421
|
-
|
422
|
-
##
|
423
|
-
# Compares temperture with <tt>other</tt> temperature.
|
424
|
-
#
|
425
|
-
# Returns <tt>0</tt> if they are considered as equal.
|
426
|
-
#
|
427
|
-
# Two temperatures are considered as equal when they have the same amount of <tt>degrees</tt>.
|
428
|
-
#
|
429
|
-
# Returns <tt>-1</tt> if temperature is lower than <tt>other</tt> temperature.
|
430
|
-
#
|
431
|
-
# Returns <tt>1</tt> if temperature is higher than <tt>other</tt> temperature.
|
432
|
-
#
|
433
|
-
# If <tt>other</tt> temperature has a different scale, temperature is automatically converted to that scale
|
434
|
-
# before <tt>degrees</tt> comparison.
|
435
|
-
#
|
436
|
-
# Temperature[20, :celsius] <=> Temperature[20, :celsius]
|
437
|
-
# # => 0
|
438
|
-
#
|
439
|
-
# Temperature[20, :celsius] <=> Temperature[293.15, :kelvin]
|
440
|
-
# # => 0
|
441
|
-
#
|
442
|
-
# IMPORTANT!!!
|
443
|
-
#
|
444
|
-
# This method rounds <tt>degrees</tt> to the nearest value with a precision of 2 decimal digits.
|
445
|
-
#
|
446
|
-
# This means the following:
|
447
|
-
#
|
448
|
-
# Temperature[20.020, :celsius] <=> Temperature[20.024, :celsius]
|
449
|
-
# # => 0
|
450
|
-
#
|
451
|
-
# Temperature[20.025, :celsius] <=> Temperature[20.029, :celsius]
|
452
|
-
# # => 0
|
453
|
-
#
|
454
|
-
# Temperature[20.024, :celsius] <=> Temperature[20.029, :celsius]
|
455
|
-
# # => -1
|
456
|
-
#
|
457
|
-
def <=>(other)
|
458
|
-
return unless assert_temperature(other)
|
459
|
-
|
460
|
-
compare_degrees(self.to_scale(other.scale).degrees, other.degrees)
|
461
|
-
end
|
462
|
-
|
463
|
-
##
|
464
|
-
# Performs addition. Returns a new Temperature.
|
465
|
-
#
|
466
|
-
# Temperature[20, :celsius] + Temperature[10, :celsius]
|
467
|
-
# # => 30 °C
|
468
|
-
#
|
469
|
-
# If the second temperature has a different scale, the first temperature is automatically converted to that
|
470
|
-
# scale before <tt>degrees</tt> addition.
|
471
|
-
#
|
472
|
-
# Temperature[283.15, :kelvin] + Temperature[20, :celsius]
|
473
|
-
# # => 30 °C
|
474
|
-
#
|
475
|
-
# Returned temperature will have the same scale as the second temperature.
|
476
|
-
#
|
477
|
-
# It is possible to add numerics.
|
478
|
-
#
|
479
|
-
# Temperature[20, :celsius] + 10
|
480
|
-
# # => 30 °C
|
481
|
-
#
|
482
|
-
# In such cases, returned temperature will have the same scale as the first temperature.
|
483
|
-
#
|
484
|
-
# Also {Ruby coersion mechanism}[https://ruby-doc.org/core/Numeric.html#method-i-coerce] is supported.
|
485
|
-
#
|
486
|
-
# 10 + Temperature[20, :celsius]
|
487
|
-
# # => 30 °C
|
488
|
-
#
|
489
|
-
# :call-seq:
|
490
|
-
# +(temperature)
|
491
|
-
# +(numeric)
|
492
|
-
#
|
493
|
-
def +(other)
|
494
|
-
assert_numeric_or_temperature!(other)
|
495
|
-
|
496
|
-
degrees, scale =
|
497
|
-
case other
|
498
|
-
when Numeric
|
499
|
-
[self.degrees + other, self.scale]
|
500
|
-
when BasicTemperature
|
501
|
-
[self.to_scale(other.scale).degrees + other.degrees, other.scale]
|
502
|
-
end
|
503
|
-
|
504
|
-
BasicTemperature.new(degrees, scale)
|
505
|
-
end
|
506
|
-
|
507
|
-
##
|
508
|
-
# Performs subtraction. Returns a new Temperature.
|
509
|
-
#
|
510
|
-
# Temperature[20, :celsius] - Temperature[10, :celsius]
|
511
|
-
# # => 10 °C
|
512
|
-
#
|
513
|
-
# If the second temperature has a different scale, the first temperature is automatically converted to that
|
514
|
-
# scale before <tt>degrees</tt> subtraction.
|
515
|
-
#
|
516
|
-
# Temperature[283.15, :kelvin] + Temperature[10, :celsius]
|
517
|
-
# # => 10 °C
|
518
|
-
#
|
519
|
-
# Returned temperature will have the same scale as the second temperature.
|
520
|
-
#
|
521
|
-
# It is possible to subtract numerics.
|
522
|
-
#
|
523
|
-
# Temperature[20, :celsius] - 10
|
524
|
-
# # => 10 °C
|
525
|
-
#
|
526
|
-
# In such cases, returned temperature will have the same scale as the first temperature.
|
527
|
-
#
|
528
|
-
# Also {Ruby coersion mechanism}[https://ruby-doc.org/core/Numeric.html#method-i-coerce] is supported.
|
529
|
-
#
|
530
|
-
# 10 - Temperature[20, :celsius]
|
531
|
-
# # => -10 °C
|
532
|
-
#
|
533
|
-
# :call-seq:
|
534
|
-
# -(temperature)
|
535
|
-
# -(numeric)
|
536
|
-
#
|
537
|
-
def -(other)
|
538
|
-
self + -other
|
539
|
-
end
|
540
|
-
|
541
|
-
##
|
542
|
-
# Returns a new Temperature with negated <tt>degrees</tt>.
|
543
|
-
#
|
544
|
-
# -Temperature[20, :celsius]
|
545
|
-
# # => -20 °C
|
546
|
-
#
|
547
|
-
def -@
|
548
|
-
BasicTemperature.new(-self.degrees, self.scale)
|
549
|
-
end
|
550
|
-
|
551
|
-
# Is used by {+}[rdoc-ref:BasicTemperature#+] and {-}[rdoc-ref:BasicTemperature#-]
|
552
|
-
# for {Ruby coersion mechanism}[https://ruby-doc.org/core/Numeric.html#method-i-coerce].
|
553
|
-
def coerce(numeric) #:nodoc:
|
554
|
-
assert_numeric!(numeric)
|
555
|
-
|
556
|
-
[BasicTemperature.new(numeric, self.scale), self]
|
557
|
-
end
|
558
|
-
|
559
|
-
# Returns a string containing a human-readable representation of temperature.
|
560
|
-
def inspect #:nodoc:
|
561
|
-
rounded_degrees = round_degrees(degrees)
|
562
|
-
|
563
|
-
printable_degrees = degrees_without_decimal?(rounded_degrees) ? rounded_degrees.to_i : rounded_degrees
|
564
|
-
|
565
|
-
scale_symbol =
|
566
|
-
case self.scale
|
567
|
-
when CELSIUS
|
568
|
-
'°C'
|
569
|
-
when FAHRENHEIT
|
570
|
-
'°F'
|
571
|
-
when KELVIN
|
572
|
-
'K'
|
573
|
-
when RANKINE
|
574
|
-
'°R'
|
575
|
-
end
|
576
|
-
|
577
|
-
"#{printable_degrees} #{scale_symbol}"
|
578
|
-
end
|
579
|
-
|
580
|
-
private
|
581
|
-
|
582
|
-
# Initialization
|
583
|
-
def initialize_via_positional_arguments(positional_arguments)
|
584
|
-
degrees, scale = positional_arguments
|
585
|
-
|
586
|
-
initialize_arguments(degrees, scale)
|
587
|
-
end
|
588
|
-
|
589
|
-
def initialize_via_keywords_arguments(keyword_arguments)
|
590
|
-
degrees, scale = keyword_arguments.values_at(:degrees, :scale)
|
591
|
-
|
592
|
-
initialize_arguments(degrees, scale)
|
593
|
-
end
|
594
|
-
|
595
|
-
def initialize_arguments(degrees, scale)
|
596
|
-
casted_degrees = cast_degrees(degrees)
|
597
|
-
casted_scale = cast_scale(scale)
|
598
|
-
|
599
|
-
assert_valid_degrees!(casted_degrees)
|
600
|
-
assert_valid_scale!(casted_scale)
|
601
|
-
|
602
|
-
@degrees = casted_degrees
|
603
|
-
@scale = casted_scale
|
604
|
-
end
|
605
|
-
|
606
|
-
# Casting
|
607
|
-
def cast_degrees(degrees)
|
608
|
-
Float(degrees) rescue nil
|
609
|
-
end
|
610
|
-
|
611
|
-
def cast_scale(scale)
|
612
|
-
scale.to_s
|
613
|
-
end
|
614
|
-
|
615
|
-
# Assertions
|
616
|
-
def assert_either_positional_arguments_or_keyword_arguments!(positional_arguments, keyword_arguments)
|
617
|
-
raise_initialization_arguments_error if positional_arguments.any? && keyword_arguments.any?
|
618
|
-
raise_initialization_arguments_error if positional_arguments.none? && keyword_arguments.none?
|
619
|
-
end
|
620
|
-
|
621
|
-
def assert_valid_degrees!(degrees)
|
622
|
-
raise_invalid_degrees_error unless degrees.is_a?(Numeric)
|
623
|
-
end
|
624
|
-
|
625
|
-
def assert_valid_scale!(scale)
|
626
|
-
raise_invalid_scale_error unless SCALES.include?(scale)
|
627
|
-
end
|
628
|
-
|
629
|
-
def assert_numeric_or_temperature!(numeric_or_temperature)
|
630
|
-
return if numeric_or_temperature.is_a?(Numeric) || numeric_or_temperature.instance_of?(BasicTemperature)
|
631
|
-
|
632
|
-
raise_invalid_numeric_or_temperature_error(numeric_or_temperature)
|
633
|
-
end
|
634
|
-
|
635
|
-
def assert_numeric!(numeric)
|
636
|
-
raise_invalid_numeric unless numeric.is_a?(Numeric)
|
637
|
-
end
|
638
|
-
|
639
|
-
def assert_temperature(temperature)
|
640
|
-
temperature.instance_of?(BasicTemperature)
|
641
|
-
end
|
642
|
-
|
643
|
-
# Raising errors
|
644
|
-
def raise_initialization_arguments_error
|
645
|
-
message =
|
646
|
-
'Positional and keyword arguments are mixed or ' \
|
647
|
-
'neither positional nor keyword arguments are passed.'
|
648
|
-
|
649
|
-
raise InitializationArgumentsError, message
|
650
|
-
end
|
651
|
-
|
652
|
-
def raise_invalid_degrees_error
|
653
|
-
raise InvalidDegreesError, 'degree is NOT a numeric value.'
|
654
|
-
end
|
655
|
-
|
656
|
-
def raise_invalid_scale_error
|
657
|
-
message =
|
658
|
-
'scale has invalid value, ' \
|
659
|
-
"valid values are #{SCALES.map { |scale| "'#{scale}'" }.join(', ')}."
|
660
|
-
|
661
|
-
raise InvalidScaleError, message
|
662
|
-
end
|
663
|
-
|
664
|
-
def raise_invalid_numeric_or_temperature_error(numeric_or_temperature)
|
665
|
-
raise InvalidNumericOrTemperatureError, "`#{numeric_or_temperature}` is neither Numeric nor Temperature."
|
666
|
-
end
|
667
|
-
|
668
|
-
# Rounding
|
669
|
-
def round_degrees(degrees)
|
670
|
-
degrees.round(2)
|
671
|
-
end
|
672
|
-
|
673
|
-
def compare_degrees(first_degrees, second_degrees)
|
674
|
-
round_degrees(first_degrees) <=> round_degrees(second_degrees)
|
675
|
-
end
|
676
|
-
|
677
|
-
def degrees_with_decimal?(degrees)
|
678
|
-
degrees % 1 != 0
|
679
|
-
end
|
680
|
-
|
681
|
-
def degrees_without_decimal?(degrees)
|
682
|
-
!degrees_with_decimal?(degrees)
|
683
|
-
end
|
684
|
-
|
685
|
-
# Memoization
|
686
|
-
def memoized(key)
|
687
|
-
name = convert_to_variable_name(key)
|
688
|
-
|
689
|
-
instance_variable_get(name) if instance_variable_defined?(name)
|
690
|
-
end
|
691
|
-
|
692
|
-
def memoize(key, proc)
|
693
|
-
name = convert_to_variable_name(key)
|
694
|
-
value = proc.call
|
695
|
-
|
696
|
-
instance_variable_set(name, value)
|
697
|
-
end
|
698
|
-
|
699
|
-
def convert_to_variable_name(key)
|
700
|
-
"@#{key}"
|
701
|
-
end
|
702
|
-
end
|
703
|
-
# rubocop:enable Metrics/ClassLength
|
6
|
+
module BasicTemperature; end
|