basic_temperature 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: abc9f2d631494075604ae36162102c4aff070974a2bd391a198ac7f95cd4e23c
4
+ data.tar.gz: 22459a3590a5af282596f6d4bb819a96e6442b6ebfd739274de9e02221c2d96d
5
+ SHA512:
6
+ metadata.gz: f1a4b929508305ce833ae0ad9bda5fc4e4b1c539fa20041a2914e7f8e2e3d6239e9ab28daa90ff9c9bf7caa1f6d66a7a7b666b8c65280b397fa46ebeaff9afef
7
+ data.tar.gz: 642cf6da0c1acf3b375a6cf02c4cad7251a67f08c31762dead3d75c91ee014ae8c9d31d7d4a21d54c23151247c940986ae69c657c3fa432257eceb08e791e91e
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.0
6
+ before_install: gem install bundler -v 2.1.2
@@ -0,0 +1,2 @@
1
+ ## 0.1.0
2
+ - Initial Release.
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in basic_temperature.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,34 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ basic_temperature (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (12.3.3)
11
+ rspec (3.9.0)
12
+ rspec-core (~> 3.9.0)
13
+ rspec-expectations (~> 3.9.0)
14
+ rspec-mocks (~> 3.9.0)
15
+ rspec-core (3.9.1)
16
+ rspec-support (~> 3.9.1)
17
+ rspec-expectations (3.9.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.9.0)
20
+ rspec-mocks (3.9.1)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.9.0)
23
+ rspec-support (3.9.2)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ basic_temperature!
30
+ rake (~> 12.0)
31
+ rspec (~> 3.0)
32
+
33
+ BUNDLED WITH
34
+ 2.1.2
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Marian Kostyk
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,40 @@
1
+ # BasicTemperature
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/basic_temperature`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'basic_temperature'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install basic_temperature
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/basic_temperature.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,26 @@
1
+ require_relative 'lib/basic_temperature/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "basic_temperature"
5
+ spec.version = BasicTemperature::VERSION
6
+ spec.authors = ["Marian Kostyk"]
7
+ spec.email = ["mariankostyk13895@gmail.com"]
8
+
9
+ spec.summary = %q{Value object for basic temperature operations.}
10
+ spec.description = %q{Value object for basic temperature operations like conversions from Celcius to Fahrenhait or Kelvin etc.}
11
+ spec.homepage = "https://github.com/marian13/basic_temperature"
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.0.0")
14
+
15
+ spec.metadata["homepage_uri"] = spec.homepage
16
+ spec.metadata["source_code_uri"] = spec.homepage
17
+ spec.metadata["changelog_uri"] = "#{spec.homepage}/CHANGELOG.md"
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
23
+ end
24
+
25
+ spec.require_paths = ["lib"]
26
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "basic_temperature"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,214 @@
1
+ require 'basic_temperature/version'
2
+
3
+ class BasicTemperature
4
+ class InitializationArgumentsError < StandardError; end
5
+ class InvalidDegreesError < StandardError; end
6
+ class InvalidScaleError < StandardError; end
7
+ class InvalidAddendError < StandardError; end
8
+ class CoersionError < StandardError; end
9
+
10
+ SCALES =
11
+ [
12
+ CELCIUS = 'celcius',
13
+ FAHRENHEIT = 'fahrenheit',
14
+ KELVIN = 'kelvin'
15
+ ]
16
+ .map(&:freeze)
17
+ .freeze
18
+
19
+ attr_reader :degrees, :scale
20
+
21
+ def initialize(*positional_arguments, **keyword_arguments)
22
+ raise_initialization_arguments_error if positional_arguments.any? && keyword_arguments.any?
23
+ raise_initialization_arguments_error if positional_arguments.none? && keyword_arguments.none?
24
+
25
+ if keyword_arguments.any?
26
+ initialize_via_keywords_arguments(keyword_arguments)
27
+ elsif positional_arguments.any?
28
+ initialize_via_positional_arguments(positional_arguments)
29
+ end
30
+ end
31
+
32
+ def set_degrees(degrees)
33
+ BasicTemperature.new(degrees, scale)
34
+ end
35
+
36
+ def set_scale(scale)
37
+ self.to_scale(scale)
38
+ end
39
+
40
+ def to_scale(scale)
41
+ case cast_scale(scale)
42
+ when CELCIUS
43
+ to_celsius
44
+ when FAHRENHEIT
45
+ to_fahrenheit
46
+ when KELVIN
47
+ to_kelvin
48
+ else
49
+ raise_invalid_scale_error
50
+ end
51
+ end
52
+
53
+ def to_celsius
54
+ return @to_celsius unless @to_celsius.nil?
55
+
56
+ return @to_celsius = self if self.scale == CELCIUS
57
+
58
+ degrees =
59
+ case self.scale
60
+ when FAHRENHEIT
61
+ (self.degrees - 32) * (5 / 9r)
62
+ when KELVIN
63
+ self.degrees - 273.15
64
+ end
65
+
66
+ @to_celsius = BasicTemperature.new(degrees, CELCIUS)
67
+ end
68
+
69
+ def to_fahrenheit
70
+ return @to_fahrenheit unless @to_fahrenheit.nil?
71
+
72
+ return @to_fahrenheit = self if self.scale == FAHRENHEIT
73
+
74
+ degrees =
75
+ case self.scale
76
+ when CELCIUS
77
+ self.degrees * (9 / 5r) + 32
78
+ when KELVIN
79
+ self.degrees * (9 / 5r) - 459.67
80
+ end
81
+
82
+ @to_fahrenheit = BasicTemperature.new(degrees, FAHRENHEIT)
83
+ end
84
+
85
+ def to_kelvin
86
+ return @to_kelvin unless @to_kelvin.nil?
87
+
88
+ return @to_kelvin = self if self.scale == KELVIN
89
+
90
+ degrees =
91
+ case self.scale
92
+ when CELCIUS
93
+ self.degrees + 273.15
94
+ when FAHRENHEIT
95
+ (self.degrees + 459.67) * (5 / 9r)
96
+ end
97
+
98
+ @to_kelvin = BasicTemperature.new(degrees, KELVIN)
99
+ end
100
+
101
+ def ==(other_temperature)
102
+ return false unless other_temperature.instance_of?(BasicTemperature)
103
+
104
+ self.degrees == other_temperature.degrees && self.scale == other_temperature.scale
105
+ end
106
+
107
+ def +(addend)
108
+ degrees, scale =
109
+ case addend
110
+ when Numeric
111
+ [self.degrees + addend, self.scale]
112
+ when BasicTemperature
113
+ [self.to_scale(addend.scale).degrees + addend.degrees, addend.scale]
114
+ else
115
+ raise_invalid_addend_error(addend)
116
+ end
117
+
118
+ BasicTemperature.new(degrees, scale)
119
+ end
120
+
121
+ def -(subtrahend)
122
+ self + (-subtrahend)
123
+ end
124
+
125
+ def -@
126
+ BasicTemperature.new(-self.degrees, self.scale)
127
+ end
128
+
129
+ def coerce(numeric)
130
+ assert_numeric!(numeric)
131
+
132
+ [BasicTemperature.new(numeric, self.scale), self]
133
+ end
134
+
135
+ def inspect
136
+ "#{degrees.to_i} #{scale.capitalize}"
137
+ end
138
+
139
+ private
140
+
141
+ def initialize_via_positional_arguments(positional_arguments)
142
+ degrees, scale = positional_arguments
143
+
144
+ initialize_arguments(degrees, scale)
145
+ end
146
+
147
+ def initialize_via_keywords_arguments(keyword_arguments)
148
+ degrees, scale = keyword_arguments.values_at(:degrees, :scale)
149
+
150
+ initialize_arguments(degrees, scale)
151
+ end
152
+
153
+ def initialize_arguments(degrees, scale)
154
+ casted_scale = cast_scale(scale)
155
+
156
+ assert_valid_degrees!(degrees)
157
+ assert_valid_scale!(casted_scale)
158
+
159
+ @degrees = degrees
160
+ @scale = casted_scale
161
+ end
162
+
163
+ def cast_scale(scale)
164
+ scale.to_s
165
+ end
166
+
167
+ def assert_valid_degrees!(degrees)
168
+ raise_invalid_degrees_error unless degrees.is_a?(Numeric)
169
+ end
170
+
171
+ def assert_valid_scale!(scale)
172
+ raise_invalid_scale_error unless SCALES.include?(scale)
173
+ end
174
+
175
+ def assert_numeric_or_temperature!(numeric_or_temperature)
176
+ if numeric_or_temperature.is_a?(Numeric) || numeric_or_temperature.instance_of?(BasicTemperature)
177
+ return
178
+ end
179
+
180
+ raise_coersion_error(numeric_or_temperature)
181
+ end
182
+
183
+ def assert_numeric!(numeric)
184
+ raise_invalid_numeric unless numeric.is_a?(Numeric)
185
+ end
186
+
187
+ def raise_initialization_arguments_error
188
+ message =
189
+ 'Positional and keyword arguments are mixed or ' \
190
+ 'neither positional nor keyword arguments are passed.'
191
+
192
+ raise InitializationArgumentsError, message
193
+ end
194
+
195
+ def raise_invalid_degrees_error
196
+ raise InvalidDegreesError, 'degree is NOT a numeric value.'
197
+ end
198
+
199
+ def raise_invalid_scale_error
200
+ message =
201
+ 'scale has invalid value, ' \
202
+ "valid values are #{SCALES.map { |scale| "'#{scale}'"}.join(', ')}."
203
+
204
+ raise InvalidScaleError, message
205
+ end
206
+
207
+ def raise_invalid_addend_error(addend)
208
+ raise InvalidAddendError, "`#{addend}` is neither Numeric nor Temperature."
209
+ end
210
+
211
+ def raise_coersion_error(object)
212
+ raise CoersionError, "#{object} is neither Numeric nor Temperature."
213
+ end
214
+ end
@@ -0,0 +1,3 @@
1
+ class BasicTemperature
2
+ VERSION = '0.1.0'.freeze
3
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: basic_temperature
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Marian Kostyk
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-02-14 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Value object for basic temperature operations like conversions from Celcius
14
+ to Fahrenhait or Kelvin etc.
15
+ email:
16
+ - mariankostyk13895@gmail.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - ".gitignore"
22
+ - ".rspec"
23
+ - ".travis.yml"
24
+ - CHANGELOG.md
25
+ - Gemfile
26
+ - Gemfile.lock
27
+ - LICENSE.txt
28
+ - README.md
29
+ - Rakefile
30
+ - basic_temperature.gemspec
31
+ - bin/console
32
+ - bin/setup
33
+ - lib/basic_temperature.rb
34
+ - lib/basic_temperature/version.rb
35
+ homepage: https://github.com/marian13/basic_temperature
36
+ licenses:
37
+ - MIT
38
+ metadata:
39
+ homepage_uri: https://github.com/marian13/basic_temperature
40
+ source_code_uri: https://github.com/marian13/basic_temperature
41
+ changelog_uri: https://github.com/marian13/basic_temperature/CHANGELOG.md
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 2.0.0
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubygems_version: 3.1.2
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: Value object for basic temperature operations.
61
+ test_files: []