chemistry-temperature 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,18 @@
1
+ .rvmrc
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rake'
4
+ gem 'flog'
5
+ gem 'flay'
6
+
7
+ group :test do
8
+ gem 'minitest'
9
+ gem 'minitest-colorize'
10
+ gem 'pry'
11
+ end
12
+
13
+ # Specify your gem's dependencies in chemistry.gemspec
14
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Isaac Sanders
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Chemistry::Temperature
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'chemistry-temperature'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install chemistry-temperature
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,14 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ LIB_FILES = FileList.new('lib/**/*.rb').
4
+ exclude('lib/chemistry-temperature/version.rb',
5
+ 'lib/chemistry/temperature/errors.rb')
6
+ TEST_FILES = LIB_FILES.pathmap('%{^lib,test}d/test_%f')
7
+
8
+ task :test do
9
+ TEST_FILES.zip(LIB_FILES).each do |test_file, lib_file|
10
+ ruby "-I lib -I . -I test -r #{lib_file} #{test_file}"
11
+ end
12
+ end
13
+
14
+ task :default => :test
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'chemistry-temperature/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "chemistry-temperature"
8
+ gem.version = Chemistry::Temperature::VERSION
9
+ gem.authors = ["Isaac Sanders"]
10
+ gem.email = ["isaac@isaacbfsanders.com"]
11
+ gem.description = %q{Work with temperatures in Celsius, Fahrenheit, and Kelvin}
12
+ gem.summary = %q{Work with temperatures in Celsius, Fahrenheit, and Kelvin}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,5 @@
1
+ module Chemistry
2
+ module Temperature
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,22 @@
1
+ require 'chemistry/temperature/parsed_expression'
2
+
3
+ module Chemistry
4
+ module Temperature
5
+ def self.parse(orig_temperature_string)
6
+ expression = Chemistry::Temperature::ParsedExpression.new(orig_temperature_string)
7
+ expression.temperature
8
+ end
9
+
10
+ def self.kelvin(temperature)
11
+ Chemistry::Temperature::Kelvin.new(temperature)
12
+ end
13
+
14
+ def self.celsius(temperature)
15
+ Chemistry::Temperature::Celsius.new(temperature)
16
+ end
17
+
18
+ def self.fahrenheit(temperature)
19
+ Chemistry::Temperature::Fahrenheit.new(temperature)
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,29 @@
1
+ require 'chemistry/temperature/unit'
2
+ require 'chemistry/temperature/kelvin'
3
+ require 'chemistry/temperature/fahrenheit'
4
+
5
+ module Chemistry
6
+ module Temperature
7
+ class Celsius < Chemistry::Temperature::Unit
8
+ def to_celsius
9
+ self
10
+ end
11
+
12
+ def to_kelvin
13
+ Chemistry::Temperature::Kelvin.new convert_to_kelvin
14
+ end
15
+
16
+ def convert_to_kelvin
17
+ @temperature + 273.15
18
+ end
19
+
20
+ def to_fahrenheit
21
+ Chemistry::Temperature::Fahrenheit.new convert_to_fahrenheit
22
+ end
23
+
24
+ def convert_to_fahrenheit
25
+ (@temperature * (9.0/5.0)) + 32.0
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,6 @@
1
+ module Chemistry
2
+ module Temperature
3
+ class Error < ::StandardError; end
4
+ class MalformedInputError < Error; end
5
+ end
6
+ end
@@ -0,0 +1,24 @@
1
+ require 'chemistry/temperature/unit'
2
+ require 'chemistry/temperature/celsius'
3
+
4
+ module Chemistry
5
+ module Temperature
6
+ class Fahrenheit < Chemistry::Temperature::Unit
7
+ def to_fahrenheit
8
+ self
9
+ end
10
+
11
+ def to_kelvin
12
+ to_celsius.to_kelvin
13
+ end
14
+
15
+ def to_celsius
16
+ Chemistry::Temperature::Celsius.new convert_to_celsius
17
+ end
18
+
19
+ def convert_to_celsius
20
+ (@temperature - 32.0) / (9.0/5.0)
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,32 @@
1
+ require 'chemistry/temperature/unit'
2
+ require 'chemistry/temperature/celsius'
3
+
4
+ module Chemistry
5
+ module Temperature
6
+ class Kelvin < Chemistry::Temperature::Unit
7
+ def to_kelvin
8
+ self
9
+ end
10
+
11
+ def to_celsius
12
+ Chemistry::Temperature::Celsius.new convert_to_celsius
13
+ end
14
+
15
+ def to_fahrenheit
16
+ to_celsius.to_fahrenheit
17
+ end
18
+
19
+ def convert_to_celsius
20
+ @temperature - 273.15
21
+ end
22
+
23
+ def ==(other)
24
+ if other.kind_of? Chemistry::Temperature::Kelvin
25
+ to_f == other.to_f
26
+ else
27
+ false
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,60 @@
1
+ require 'chemistry/temperature/fahrenheit'
2
+ require 'chemistry/temperature/errors'
3
+
4
+ module Chemistry
5
+ module Temperature
6
+ class ParsedExpression
7
+ TEMPERATURE_FORMAT = /(-?\d+\.?\d*)([fck])/i
8
+ INVALID_CHARACTERS = /[^\-\d\.kfc]/
9
+ UNIT_LOOKUP = {
10
+ 'f' => Chemistry::Temperature::Fahrenheit,
11
+ 'c' => Chemistry::Temperature::Celsius,
12
+ 'k' => Chemistry::Temperature::Kelvin
13
+ }
14
+
15
+ def initialize(orig_temperature_string)
16
+ @temperature_string = orig_temperature_string.downcase
17
+
18
+ if invalid_expression?
19
+ raise_malformed_input_error
20
+ end
21
+ end
22
+
23
+ def number
24
+ @number ||= Float(tokens[1])
25
+ end
26
+
27
+ def unit
28
+ @unit ||= UNIT_LOOKUP.fetch(tokens[2])
29
+ end
30
+
31
+ def temperature
32
+ @temperature ||= unit.new(number)
33
+ end
34
+
35
+ private
36
+
37
+ attr_reader :temperature_string
38
+
39
+ def invalid_expression?
40
+ raw_tokens.empty?
41
+ end
42
+
43
+ def raw_tokens
44
+ temperature_string.match(TEMPERATURE_FORMAT).to_a
45
+ end
46
+
47
+ def raise_malformed_input_error
48
+ raise MalformedInputError, invalid_format_message
49
+ end
50
+
51
+ def invalid_format_message
52
+ "'#{temperature_string}' has invalid format"
53
+ end
54
+
55
+ def tokens
56
+ @tokens ||= raw_tokens
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,25 @@
1
+ module Chemistry
2
+ module Temperature
3
+ class Unit
4
+ def initialize(temperature)
5
+ @temperature = temperature
6
+ end
7
+
8
+ def to_i
9
+ @temperature.to_i
10
+ end
11
+
12
+ def to_f
13
+ @temperature.to_f
14
+ end
15
+
16
+ def ==(other)
17
+ if other.kind_of? Chemistry::Temperature::Unit
18
+ to_kelvin == other.to_kelvin
19
+ else
20
+ false
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,28 @@
1
+ require 'test_helper'
2
+
3
+ class ToCelsiusTest < MiniTest::Unit::TestCase
4
+ def test_returns_self
5
+ temp = Chemistry::Temperature::Celsius.new(1)
6
+ assert_equal temp, temp.to_celsius
7
+ end
8
+ end
9
+
10
+ class ToKelvinTest < MiniTest::Unit::TestCase
11
+ def test_instance_of_kelvin
12
+ temp = Chemistry::Temperature::Celsius.new(1)
13
+ assert temp.to_kelvin.instance_of? Chemistry::Temperature::Kelvin
14
+ end
15
+
16
+ def test_difference_of_273
17
+ base = 1
18
+ temp = Chemistry::Temperature::Celsius.new(base)
19
+ assert_equal (temp.to_i - temp.to_kelvin.to_i).abs, 273
20
+ end
21
+ end
22
+
23
+ class ToFahrenheitTest < MiniTest::Unit::TestCase
24
+ def test_0C_is_32F
25
+ temp = Chemistry::Temperature::Celsius.new(0)
26
+ assert_equal temp.to_fahrenheit.to_i, 32
27
+ end
28
+ end
@@ -0,0 +1,15 @@
1
+ require 'test_helper'
2
+
3
+ class ToFahrenheitTest < MiniTest::Unit::TestCase
4
+ def test_returns_self
5
+ temp = Chemistry::Temperature::Fahrenheit.new(1)
6
+ assert_equal temp, temp.to_fahrenheit
7
+ end
8
+ end
9
+
10
+ class ToCelsiusTest < MiniTest::Unit::TestCase
11
+ def test_32F_is_0C
12
+ temp = Chemistry::Temperature::Fahrenheit.new(32)
13
+ assert_equal temp.to_celsius.to_i, 0
14
+ end
15
+ end
@@ -0,0 +1,38 @@
1
+ require 'test_helper'
2
+
3
+ class ToKelvinTest < MiniTest::Unit::TestCase
4
+ def test_returns_self
5
+ temp = Chemistry::Temperature::Kelvin.new(1)
6
+ assert_equal temp, temp.to_kelvin
7
+ end
8
+ end
9
+
10
+ class ToCelsiusTest < MiniTest::Unit::TestCase
11
+ def test_instance_of_celsius
12
+ temp = Chemistry::Temperature::Kelvin.new(1)
13
+ assert temp.to_celsius.instance_of? Chemistry::Temperature::Celsius
14
+ end
15
+
16
+ def test_difference_of_273
17
+ base = 1
18
+ temp = Chemistry::Temperature::Kelvin.new(base)
19
+ assert_equal (temp.to_i - temp.to_celsius.to_i).abs, 273
20
+ end
21
+ end
22
+
23
+ class ToFahrenheitTest < MiniTest::Unit::TestCase
24
+ def test_273_15K_is_32F
25
+ temp = Chemistry::Temperature::Kelvin.new(273.15)
26
+ assert_equal temp.to_fahrenheit.to_i, 32
27
+ end
28
+ end
29
+
30
+ class EqualityTest < MiniTest::Unit::TestCase
31
+ def test_0_is_0
32
+ assert_equal Chemistry::Temperature::Kelvin.new(0), Chemistry::Temperature::Kelvin.new(0)
33
+ end
34
+
35
+ def test_0_1_is_not_0
36
+ refute_equal Chemistry::Temperature::Kelvin.new(0.1), Chemistry::Temperature::Kelvin.new(0)
37
+ end
38
+ end
@@ -0,0 +1,37 @@
1
+ require 'test_helper'
2
+
3
+ class InitializeTest < MiniTest::Unit::TestCase
4
+ def test_takes_a_valid_temperature_string
5
+ assert Chemistry::Temperature::ParsedExpression.new '2K'
6
+ end
7
+
8
+ def test_rejects_invalid_strings
9
+ assert_raises Chemistry::Temperature::MalformedInputError do
10
+ Chemistry::Temperature::ParsedExpression.new 'sdffasd'
11
+ end
12
+ end
13
+ end
14
+
15
+ class NumberTest < MiniTest::Unit::TestCase
16
+ def test_returns_float_on_good_input
17
+ expression = Chemistry::Temperature::ParsedExpression.new '1f'
18
+ assert_instance_of Float, expression.number
19
+ end
20
+ end
21
+
22
+ class TemperatureUnitTest < MiniTest::Unit::TestCase
23
+ def test_returns_kelvin_on_kelvin_input
24
+ expression = Chemistry::Temperature::ParsedExpression.new '0K'
25
+ assert_equal expression.unit, Chemistry::Temperature::Kelvin
26
+ end
27
+
28
+ def test_returns_celsius_on_celsius_input
29
+ expression = Chemistry::Temperature::ParsedExpression.new '0C'
30
+ assert_equal expression.unit, Chemistry::Temperature::Celsius
31
+ end
32
+
33
+ def test_returns_fahrenheit_on_fahrenheit_input
34
+ expression = Chemistry::Temperature::ParsedExpression.new '0F'
35
+ assert_equal expression.unit, Chemistry::Temperature::Fahrenheit
36
+ end
37
+ end
@@ -0,0 +1,30 @@
1
+ require 'test_helper'
2
+ require 'chemistry/temperature/fahrenheit'
3
+
4
+ class ToITest < MiniTest::Unit::TestCase
5
+ def test_returns_temperature
6
+ temp = 1
7
+ assert_equal Chemistry::Temperature::Unit.new(temp).to_i, temp.to_i
8
+ end
9
+ end
10
+
11
+ class ToFTest < MiniTest::Unit::TestCase
12
+ def test_returns_temperature
13
+ temp = 1.0
14
+ assert_equal Chemistry::Temperature::Unit.new(temp).to_f, temp.to_f
15
+ end
16
+ end
17
+
18
+ class EqualityTest < MiniTest::Unit::TestCase
19
+ def test_nontemperatures_return_false
20
+ temp = Chemistry::Temperature::Fahrenheit.new(1)
21
+ refute_equal temp, 'a'
22
+ end
23
+
24
+ def test_32F_and_0C_return_true
25
+ f = Chemistry::Temperature::Fahrenheit.new(32)
26
+ c = Chemistry::Temperature::Celsius.new(0)
27
+ assert_equal f, c
28
+ end
29
+ end
30
+
@@ -0,0 +1,106 @@
1
+ require 'test_helper'
2
+
3
+ class TemperateureTestCase < MiniTest::Unit::TestCase
4
+ private
5
+
6
+ def assert_parsed_temperature_equals(temperature_expression, temperature)
7
+ assert_equal Chemistry::Temperature.parse(temperature_expression),
8
+ temperature
9
+ end
10
+
11
+ def assert_finds_malformed_input(&block)
12
+ assert_raises Chemistry::Temperature::MalformedInputError, &block
13
+ end
14
+ end
15
+
16
+ class KelvinTest < MiniTest::Unit::TestCase
17
+ def test_returns_kelvin_wrapper
18
+ assert_instance_of Chemistry::Temperature::Kelvin,
19
+ Chemistry::Temperature.kelvin(1)
20
+ end
21
+ end
22
+
23
+ class FahrenheitTest < MiniTest::Unit::TestCase
24
+ def test_returns_fahrenheit_wrapper
25
+ assert_instance_of Chemistry::Temperature::Fahrenheit,
26
+ Chemistry::Temperature.fahrenheit(1)
27
+ end
28
+ end
29
+
30
+ class CelsiusTest < MiniTest::Unit::TestCase
31
+ def test_returns_celsius_wrapper
32
+ assert_instance_of Chemistry::Temperature::Celsius,
33
+ Chemistry::Temperature.celsius(1)
34
+ end
35
+ end
36
+
37
+ class ParseTest < TemperateureTestCase
38
+ def test_F_raises_malformed_input_error
39
+ assert_finds_malformed_input do
40
+ Chemistry::Temperature.parse('f')
41
+ end
42
+ end
43
+
44
+ def test_30_raises_malformed_input_error
45
+ assert_finds_malformed_input do
46
+ Chemistry::Temperature.parse('30')
47
+ end
48
+ end
49
+
50
+ def test_1oof_raises_malformed_input_error
51
+ assert_finds_malformed_input do
52
+ Chemistry::Temperature.parse('1oof')
53
+ end
54
+ end
55
+
56
+ def test_dsfjklsdflkjsdflkj_raises_malformed_input_error
57
+ assert_finds_malformed_input do
58
+ Chemistry::Temperature.parse('dsfjklsdflkjsdflkj')
59
+ end
60
+ end
61
+
62
+ def test_parses_neg_32F
63
+ assert_parsed_temperature_equals '-32F',
64
+ Chemistry::Temperature.fahrenheit(-32)
65
+ end
66
+
67
+ def test_parses_neg_53_2F
68
+ assert_parsed_temperature_equals '-53.2F',
69
+ Chemistry::Temperature.fahrenheit(-53.2)
70
+ end
71
+
72
+ def test_parses_0F
73
+ assert_parsed_temperature_equals '0F',
74
+ Chemistry::Temperature.fahrenheit(0)
75
+ end
76
+
77
+ def test_parses_32F
78
+ assert_parsed_temperature_equals '32F',
79
+ Chemistry::Temperature.fahrenheit(32)
80
+ end
81
+
82
+ def test_parses_43_1F
83
+ assert_parsed_temperature_equals '43.1F',
84
+ Chemistry::Temperature.fahrenheit(43.1)
85
+ end
86
+
87
+ def test_parses_0C
88
+ assert_parsed_temperature_equals '0C',
89
+ Chemistry::Temperature.celsius(0)
90
+ end
91
+
92
+ def test_parses_32C
93
+ assert_parsed_temperature_equals '32C',
94
+ Chemistry::Temperature.celsius(32)
95
+ end
96
+
97
+ def test_parses_43_1C
98
+ assert_parsed_temperature_equals '43.1C',
99
+ Chemistry::Temperature.celsius(43.1)
100
+ end
101
+
102
+ def test_parses_0K
103
+ assert_parsed_temperature_equals '0K',
104
+ Chemistry::Temperature.kelvin(0)
105
+ end
106
+ end
@@ -0,0 +1,8 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/colorize'
3
+
4
+ class ElementTestCase < MiniTest::Unit::TestCase
5
+ def element
6
+ @element ||= Class.new(Chemistry::Element)
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chemistry-temperature
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Isaac Sanders
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-08-12 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Work with temperatures in Celsius, Fahrenheit, and Kelvin
15
+ email:
16
+ - isaac@isaacbfsanders.com
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - .gitignore
22
+ - Gemfile
23
+ - LICENSE.txt
24
+ - README.md
25
+ - Rakefile
26
+ - chemistry-temperature.gemspec
27
+ - lib/chemistry-temperature/version.rb
28
+ - lib/chemistry/temperature.rb
29
+ - lib/chemistry/temperature/celsius.rb
30
+ - lib/chemistry/temperature/errors.rb
31
+ - lib/chemistry/temperature/fahrenheit.rb
32
+ - lib/chemistry/temperature/kelvin.rb
33
+ - lib/chemistry/temperature/parsed_expression.rb
34
+ - lib/chemistry/temperature/unit.rb
35
+ - test/chemistry/temperature/test_celsius.rb
36
+ - test/chemistry/temperature/test_fahrenheit.rb
37
+ - test/chemistry/temperature/test_kelvin.rb
38
+ - test/chemistry/temperature/test_parsed_expression.rb
39
+ - test/chemistry/temperature/test_unit.rb
40
+ - test/chemistry/test_temperature.rb
41
+ - test/test_helper.rb
42
+ homepage: ''
43
+ licenses: []
44
+ post_install_message:
45
+ rdoc_options: []
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ segments:
55
+ - 0
56
+ hash: -218002032132591410
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ segments:
64
+ - 0
65
+ hash: -218002032132591410
66
+ requirements: []
67
+ rubyforge_project:
68
+ rubygems_version: 1.8.24
69
+ signing_key:
70
+ specification_version: 3
71
+ summary: Work with temperatures in Celsius, Fahrenheit, and Kelvin
72
+ test_files:
73
+ - test/chemistry/temperature/test_celsius.rb
74
+ - test/chemistry/temperature/test_fahrenheit.rb
75
+ - test/chemistry/temperature/test_kelvin.rb
76
+ - test/chemistry/temperature/test_parsed_expression.rb
77
+ - test/chemistry/temperature/test_unit.rb
78
+ - test/chemistry/test_temperature.rb
79
+ - test/test_helper.rb