weight_conversion 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 07d41f7525499b367a6dda635dc27df551dddc05
4
+ data.tar.gz: 7421c4f2c701424f9c308153fededad941f35572
5
+ SHA512:
6
+ metadata.gz: 5c2c723ff471778e0c641c701115653c373d073373a97ebd4b76942ea46a952a895a2d4b4a3ab383d4f58dc17dc93b1fb17b38499ccdc780e05d832be759f67c
7
+ data.tar.gz: 385a9dffcdb100adf87a635e8da3fe16767860aa41ba6c8b6a88c4b382ca86a567c51669e5a5cb4d53c01972e826aa2fa8177cb2c3f934c7710c06d0903a4371
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - "1.9.2"
4
+ - "1.9.3"
5
+ - "2.1.5"
6
+ - "2.2.3"
7
+ - rbx
8
+
9
+ # uncomment this line if your project needs to run something other than `rake`:
10
+ script: bundle exec rspec spec
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in weight-conversion.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 TODO: Write your name
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.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # WeightConversion (0.1.0)
2
+
3
+ * [![Code Climate](https://codeclimate.com/github/pzupan/weight_conversion.png)](https://codeclimate.com/github/pzupan/weight_conversion)
4
+ * [![Build Status](https://api.travis-ci.org/pzupan/weight_conversion.png)](https://travis-ci.org/pzupan/weight_conversion)
5
+ * [![GitHub version](https://badge.fury.io/gh/pzupan%2Fweight_conversion.svg)](https://badge.fury.io/gh/pzupan%2Fweight_conversion)
6
+
7
+ Weight object for converting from grams, ounces, pounds and kilograms into a common unit.
8
+
9
+ Based on Anton Shemerey's Weight gem (https://github.com/shemerey/weight)
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ $ gem 'weight_conversion'
16
+
17
+ And then execute:
18
+
19
+ $ bundle install
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install weight_conversion
24
+
25
+ ## Usage
26
+
27
+ ### Basic Conversion
28
+
29
+ ```ruby
30
+ Weight.new(1, :lb).to_kgs # => 0.4536
31
+ Weight.new(1, :kg).to_mgs # => 1000.0
32
+ ```
33
+
34
+ ### Basic Math with Weight objects
35
+
36
+ ```ruby
37
+ Weight.new(1, :kg) + Weight.new(1, :kg) == Weight.new(2, :kg)
38
+ Weight.new(1, :kg) - Weight.new(1, :kg) == Weight.new(0, :kg)
39
+
40
+ Weight.new(1, :kg) * 2 == Weight.new(2, :kg)
41
+ Weight.new(2, :kg) / 2 == Weight.new(1, :kg)
42
+ ```
43
+
44
+ #### Convert result to the first object unit system
45
+
46
+ ```ruby
47
+ Weight.new(1, :kg) + Weight.new(1, :lb) # => #<Weight: @input_value=1.4536, @input_unit=:kg>
48
+ Weight.new(1, :lb) + Weight.new(1, :kg) # => #<Weight: @input_value=3.2046, @input_unit=:lb>
49
+ ```
50
+
51
+ ### Basic comparison with Weight objects
52
+
53
+ ```ruby
54
+ Weight.new(3, :lb).between?(Weight.new(1, :kg), Weight.new(2, :kg))
55
+ Weight.new(1, :kg) > Weight.new(2, :lb)
56
+ Weight.new(1, :lb) <= Weight.new(0.5, :kg)
57
+ ```
58
+
59
+ ## Contributing
60
+
61
+ 1. Fork it ( https://github.com/[my-github-username]/weight_conversion/fork )
62
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
63
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
64
+ 4. Push to the branch (`git push origin my-new-feature`)
65
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,138 @@
1
+ require "weight_conversion/version"
2
+
3
+ class Weight
4
+
5
+ include Comparable
6
+
7
+ def initialize(value=0.0, unit='lb')
8
+ self.value = value
9
+ self.unit = unit
10
+ end
11
+
12
+ def value
13
+ @input_value
14
+ end
15
+
16
+ def unit
17
+ @input_unit
18
+ end
19
+
20
+ def to_gms
21
+ data_in_grams.round(4)
22
+ end
23
+
24
+ def to_ozs
25
+ (data_in_grams / grams_per_ounce).round(4)
26
+ end
27
+
28
+ def to_lbs
29
+ (data_in_grams / grams_per_pound).round(4)
30
+ end
31
+
32
+ def to_kgs
33
+ (data_in_grams / grams_per_kilogram).round(4)
34
+ end
35
+
36
+ def to_i
37
+ value.round
38
+ end
39
+
40
+ def to_f
41
+ value.round(4).to_f
42
+ end
43
+
44
+ def +(other)
45
+ return if is_not_weight?(other)
46
+ self.class.new(value + other_value(other), unit)
47
+ end
48
+
49
+ def -(other)
50
+ return if is_not_weight?(other)
51
+ self.class.new(value - other_value(other), unit)
52
+ end
53
+
54
+ def <=>(other)
55
+ return if is_not_weight?(other)
56
+ self.to_gms <=> other.to_gms
57
+ end
58
+
59
+ def ==(other)
60
+ return if is_not_weight?(other)
61
+ self.to_gms == other.to_gms
62
+ end
63
+
64
+ def *(other)
65
+ raise TypeError, 'You can only multiply by a number' unless other.is_a?(Numeric)
66
+ self.class.new(value * other, unit)
67
+ end
68
+
69
+ def /(other)
70
+ raise TypeError, 'You can only divide by a number' unless other.is_a?(Numeric)
71
+ self.class.new(value / other, unit)
72
+ end
73
+
74
+ private
75
+
76
+ def value=(value)
77
+ raise TypeError, 'Value cannot be negative' if value < 0
78
+ raise TypeError, 'Value must be Numeric' unless value.is_a? Numeric
79
+ @input_value = value
80
+ end
81
+
82
+ def unit=(unit)
83
+ unit = unit.to_s.downcase.to_sym
84
+ raise ArgumentError, "Allowed unit types #{allowed_units.inspect}" unless allowed_units.include?(unit)
85
+ @input_unit = unit
86
+ end
87
+
88
+ def allowed_units
89
+ [:gm, :oz, :lb, :kg]
90
+ end
91
+
92
+ def other_value(other)
93
+ case unit
94
+ when :gm
95
+ other.to_gms
96
+ when :oz
97
+ other.to_ozs
98
+ when :lb
99
+ other.to_lbs
100
+ when :kg
101
+ other.to_kgs
102
+ end
103
+ end
104
+
105
+ def data_in_grams
106
+ case unit
107
+ when :gm
108
+ value
109
+ when :oz
110
+ value * grams_per_ounce
111
+ when :lb
112
+ value * grams_per_pound
113
+ when :kg
114
+ value * grams_per_kilogram
115
+ else
116
+ raise TypeError, 'Unit is not valid.'
117
+ end
118
+ end
119
+
120
+ def grams_per_ounce
121
+ 28.34952
122
+ end
123
+
124
+ def grams_per_pound
125
+ 453.5923
126
+ end
127
+
128
+ def grams_per_kilogram
129
+ 1000
130
+ end
131
+
132
+ def is_not_weight?(other)
133
+ return false if other.is_a?(Weight)
134
+ raise TypeError, 'Not a Weight class.'
135
+ return true
136
+ end
137
+
138
+ end
@@ -0,0 +1,3 @@
1
+ module WeightConversion
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'weight_conversion'
4
+ # require 'coveralls'
5
+
6
+ # Coveralls.wear!
7
+
8
+ RSpec.configure do |config|
9
+ # Optional configs
10
+ end
@@ -0,0 +1,151 @@
1
+ require 'spec_helper'
2
+
3
+ describe Weight do
4
+
5
+ let(:one_kg) { Weight.new(1, :kg) }
6
+ let(:one_lb) { Weight.new(1, :lb) }
7
+ let(:one_oz) { Weight.new(1, :oz) }
8
+ let(:one_gm) { Weight.new(1, :gm) }
9
+
10
+ it 'should take two options value and unit type' do
11
+ Weight.new(1, :lb).should be_instance_of(Weight)
12
+ end
13
+
14
+ describe 'math operators' do
15
+ it 'for minus operator result unit should be equal to first object unit' do
16
+ (one_kg - one_lb).unit.should == :kg
17
+ end
18
+
19
+ it 'for plus operator result unit should be equal to first object unit' do
20
+ (one_kg + one_lb).unit.should == :kg
21
+ end
22
+
23
+ it 'minus operator should compute weight objects with different units properly' do
24
+ (one_kg - one_lb).should > one_lb
25
+ (one_lb - one_oz).should < one_lb
26
+ end
27
+
28
+ it 'plua operator should compute weight objects with different units properly' do
29
+ (one_gm + one_oz).should < one_lb
30
+ (one_oz + one_lb).should > one_lb
31
+ end
32
+
33
+ it 'plus operator should have proper result' do
34
+ (one_lb + one_lb).should == Weight.new(2, :lb)
35
+ end
36
+
37
+ it 'two objects with the same weight and type should be equal' do
38
+ one_kg.should == Weight.new(1, :kg)
39
+ end
40
+
41
+ it 'operation resulting in zero is okay' do
42
+ (one_lb - one_lb).should == Weight.new(0, :lb)
43
+ end
44
+
45
+ it 'times operator should have proper result' do
46
+ (one_lb * 2).should == Weight.new(2, :lb)
47
+ end
48
+
49
+ it 'operation on something that is not a Weight should raise an error' do
50
+ expect do
51
+ (one_lb + 1)
52
+ end.to raise_error(TypeError)
53
+ end
54
+
55
+ it 'attempting to multiply a weight by another weight should raise an error' do
56
+ expect do
57
+ (one_lb * one_lb)
58
+ end.to raise_error(TypeError)
59
+ end
60
+
61
+ it 'multiplication should have proper result' do
62
+ (one_kg * 2).should == Weight.new(2, :kg)
63
+ end
64
+
65
+ it 'division should have proper result' do
66
+ (Weight.new(2, :kg) / 2).should == one_kg
67
+ end
68
+
69
+ it 'attempting to divide a weight by another weight should raise an error' do
70
+ expect do
71
+ (one_lb / one_lb)
72
+ end.to raise_error(TypeError)
73
+ end
74
+ end
75
+
76
+ describe 'calculation between different units' do
77
+ it 'should convert 1 kg to 2.2046 lbs' do
78
+ Weight.new(1, :kg).to_lbs.should == 2.2046
79
+ end
80
+
81
+ it 'should convert 1 lbs to 0.45359 kg' do
82
+ Weight.new(1, :lb).to_kgs.should == 0.4536
83
+ end
84
+
85
+ it 'should convert 1 kg to 35.2740 ounces' do
86
+ Weight.new(1, :kg).to_ozs.should == 35.2740
87
+ end
88
+
89
+ it 'should convert 1 kg to 1000 grams' do
90
+ Weight.new(1, :kg).to_gms.should == 1000
91
+ end
92
+
93
+ it 'should convert 1000 gms to 1 kg' do
94
+ Weight.new(1000, :gm).to_kgs.should == 1
95
+ end
96
+
97
+ it 'should convert 28.3495 gms to 1 oz' do
98
+ Weight.new(28.3495, :gm).to_ozs.should == 1
99
+ end
100
+
101
+ it 'should convert 453.5923 gms to 1 lb' do
102
+ Weight.new(453.5923, :gm).to_lbs.should == 1
103
+ end
104
+
105
+ it 'should convert 0 kgs to 0 lb' do
106
+ Weight.new(0, 'kg').to_lbs.should == 0.0
107
+ end
108
+
109
+ it 'should raise an error when the weight is negative' do
110
+ expect do
111
+ Weight.new(-1, 'kg')
112
+ end.to raise_error(TypeError)
113
+ end
114
+
115
+ it 'should return the weight in pounds when converted to_f' do
116
+ Weight.new(0.6, 'lb').to_f.should == 0.6
117
+ end
118
+
119
+ it 'should round 0.6 lbs to 1 lb when converted to_i' do
120
+ Weight.new(0.6, 'lb').to_i.should == 1
121
+ end
122
+
123
+ it 'should round 0.5 lbs to 1 lb when converted to_i' do
124
+ Weight.new(0.5, 'lb').to_i.should == 1
125
+ end
126
+
127
+ it 'should round 0.4 lbs to 0 lb when converted to_i' do
128
+ Weight.new(0.4, 'lb').to_i.should == 0
129
+ end
130
+ end
131
+
132
+ describe 'comparison of different weights' do
133
+ it 'two pounds should be more than one' do
134
+ Weight.new(2, :lb).should be > one_lb
135
+ end
136
+
137
+ it 'two pounds should be greater than or equal to one plus one' do
138
+ Weight.new(2, :lb) >= one_lb + one_lb
139
+ end
140
+
141
+ it 'one pound should be less than two' do
142
+ one_lb.should be < Weight.new(2, :lb)
143
+ end
144
+
145
+ it 'attempting to compare a weight to something that is not a weight should raise an error' do
146
+ expect do
147
+ (Weight.new(1, :lb) == 1)
148
+ end.to raise_error(TypeError)
149
+ end
150
+ end
151
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'weight_conversion/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "weight_conversion"
8
+ spec.version = WeightConversion::VERSION
9
+ spec.authors = ["Paul Zupan"]
10
+ spec.email = ["pzupan@gmail.com"]
11
+ spec.summary = %q{Gem to convert measures of weight.}
12
+ spec.description = %q{}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.4"
24
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: weight_conversion
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Paul Zupan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-11-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.4'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.4'
55
+ description: ''
56
+ email:
57
+ - pzupan@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/weight_conversion.rb
70
+ - lib/weight_conversion/version.rb
71
+ - spec/spec_helper.rb
72
+ - spec/weight_conversion_spec.rb
73
+ - weight_conversion.gemspec
74
+ homepage: ''
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.4.3
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Gem to convert measures of weight.
98
+ test_files:
99
+ - spec/spec_helper.rb
100
+ - spec/weight_conversion_spec.rb