weight 0.0.1

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: aa6ce855ed5b69b14e47239e204a95bd8f0aa8ee
4
+ data.tar.gz: 9ae53a6cf99f0cf5d474d49baecd3169e084379e
5
+ SHA512:
6
+ metadata.gz: 31bbe20390655459046b40bc53428ca9fa1c36447fa306abbe43909cd1b15f1289cdda20ffddfc5eb90dfd93fceb4ef4c90f583a28378f0958a686450a626b76
7
+ data.tar.gz: c172d0566ba2646a19096a2087316a46c277068adc606b187a845abe7d42cd8bc5d74c7f6665d893617838e58ca369e8118089063243120835b67e6e195954ac
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in weight.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Anton Shemerey
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,66 @@
1
+ # Weight (0.0.1 alpha version)
2
+
3
+ [![Code Climate](https://codeclimate.com/github/shemerey/weight.png)](https://codeclimate.com/github/shemerey/weight)
4
+
5
+ It's dead simple Value object, which provides convenient way to work with
6
+ weight in a different unit systems. It could be useful if you have to work with
7
+ different unit system, for example you have to work with :kg as well as :lb for USA
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'weight'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install weight
22
+
23
+ ## Configuration
24
+ You could configure default unit system (:kg or :lb)
25
+
26
+ just put this in your initializer: config/initializers/weight.rb
27
+
28
+ ```ruby
29
+ # new object in kg by default
30
+ Weight.new # => <Weight: @input_value=0, @input_unit=:kg>
31
+
32
+ Weight::Configuration.configure do |config|
33
+ config.default_unit = :lb # by default it uses :kg
34
+ end
35
+
36
+ # Now default object looks like
37
+ Weight.new # => <Weight: @input_value=0, @input_unit=:lb>
38
+ ```
39
+
40
+ ## Usage
41
+
42
+ ### Basic Math with Weight objects
43
+
44
+ ```ruby
45
+ Weight.new(1, :kg) + Weight.new(1, :kg) == Weight.new(2, :kg)
46
+ Weight.new(1, :kg) - Weight.new(1, :kg) == Weight.new(0, :kg)
47
+
48
+ Weight.new(1, :kg) * 2 == Weight.new(2, :kg)
49
+ Weight.new(2, :kg) / 2 == Weight.new(1, :kg)
50
+ ```
51
+
52
+ ### Basic comparison with Weight objects
53
+
54
+ ```ruby
55
+ Weight.new(3, :lb).between?(Weight.new(1, :kg), Weight.new(2, :kg))
56
+ Weight.new(1, :kg) > Weight.new(2, :lb)
57
+ Weight.new(1, :lb) <= Weight.new(0.5, :kg)
58
+ ```
59
+
60
+ ## Contributing
61
+
62
+ 1. Fork it
63
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
64
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
65
+ 4. Push to the branch (`git push origin my-new-feature`)
66
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
7
+ task :test => :spec
data/lib/weight.rb ADDED
@@ -0,0 +1,136 @@
1
+ require "weight/all"
2
+
3
+ class Weight
4
+ include Comparable
5
+ include Weight::Configuration
6
+
7
+ # Class contructor
8
+ # @param value [Int] The weight of the object
9
+ # @param unit [String] The unit in which the value is expressed. Currently
10
+ # supports 'lb' and 'kg'
11
+ # @raise [TypeError] When weight is negative
12
+ def initialize(value = 0, unit = default_unit)
13
+ self.value, self.unit = value, unit
14
+ end
15
+
16
+ def value
17
+ @input_value
18
+ end
19
+
20
+ def unit
21
+ @input_unit
22
+ end
23
+
24
+ # Returns the weight converted to pounds
25
+ # @return [Float] The weight in pounds
26
+ def to_kgs
27
+ raw_data_in_kg.round(4)
28
+ end
29
+
30
+ # Returns the weight converted to kilograms
31
+ # @return [Float] The weight in kilograms
32
+ def to_lbs
33
+ (raw_data_in_kg * pounds_per_kilogram).round(4)
34
+ end
35
+
36
+ # Returns the weight converted to pounds
37
+ # @return [Float] The weight in pounds
38
+ def to_f
39
+ value.to_f.round(4)
40
+ end
41
+
42
+ # Returns the weight in pounds rounded to the closest integer
43
+ # @return [Int] The weight in pounds rounded
44
+ def to_i
45
+ value.round
46
+ end
47
+
48
+ # Addition operation
49
+ # @param other [Weight] The other weight to add to the current one
50
+ # @return [Weight] The sum of the two weights
51
+ # @raise [TypeError] When the argument passed is not a Weight
52
+ def +(other)
53
+ raise TypeError, 'You can only add weights' unless other.is_a?(Weight)
54
+ self.class.new(raw_data_in_kg + other.to_kgs, :kg)
55
+ end
56
+
57
+ # Comparison operator
58
+ # @param other [Weight] The other weight to compare to the current one
59
+ # @return [Bool] True if both of the objects have the same weight. False
60
+ # otherwise
61
+ # @raise [TypeError] When the argument passed is not a Weight
62
+ def ==(other)
63
+ raise TypeError, 'You can only compare weights' unless other.is_a?(Weight)
64
+ raw_data_in_kg.round(4) == other.to_kgs
65
+ end
66
+
67
+ # Comparison operator
68
+ # @param other [Weight] The other weight to compare to the current one
69
+ # @return [-1,0,1]
70
+ # @raise [TypeError] When the argument passed is not a Weight
71
+ def <=>(other)
72
+ raise TypeError, 'You can only compare weights' unless other.is_a?(Weight)
73
+ self.to_kgs <=> other.to_kgs
74
+ end
75
+
76
+ # Substract operation
77
+ # @param other [Weight] The other weight to substract to the current one
78
+ # @return [Weight] The substraction of the two weights
79
+ # @raise [TypeError] When the argument passed is not a Weight
80
+ def -(other)
81
+ raise TypeError, 'You can only substract weights' unless other.is_a?(Weight)
82
+ self.class.new(raw_data_in_kg - other.to_kgs, :kg)
83
+ end
84
+
85
+ # Multiplication operation
86
+ # @param other [Weight] The other weight to multiply by the current one
87
+ # @return [Weight] The product of the two weights
88
+ # @raise [TypeError] When the argument passed is not a Weight
89
+ def *(other)
90
+ raise TypeError, 'You can only multiply a weight by a number' unless other.is_a?(Numeric)
91
+ self.class.new(value * other, unit)
92
+ end
93
+
94
+ # Division operation
95
+ # @param other [Weight] The weight to divide the current one by
96
+ # @return [Weight] The result of the operation
97
+ # @raise [TypeError] When the argument passed is not a Weight
98
+ def /(other)
99
+ raise TypeError, 'You can only divide a weight by a number' unless other.is_a?(Numeric)
100
+ self.class.new(value / other, unit)
101
+ end
102
+
103
+ private
104
+
105
+ # The weight expressed in kilograms for internal usage
106
+ # @return [Float] The weight in pounds
107
+ # @raise [TypeError] When the unit passed was neither kg nor lb
108
+ def raw_data_in_kg
109
+ case unit
110
+ when :kg
111
+ value
112
+ when :lb
113
+ value / pounds_per_kilogram
114
+ else
115
+ raise TypeError, 'Unit is neither kg nor lb'
116
+ end
117
+ end
118
+
119
+ def value=(value)
120
+ raise TypeError, 'Weight cannot be negative' if value < 0
121
+ raise TypeError, 'Weight value should be Numeric' unless value.is_a? Numeric
122
+ @input_value = value
123
+ end
124
+
125
+ def unit=(unit)
126
+ unit = unit.to_s.downcase.to_sym
127
+ raise ArgumentError, "You should use one from allowe unit tipes #{allowed_units.inspect}" unless allowed_units.include?(unit)
128
+ @input_unit = unit
129
+ end
130
+
131
+ # The conversion rate between pounds and kilograms
132
+ # @return [Float] The number of pounds in a kilogram
133
+ def pounds_per_kilogram
134
+ 2.2046
135
+ end
136
+ end
data/lib/weight/all.rb ADDED
@@ -0,0 +1,2 @@
1
+ require 'weight/version'
2
+ require 'weight/configuration'
@@ -0,0 +1,21 @@
1
+ module Weight::Configuration
2
+ module_function
3
+
4
+ def self.configure
5
+ yield self if block_given?
6
+ end
7
+
8
+ def default_unit=(weight_unit_code)
9
+ default_unit = weight_unit_code.to_s.downcase.to_sym
10
+ raise ArgumentError unless allowed_units.include?(default_unit)
11
+ @@default_unit = default_unit
12
+ end
13
+
14
+ def default_unit
15
+ @@default_unit ||= :kg
16
+ end
17
+
18
+ def allowed_units
19
+ [:kg, :lb]
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ class Weight
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'weight'
4
+
5
+ RSpec.configure do |config|
6
+
7
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe Weight::Configuration do
4
+ describe '#default configuraiton' do
5
+ it 'should be kilogram by default' do
6
+ described_class.default_unit.should == :kg
7
+ end
8
+ end
9
+
10
+ describe 'only :lb, :kg allowed' do
11
+ it 'raise ArgumentError if default_unit not in allowed list [:kg, :lb]' do
12
+ expect {
13
+ described_class.default_unit = 'g'
14
+ }.to raise_error(ArgumentError)
15
+ end
16
+ end
17
+
18
+ describe '#configure default_unit as lb' do
19
+ ['lb', 'Lb', 'lB', 'LB', :lb, :Lb, :lB, :LB].each do |lb|
20
+ it "should be lb if we configure it with #{lb} as a default_unit" do
21
+ described_class.default_unit = lb
22
+ described_class.default_unit.should == :lb
23
+ end
24
+ end
25
+ end
26
+
27
+ describe '#configure default_unit as kg' do
28
+ ['kg', 'Kg', 'kG', 'KG', :kg, :Kg, :kG, :KG].each do |kg|
29
+ it "should be kg if we configure it with #{kg} as a default_unit" do
30
+ described_class.default_unit = kg
31
+ described_class.default_unit.should == :kg
32
+ end
33
+ end
34
+ end
35
+ end
36
+
@@ -0,0 +1,133 @@
1
+ require 'spec_helper'
2
+
3
+ describe Weight do
4
+ let(:one_kg) { described_class.new(1, 'kg') }
5
+ let(:two_kg) { described_class.new(2, 'kg') }
6
+
7
+ let(:one_lb) { described_class.new(1, 'lb') }
8
+ let(:two_lb) { described_class.new(2, 'lb') }
9
+
10
+ let(:another_lb) { described_class.new(1, 'lb') }
11
+
12
+ it 'should take two options value and unit type' do
13
+ described_class.new(1, 'lb').should be_instance_of(described_class)
14
+ end
15
+
16
+ describe 'calculation between different units' do
17
+ it 'should convert 1 kg to 2.2046 lbs' do
18
+ described_class.new(1, 'kg').to_lbs.should == 2.2046
19
+ end
20
+
21
+ it 'should convert 1 lbs to 0.45359 kg' do
22
+ described_class.new(1, 'lb').to_kgs.should == 0.4536
23
+ end
24
+
25
+ it 'should convert 0 kgs to 0 lb' do
26
+ described_class.new(0, 'kg').to_lbs.should == 0.0
27
+ end
28
+
29
+ it 'should convert 0 lbs to 0 kg' do
30
+ described_class.new(0, 'lb').to_kgs.should == 0.0
31
+ end
32
+
33
+ it 'should raise an error when the weight in kgs is negative' do
34
+ expect do
35
+ described_class.new(-1, 'kg')
36
+ end.to raise_error(TypeError)
37
+ end
38
+
39
+ it 'should raise an error when the weight in lbs is negative' do
40
+ expect do
41
+ described_class.new(-1, 'lb')
42
+ end.to raise_error(TypeError)
43
+ end
44
+
45
+ it 'should return the weight in pounds when converted to_f' do
46
+ described_class.new(0.6, 'lb').to_f.should == 0.6
47
+ end
48
+
49
+ it 'should round 0.6 lbs to 1 lb when converted to_i' do
50
+ described_class.new(0.6, 'lb').to_i.should == 1
51
+ end
52
+
53
+ it 'should round 0.5 lbs to 1 lb when converted to_i' do
54
+ described_class.new(0.5, 'lb').to_i.should == 1
55
+ end
56
+
57
+ it 'should round 0.4 lbs to 0 lb when converted to_i' do
58
+ described_class.new(0.4, 'lb').to_i.should == 0
59
+ end
60
+ end
61
+
62
+ describe 'comparison of different weights' do
63
+ it 'two lb more than two' do
64
+ two_lb.should be > one_lb
65
+ end
66
+
67
+ it 'two lb more or eql than two' do
68
+ two_lb.should be >= two_lb
69
+ end
70
+
71
+ it 'one lb less than two' do
72
+ one_lb.should be < two_lb
73
+ end
74
+
75
+ it '1 lb + 1 lb should be 2 lbs' do
76
+ (one_lb + one_lb).should == two_lb
77
+ end
78
+
79
+ it 'two objects with the same weight and type should be equal' do
80
+ one_lb.should == another_lb
81
+ end
82
+
83
+ it '2 lbs - 1 lb should be 1 lb' do
84
+ (two_lb - one_lb).should == one_lb
85
+ end
86
+
87
+ it '2 times 1 lb should be 2 lbs' do
88
+ (one_lb * 2).should == two_lb
89
+ end
90
+
91
+ it 'attempting to compare a weight to something that is not a weight should raise an error' do
92
+ expect do
93
+ (one_lb == 1)
94
+ end.to raise_error(TypeError)
95
+ end
96
+
97
+ it 'attempting to add a weight to something that is not a weight should raise an error' do
98
+ expect do
99
+ (one_lb + 1)
100
+ end.to raise_error(TypeError)
101
+ end
102
+
103
+ it 'attempting to substract from a weight something that is not a weight should raise an error' do
104
+ expect do
105
+ (one_lb - 1)
106
+ end.to raise_error(TypeError)
107
+ end
108
+
109
+ it 'attempting to multiply a weight by another weight should raise an error' do
110
+ expect do
111
+ (one_lb * one_lb)
112
+ end.to raise_error(TypeError)
113
+ end
114
+
115
+ it '1 kg multipied by two should be 2 kg' do
116
+ (one_kg * 2).should == two_kg
117
+ end
118
+
119
+ it '2 kgs divided by two should be 1 kg' do
120
+ (two_kg / 2).should == one_kg
121
+ end
122
+
123
+ it '2 lbs divided by two should be 1 lbs' do
124
+ (two_lb / 2).should == one_lb
125
+ end
126
+
127
+ it 'attempting to divide a weight by another weight should raise an error' do
128
+ expect do
129
+ (two_lb / two_lb)
130
+ end.to raise_error(TypeError)
131
+ end
132
+ end
133
+ end
data/weight.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'weight/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "weight"
8
+ spec.version = Weight::VERSION
9
+ spec.authors = ["Anton Shemerey"]
10
+ spec.email = ["shemerey@gmail.com"]
11
+ spec.description = %q{Dead simple value object, with one purpose work with weight}
12
+ spec.summary = %q{
13
+ It's dead simple Value object, which provides convenient way
14
+ to work with weight in a different unit systems. It could be useful
15
+ if you have to work with different unit system, for example
16
+ you have to work with :kg as well as :lb for USA
17
+ }
18
+ spec.homepage = "https://github.com/shemerey/weight"
19
+ spec.license = "MIT"
20
+
21
+ spec.files = `git ls-files`.split($/)
22
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
23
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.3"
27
+ spec.add_development_dependency "rake"
28
+ spec.add_development_dependency 'rspec'
29
+ spec.add_development_dependency 'pry'
30
+
31
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: weight
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Anton Shemerey
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-11-19 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.3'
20
+ type: :development
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'
34
+ type: :development
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'
48
+ type: :development
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'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Dead simple value object, with one purpose work with weight
70
+ email:
71
+ - shemerey@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/weight.rb
82
+ - lib/weight/all.rb
83
+ - lib/weight/configuration.rb
84
+ - lib/weight/version.rb
85
+ - spec/spec_helper.rb
86
+ - spec/weight/configuration_spec.rb
87
+ - spec/weight_spec.rb
88
+ - weight.gemspec
89
+ homepage: https://github.com/shemerey/weight
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - '>='
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.1.10
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: It's dead simple Value object, which provides convenient way to work with
113
+ weight in a different unit systems. It could be useful if you have to work with
114
+ different unit system, for example you have to work with :kg as well as :lb for
115
+ USA
116
+ test_files:
117
+ - spec/spec_helper.rb
118
+ - spec/weight/configuration_spec.rb
119
+ - spec/weight_spec.rb