weight 0.0.2 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.travis.yml +0 -1
- data/README.md +13 -1
- data/lib/weight.rb +12 -2
- data/lib/weight/version.rb +1 -1
- data/spec/weight_spec.rb +81 -52
- metadata +75 -92
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f1b7de322bec0968f875b996916a86835aa776d7
|
4
|
+
data.tar.gz: 6556f54901f914c40561ef6dd608d52dca2be463
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: eba77a4c2438374d5de29150cfb2592e56718a0ff54c385d99546c7d63592197a596bb681002980836b55c0a52757e8050f4d9cd504c390e345ccd152b90b2d1
|
7
|
+
data.tar.gz: 4563f7e5b8caec2d3f8a912df6842f2729ae70408f5b209dc8849a3e1592668d00fa57d10ca21e5780e87eb30da8e9a2b0c87ed94bd57273d2aef5b983655c43
|
data/.travis.yml
CHANGED
data/README.md
CHANGED
@@ -1,7 +1,8 @@
|
|
1
|
-
# Weight (0.0
|
1
|
+
# Weight (1.0.0 version)
|
2
2
|
|
3
3
|
* [![Code Climate](https://codeclimate.com/github/shemerey/weight.png)](https://codeclimate.com/github/shemerey/weight)
|
4
4
|
* [![Build Status](https://api.travis-ci.org/shemerey/weight.png)](https://travis-ci.org/shemerey/weight)
|
5
|
+
* [![Gem Version](https://badge.fury.io/rb/weight.png)](http://badge.fury.io/rb/weight)
|
5
6
|
|
6
7
|
It's dead simple Value object, which provides convenient way to work with
|
7
8
|
weight in a different unit systems. It could be useful if you have to work with
|
@@ -61,6 +62,13 @@ just put this in your initializer: config/initializers/weight.rb
|
|
61
62
|
Weight.new(2, :kg) / 2 == Weight.new(1, :kg)
|
62
63
|
```
|
63
64
|
|
65
|
+
#### Convert result to the first object unit system
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
Weight.new(1, :kg) + Weight.new(1, :lb) # => #<Weight: @input_value=1.4536, @input_unit=:kg>
|
69
|
+
Weight.new(1, :lb) + Weight.new(1, :kg) # => #<Weight: @input_value=3.2046, @input_unit=:lb>
|
70
|
+
```
|
71
|
+
|
64
72
|
### Basic comparison with Weight objects
|
65
73
|
|
66
74
|
```ruby
|
@@ -76,3 +84,7 @@ just put this in your initializer: config/initializers/weight.rb
|
|
76
84
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
77
85
|
4. Push to the branch (`git push origin my-new-feature`)
|
78
86
|
5. Create new Pull Request
|
87
|
+
|
88
|
+
|
89
|
+
[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/shemerey/weight/trend.png)](https://bitdeli.com/free "Bitdeli Badge")
|
90
|
+
|
data/lib/weight.rb
CHANGED
@@ -51,7 +51,7 @@ class Weight
|
|
51
51
|
# @raise [TypeError] When the argument passed is not a Weight
|
52
52
|
def +(other)
|
53
53
|
raise TypeError, 'You can only add weights' unless other.is_a?(Weight)
|
54
|
-
self.class.new(
|
54
|
+
self.class.new(value + other_value(other), unit)
|
55
55
|
end
|
56
56
|
|
57
57
|
# Comparison operator
|
@@ -79,7 +79,7 @@ class Weight
|
|
79
79
|
# @raise [TypeError] When the argument passed is not a Weight
|
80
80
|
def -(other)
|
81
81
|
raise TypeError, 'You can only substract weights' unless other.is_a?(Weight)
|
82
|
-
self.class.new(
|
82
|
+
self.class.new(value - other_value(other), unit)
|
83
83
|
end
|
84
84
|
|
85
85
|
# Multiplication operation
|
@@ -102,6 +102,16 @@ class Weight
|
|
102
102
|
|
103
103
|
private
|
104
104
|
|
105
|
+
# Helper method return value for other object in current object unit system
|
106
|
+
def other_value(other)
|
107
|
+
case unit
|
108
|
+
when :kg
|
109
|
+
other.to_kgs
|
110
|
+
when :lb
|
111
|
+
other.to_lbs
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
105
115
|
def round(value)
|
106
116
|
sprintf("%0.0#{round_level}f", value.to_f).to_f
|
107
117
|
end
|
data/lib/weight/version.rb
CHANGED
data/spec/weight_spec.rb
CHANGED
@@ -8,6 +8,7 @@ describe Weight do
|
|
8
8
|
|
9
9
|
let(:one_lb) { described_class.new(1, 'lb') }
|
10
10
|
let(:two_lb) { described_class.new(2, 'lb') }
|
11
|
+
let(:three_lb) { described_class.new(3, 'lb') }
|
11
12
|
|
12
13
|
let(:another_lb) { described_class.new(1, 'lb') }
|
13
14
|
|
@@ -15,6 +16,86 @@ describe Weight do
|
|
15
16
|
described_class.new(1, 'lb').should be_instance_of(described_class)
|
16
17
|
end
|
17
18
|
|
19
|
+
describe 'basic mas calculation' do
|
20
|
+
it 'for - operator result unit should be first == to first object unit' do
|
21
|
+
(one_kg - one_lb).unit.should == :kg
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'for - operator result unit should be first == to first object unit' do
|
25
|
+
(three_lb - one_kg).unit.should == :lb
|
26
|
+
end
|
27
|
+
|
28
|
+
it 'for + operator result unit should be first == to first object unit' do
|
29
|
+
(one_kg + one_lb).unit.should == :kg
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'for + operator result unit should be first == to first object unit' do
|
33
|
+
(one_lb + one_kg).unit.should == :lb
|
34
|
+
end
|
35
|
+
|
36
|
+
it '[-] should compute weight objects with different units properly' do
|
37
|
+
(one_kg - one_lb).should > one_lb
|
38
|
+
(one_kg + one_lb).should < two_kg
|
39
|
+
end
|
40
|
+
|
41
|
+
it '[+] should compute weight objects with different units properly' do
|
42
|
+
(one_kg + one_lb).should < two_kg
|
43
|
+
(one_kg + one_lb).should > two_lb
|
44
|
+
end
|
45
|
+
|
46
|
+
it '1 lb + 1 lb should be 2 lbs' do
|
47
|
+
(one_lb + one_lb).should == two_lb
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'two objects with the same weight and type should be equal' do
|
51
|
+
one_lb.should == another_lb
|
52
|
+
end
|
53
|
+
|
54
|
+
it '2 lbs - 1 lb should be 1 lb' do
|
55
|
+
(two_lb - one_lb).should == one_lb
|
56
|
+
end
|
57
|
+
|
58
|
+
it '2 times 1 lb should be 2 lbs' do
|
59
|
+
(one_lb * 2).should == two_lb
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'attempting to add a weight to something that is not a weight should raise an error' do
|
63
|
+
expect do
|
64
|
+
(one_lb + 1)
|
65
|
+
end.to raise_error(TypeError)
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'attempting to substract from a weight something that is not a weight should raise an error' do
|
69
|
+
expect do
|
70
|
+
(one_lb - 1)
|
71
|
+
end.to raise_error(TypeError)
|
72
|
+
end
|
73
|
+
|
74
|
+
it 'attempting to multiply a weight by another weight should raise an error' do
|
75
|
+
expect do
|
76
|
+
(one_lb * one_lb)
|
77
|
+
end.to raise_error(TypeError)
|
78
|
+
end
|
79
|
+
|
80
|
+
it '1 kg multipied by two should be 2 kg' do
|
81
|
+
(one_kg * 2).should == two_kg
|
82
|
+
end
|
83
|
+
|
84
|
+
it '2 kgs divided by two should be 1 kg' do
|
85
|
+
(two_kg / 2).should == one_kg
|
86
|
+
end
|
87
|
+
|
88
|
+
it '2 lbs divided by two should be 1 lbs' do
|
89
|
+
(two_lb / 2).should == one_lb
|
90
|
+
end
|
91
|
+
|
92
|
+
it 'attempting to divide a weight by another weight should raise an error' do
|
93
|
+
expect do
|
94
|
+
(two_lb / two_lb)
|
95
|
+
end.to raise_error(TypeError)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
18
99
|
describe 'calculation between different units' do
|
19
100
|
it 'should convert 1 kg to 2.2046 lbs' do
|
20
101
|
described_class.new(1, 'kg').to_lbs.should == 2.2046
|
@@ -74,62 +155,10 @@ describe Weight do
|
|
74
155
|
one_lb.should be < two_lb
|
75
156
|
end
|
76
157
|
|
77
|
-
it '1 lb + 1 lb should be 2 lbs' do
|
78
|
-
(one_lb + one_lb).should == two_lb
|
79
|
-
end
|
80
|
-
|
81
|
-
it 'two objects with the same weight and type should be equal' do
|
82
|
-
one_lb.should == another_lb
|
83
|
-
end
|
84
|
-
|
85
|
-
it '2 lbs - 1 lb should be 1 lb' do
|
86
|
-
(two_lb - one_lb).should == one_lb
|
87
|
-
end
|
88
|
-
|
89
|
-
it '2 times 1 lb should be 2 lbs' do
|
90
|
-
(one_lb * 2).should == two_lb
|
91
|
-
end
|
92
|
-
|
93
158
|
it 'attempting to compare a weight to something that is not a weight should raise an error' do
|
94
159
|
expect do
|
95
160
|
(one_lb == 1)
|
96
161
|
end.to raise_error(TypeError)
|
97
162
|
end
|
98
|
-
|
99
|
-
it 'attempting to add a weight to something that is not a weight should raise an error' do
|
100
|
-
expect do
|
101
|
-
(one_lb + 1)
|
102
|
-
end.to raise_error(TypeError)
|
103
|
-
end
|
104
|
-
|
105
|
-
it 'attempting to substract from a weight something that is not a weight should raise an error' do
|
106
|
-
expect do
|
107
|
-
(one_lb - 1)
|
108
|
-
end.to raise_error(TypeError)
|
109
|
-
end
|
110
|
-
|
111
|
-
it 'attempting to multiply a weight by another weight should raise an error' do
|
112
|
-
expect do
|
113
|
-
(one_lb * one_lb)
|
114
|
-
end.to raise_error(TypeError)
|
115
|
-
end
|
116
|
-
|
117
|
-
it '1 kg multipied by two should be 2 kg' do
|
118
|
-
(one_kg * 2).should == two_kg
|
119
|
-
end
|
120
|
-
|
121
|
-
it '2 kgs divided by two should be 1 kg' do
|
122
|
-
(two_kg / 2).should == one_kg
|
123
|
-
end
|
124
|
-
|
125
|
-
it '2 lbs divided by two should be 1 lbs' do
|
126
|
-
(two_lb / 2).should == one_lb
|
127
|
-
end
|
128
|
-
|
129
|
-
it 'attempting to divide a weight by another weight should raise an error' do
|
130
|
-
expect do
|
131
|
-
(two_lb / two_lb)
|
132
|
-
end.to raise_error(TypeError)
|
133
|
-
end
|
134
163
|
end
|
135
164
|
end
|
metadata
CHANGED
@@ -1,89 +1,78 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: weight
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
- 2
|
10
|
-
version: 0.0.2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
11
5
|
platform: ruby
|
12
|
-
authors:
|
6
|
+
authors:
|
13
7
|
- Anton Shemerey
|
14
8
|
autorequire:
|
15
9
|
bindir: bin
|
16
10
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
- !ruby/object:Gem::Dependency
|
11
|
+
date: 2013-11-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
21
14
|
name: bundler
|
22
|
-
|
23
|
-
|
24
|
-
requirements:
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
25
17
|
- - ~>
|
26
|
-
- !ruby/object:Gem::Version
|
27
|
-
|
28
|
-
segments:
|
29
|
-
- 1
|
30
|
-
- 3
|
31
|
-
version: "1.3"
|
32
|
-
prerelease: false
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
33
20
|
type: :development
|
34
|
-
requirement: *id001
|
35
|
-
- !ruby/object:Gem::Dependency
|
36
|
-
name: rake
|
37
|
-
version_requirements: &id002 !ruby/object:Gem::Requirement
|
38
|
-
none: false
|
39
|
-
requirements:
|
40
|
-
- - ">="
|
41
|
-
- !ruby/object:Gem::Version
|
42
|
-
hash: 3
|
43
|
-
segments:
|
44
|
-
- 0
|
45
|
-
version: "0"
|
46
21
|
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
47
34
|
type: :development
|
48
|
-
requirement: *id002
|
49
|
-
- !ruby/object:Gem::Dependency
|
50
|
-
name: rspec
|
51
|
-
version_requirements: &id003 !ruby/object:Gem::Requirement
|
52
|
-
none: false
|
53
|
-
requirements:
|
54
|
-
- - ">="
|
55
|
-
- !ruby/object:Gem::Version
|
56
|
-
hash: 3
|
57
|
-
segments:
|
58
|
-
- 0
|
59
|
-
version: "0"
|
60
35
|
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '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: '0'
|
61
48
|
type: :development
|
62
|
-
requirement: *id003
|
63
|
-
- !ruby/object:Gem::Dependency
|
64
|
-
name: pry
|
65
|
-
version_requirements: &id004 !ruby/object:Gem::Requirement
|
66
|
-
none: false
|
67
|
-
requirements:
|
68
|
-
- - ">="
|
69
|
-
- !ruby/object:Gem::Version
|
70
|
-
hash: 3
|
71
|
-
segments:
|
72
|
-
- 0
|
73
|
-
version: "0"
|
74
49
|
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
75
62
|
type: :development
|
76
|
-
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
77
69
|
description: Dead simple value object, with one purpose work with weight
|
78
|
-
email:
|
70
|
+
email:
|
79
71
|
- shemerey@gmail.com
|
80
72
|
executables: []
|
81
|
-
|
82
73
|
extensions: []
|
83
|
-
|
84
74
|
extra_rdoc_files: []
|
85
|
-
|
86
|
-
files:
|
75
|
+
files:
|
87
76
|
- .gitignore
|
88
77
|
- .travis.yml
|
89
78
|
- Gemfile
|
@@ -99,39 +88,33 @@ files:
|
|
99
88
|
- spec/weight_spec.rb
|
100
89
|
- weight.gemspec
|
101
90
|
homepage: https://github.com/shemerey/weight
|
102
|
-
licenses:
|
91
|
+
licenses:
|
103
92
|
- MIT
|
93
|
+
metadata: {}
|
104
94
|
post_install_message:
|
105
95
|
rdoc_options: []
|
106
|
-
|
107
|
-
require_paths:
|
96
|
+
require_paths:
|
108
97
|
- lib
|
109
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
none: false
|
120
|
-
requirements:
|
121
|
-
- - ">="
|
122
|
-
- !ruby/object:Gem::Version
|
123
|
-
hash: 3
|
124
|
-
segments:
|
125
|
-
- 0
|
126
|
-
version: "0"
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - '>='
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
127
108
|
requirements: []
|
128
|
-
|
129
109
|
rubyforge_project:
|
130
|
-
rubygems_version: 1.
|
110
|
+
rubygems_version: 2.1.10
|
131
111
|
signing_key:
|
132
|
-
specification_version:
|
133
|
-
summary: It's dead simple Value object, which provides convenient way to work with
|
134
|
-
|
112
|
+
specification_version: 4
|
113
|
+
summary: It's dead simple Value object, which provides convenient way to work with
|
114
|
+
weight in a different unit systems. It could be useful if you have to work with
|
115
|
+
different unit system, for example you have to work with :kg as well as :lb for
|
116
|
+
USA
|
117
|
+
test_files:
|
135
118
|
- spec/spec_helper.rb
|
136
119
|
- spec/weight/configuration_spec.rb
|
137
120
|
- spec/weight_spec.rb
|