auom 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,9 @@
1
+ # v0.0.4 2012-11-20
2
+
3
+ * [feature] Support for relational operatiors >, <, >= and <=
4
+
5
+ [Compare v0.0.3..v0.0.4](https://github.com/mbj/auom/compare/v0.0.3...v0.0.4)
6
+
7
+ # v0.0.3 2012-11-19
8
+
9
+ First public release :)
@@ -1,3 +1,3 @@
1
1
  ---
2
- threshold: 12.0
3
- total_score: 68.0
2
+ threshold: 12
3
+ total_score: 128.0
@@ -2,8 +2,13 @@
2
2
  library: auom
3
3
  namespace: AUOM
4
4
  aliases:
5
+ AUOM::Relational:
6
+ '>=': greater_than_or_equal_to?
7
+ '<=': less_than_or_equal_to?
8
+ '<': less_than?
9
+ '>': greater_than?
5
10
  AUOM::Algebra:
6
- '-': substract
7
- '+': add
8
- '*': multiply
9
- '/': divide
11
+ '-': substract
12
+ '+': add
13
+ '*': multiply
14
+ '/': divide
@@ -3,7 +3,7 @@ AbcMetricMethodCheck:
3
3
  score: 25.1
4
4
  AssignmentInConditionalCheck: { }
5
5
  CaseMissingElseCheck: { }
6
- ClassLineCountCheck: { line_count: 274 }
6
+ ClassLineCountCheck: { line_count: 315 }
7
7
  ClassNameCheck:
8
8
  pattern: !ruby/regexp /\A(?:[A-Z]+|[A-Z][a-z](?:[A-Z]?[a-z])+)\z/
9
9
  ClassVariableCheck: { }
@@ -14,7 +14,7 @@ ForLoopCheck: { }
14
14
  MethodLineCountCheck: { line_count: 14 }
15
15
  MethodNameCheck:
16
16
  pattern: !ruby/regexp /\A(?:[a-z\d](?:_?[a-z\d])+[?!=]?|\[\]=?|==|<=>|<<|[+*&|-])\z/
17
- ModuleLineCountCheck: { line_count: 277 }
17
+ ModuleLineCountCheck: { line_count: 318 }
18
18
  ModuleNameCheck:
19
19
  pattern: !ruby/regexp /\A(?:[A-Z]+|[A-Z][a-z](?:[A-Z]?[a-z])+)\z/
20
20
  ParameterNumberCheck: { parameter_count: 3 }
@@ -4,6 +4,7 @@ require 'backports'
4
4
  require 'auom/algebra'
5
5
  require 'auom/equalization'
6
6
  require 'auom/inspection'
7
+ require 'auom/relational'
7
8
 
8
9
  # Library namespace
9
10
  module AUOM
@@ -12,6 +13,7 @@ module AUOM
12
13
  include Algebra
13
14
  include Equalization
14
15
  include Inspection
16
+ include Relational
15
17
 
16
18
  # Return scalar
17
19
  #
@@ -111,6 +113,31 @@ module AUOM
111
113
  numerators.empty? and denominators.empty?
112
114
  end
113
115
 
116
+ # Test if units are the same
117
+ #
118
+ # @param [Unit] other
119
+ #
120
+ # @return [true]
121
+ # if units are the same
122
+ #
123
+ # @return [false]
124
+ # otehrwise
125
+ #
126
+ # @example
127
+ #
128
+ # a = Unit.new(1)
129
+ # b = Unit.new(1, :euro)
130
+ # c = Unit.new(2, :euro)
131
+ #
132
+ # a.same_unit?(b) # => false
133
+ # b.same_unit?(c) # => true
134
+ #
135
+ # @api public
136
+ #
137
+ def same_unit?(other)
138
+ other.unit.eql?(unit)
139
+ end
140
+
114
141
  # Instancitate a new unit
115
142
  #
116
143
  # @param [Rational] scalar
@@ -156,6 +183,22 @@ module AUOM
156
183
  super(scalar, *[numerators, denominators].map { |base| base.sort_by(&:to_s) }).freeze
157
184
  end
158
185
 
186
+ # Assert units are the same
187
+ #
188
+ # @param [Unit] other
189
+ #
190
+ # @return [self]
191
+ #
192
+ # @api private
193
+ #
194
+ def assert_same_unit(other)
195
+ unless same_unit?(other)
196
+ raise ArgumentError, 'Incompatible units'
197
+ end
198
+
199
+ self
200
+ end
201
+
159
202
  private
160
203
 
161
204
  # Initialize unit
@@ -180,7 +223,6 @@ module AUOM
180
223
  @scalar.freeze
181
224
  end
182
225
 
183
-
184
226
  # Return converted operand or raise error
185
227
  #
186
228
  # @param [Object] operand
@@ -24,9 +24,7 @@ module AUOM
24
24
  klass = self.class
25
25
  operand = klass.convert(operand)
26
26
 
27
- unless operand.unit == unit
28
- raise ArgumentError, 'Incompatible units for substraction or addition'
29
- end
27
+ assert_same_unit(operand)
30
28
 
31
29
  klass.new(
32
30
  operand.scalar + scalar,
@@ -35,7 +33,7 @@ module AUOM
35
33
  )
36
34
  end
37
35
 
38
- alias :+ :add
36
+ alias_method :+, :add
39
37
 
40
38
  # Return substraction result
41
39
  #
@@ -60,7 +58,7 @@ module AUOM
60
58
  self.add(self.class.convert(operand) * -1)
61
59
  end
62
60
 
63
- alias :- :substract
61
+ alias_method :-, :substract
64
62
 
65
63
  # Return multiplication result
66
64
  #
@@ -92,7 +90,7 @@ module AUOM
92
90
  )
93
91
  end
94
92
 
95
- alias :* :multiply
93
+ alias_method :*, :multiply
96
94
 
97
95
  # Return division result
98
96
  #
@@ -124,6 +122,6 @@ module AUOM
124
122
  )
125
123
  end
126
124
 
127
- alias :/ :divide
125
+ alias_method :/, :divide
128
126
  end
129
127
  end
@@ -0,0 +1,90 @@
1
+ module AUOM
2
+ # Mixin to add relational operators
3
+ module Relational
4
+
5
+ # Test if unit is less than or equal to other
6
+ #
7
+ # @param [Unit] other
8
+ #
9
+ # @return [true]
10
+ # if unit is less than other
11
+ #
12
+ # @return [false]
13
+ # otherwise
14
+ #
15
+ # @api private
16
+ #
17
+ def less_than_or_equal_to?(other)
18
+ relational_operation(other, :<=)
19
+ end
20
+ alias_method :<=, :less_than_or_equal_to?
21
+
22
+ # Test if unit is greater than or equal toother
23
+ #
24
+ # @param [Unit] other
25
+ #
26
+ # @return [true]
27
+ # if unit is greater than other
28
+ #
29
+ # @return [false]
30
+ # otherwise
31
+ #
32
+ # @api private
33
+ #
34
+ def greater_than_or_equal_to?(other)
35
+ relational_operation(other, :>=)
36
+ end
37
+ alias_method :>=, :greater_than_or_equal_to?
38
+
39
+ # Test if unit is less than other
40
+ #
41
+ # @param [Unit] other
42
+ #
43
+ # @return [true]
44
+ # if unit is less than other
45
+ #
46
+ # @return [false]
47
+ # otherwise
48
+ #
49
+ # @api private
50
+ #
51
+ def less_than?(other)
52
+ relational_operation(other, :<)
53
+ end
54
+ alias_method :<, :less_than?
55
+
56
+ # Test if unit is greater than other
57
+ #
58
+ # @param [Unit] other
59
+ #
60
+ # @return [true]
61
+ # if unit is greater than other
62
+ #
63
+ # @return [false]
64
+ # otherwise
65
+ #
66
+ # @api private
67
+ #
68
+ def greater_than?(other)
69
+ relational_operation(other, :>)
70
+ end
71
+ alias_method :<, :less_than?
72
+
73
+ private
74
+
75
+ # Peform relational operation
76
+ #
77
+ # @param [Unit] other
78
+ # @param [Symbol] operation
79
+ #
80
+ # @return [Object]
81
+ #
82
+ # @api private
83
+ #
84
+ def relational_operation(other, operation)
85
+ assert_same_unit(other)
86
+ scalar.public_send(operation, other.scalar)
87
+ end
88
+
89
+ end
90
+ end
@@ -1,3 +1,3 @@
1
1
  module AUOM
2
- VERSION = '0.0.3'.freeze
2
+ VERSION = '0.0.4'.freeze
3
3
  end
@@ -1,5 +1,5 @@
1
1
  shared_examples_for 'an incompatible operation' do
2
2
  it 'should raise ArgumentError' do
3
- expect { subject }.to raise_error(ArgumentError,'Incompatible units for substraction or addition')
3
+ expect { subject }.to raise_error(ArgumentError,'Incompatible units')
4
4
  end
5
5
  end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe AUOM::Relational,'#greater_than_or_equal_to?' do
4
+
5
+ subject { object.greater_than_or_equal_to?(operand) }
6
+
7
+ let(:object) { AUOM::Unit.new(1, :meter) }
8
+
9
+ let(:operand) { AUOM::Unit.new(scalar, unit) }
10
+
11
+ context 'when operand unit is the same' do
12
+
13
+ let(:unit) { :meter }
14
+
15
+ context 'and operand scalar is less than receiver scalar' do
16
+ let(:scalar) { 0 }
17
+
18
+ it { should be(true) }
19
+ end
20
+
21
+ context 'and operand scalar is equal to receiver scalar' do
22
+ let(:scalar) { 1 }
23
+
24
+ it { should be(true) }
25
+ end
26
+
27
+ context 'and operand scalar is greater than receiver scalar' do
28
+ let(:scalar) { 2 }
29
+
30
+ it { should be(false) }
31
+ end
32
+ end
33
+
34
+ context 'when operand unit is not the same' do
35
+ let(:scalar) { 1 }
36
+ let(:unit) { :euro }
37
+
38
+ it_should_behave_like 'an incompatible operation'
39
+ end
40
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe AUOM::Relational,'#greater_than?' do
4
+
5
+ subject { object.greater_than?(operand) }
6
+
7
+ let(:object) { AUOM::Unit.new(1, :meter) }
8
+
9
+ let(:operand) { AUOM::Unit.new(scalar, unit) }
10
+
11
+ context 'when operand unit is the same' do
12
+
13
+ let(:unit) { :meter }
14
+
15
+ context 'and operand scalar is less than receiver scalar' do
16
+ let(:scalar) { 0 }
17
+
18
+ it { should be(true) }
19
+ end
20
+
21
+ context 'and operand scalar is equal to receiver scalar' do
22
+ let(:scalar) { 1 }
23
+
24
+ it { should be(false) }
25
+ end
26
+
27
+ context 'and operand scalar is greater than receiver scalar' do
28
+ let(:scalar) { 2 }
29
+
30
+ it { should be(false) }
31
+ end
32
+ end
33
+
34
+ context 'when operand unit is not the same' do
35
+ let(:scalar) { 1 }
36
+ let(:unit) { :euro }
37
+
38
+ it_should_behave_like 'an incompatible operation'
39
+ end
40
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe AUOM::Relational,'#less_than_or_equal_to?' do
4
+
5
+ subject { object.less_than_or_equal_to?(operand) }
6
+
7
+ let(:object) { AUOM::Unit.new(1, :meter) }
8
+
9
+ let(:operand) { AUOM::Unit.new(scalar, unit) }
10
+
11
+ context 'when operand unit is the same' do
12
+
13
+ let(:unit) { :meter }
14
+
15
+ context 'and operand scalar is less than receiver scalar' do
16
+ let(:scalar) { 0 }
17
+
18
+ it { should be(false) }
19
+ end
20
+
21
+ context 'and operand scalar is equal to receiver scalar' do
22
+ let(:scalar) { 1 }
23
+
24
+ it { should be(true) }
25
+ end
26
+
27
+ context 'and operand scalar is greater than receiver scalar' do
28
+ let(:scalar) { 2 }
29
+
30
+ it { should be(true) }
31
+ end
32
+ end
33
+
34
+ context 'when operand unit is not the same' do
35
+ let(:scalar) { 1 }
36
+ let(:unit) { :euro }
37
+
38
+ it_should_behave_like 'an incompatible operation'
39
+ end
40
+ end
@@ -0,0 +1,40 @@
1
+ require 'spec_helper'
2
+
3
+ describe AUOM::Relational,'#less_than?' do
4
+
5
+ subject { object.less_than?(operand) }
6
+
7
+ let(:object) { AUOM::Unit.new(1, :meter) }
8
+
9
+ let(:operand) { AUOM::Unit.new(scalar, unit) }
10
+
11
+ context 'when operand unit is the same' do
12
+
13
+ let(:unit) { :meter }
14
+
15
+ context 'and operand scalar is less than receiver scalar' do
16
+ let(:scalar) { 0 }
17
+
18
+ it { should be(false) }
19
+ end
20
+
21
+ context 'and operand scalar is equal to receiver scalar' do
22
+ let(:scalar) { 1 }
23
+
24
+ it { should be(false) }
25
+ end
26
+
27
+ context 'and operand scalar is greater than receiver scalar' do
28
+ let(:scalar) { 2 }
29
+
30
+ it { should be(true) }
31
+ end
32
+ end
33
+
34
+ context 'when operand unit is not the same' do
35
+ let(:scalar) { 1 }
36
+ let(:unit) { :euro }
37
+
38
+ it_should_behave_like 'an incompatible operation'
39
+ end
40
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe AUOM::Unit,'#assert_same_unit' do
4
+
5
+ subject { object.assert_same_unit(other) }
6
+
7
+ let(:object) { described_class.new(1,*unit) }
8
+ let(:unit) { [[:meter],[:euro]] }
9
+
10
+ context 'when units are the same' do
11
+ let(:other) { AUOM::Unit.new(2, :meter, :euro) }
12
+
13
+ it { should be(object) }
14
+ end
15
+
16
+ context 'when units arent the same' do
17
+ let(:other) { AUOM::Unit.new(2, :meter) }
18
+
19
+ it_should_behave_like 'an incompatible operation'
20
+ end
21
+
22
+ end
@@ -0,0 +1,22 @@
1
+ require 'spec_helper'
2
+
3
+ describe AUOM::Unit,'#same_unit?' do
4
+
5
+ subject { object.same_unit?(other) }
6
+
7
+ let(:object) { described_class.new(1,*unit) }
8
+ let(:unit) { [[:meter],[:euro]] }
9
+
10
+ context 'when units are the same' do
11
+ let(:other) { AUOM::Unit.new(2, :meter, :euro) }
12
+
13
+ it { should be(true) }
14
+ end
15
+
16
+ context 'when units arent the same' do
17
+ let(:other) { AUOM::Unit.new(2, :meter) }
18
+
19
+ it { should be(false) }
20
+ end
21
+
22
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: auom
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Markus Schirp
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-11-19 00:00:00 Z
18
+ date: 2012-11-20 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: backports
@@ -46,6 +46,7 @@ files:
46
46
  - .gitignore
47
47
  - .rspec
48
48
  - .travis.yml
49
+ - Changelog.md
49
50
  - Gemfile
50
51
  - Gemfile.devtools
51
52
  - Guardfile
@@ -64,6 +65,7 @@ files:
64
65
  - lib/auom/algebra.rb
65
66
  - lib/auom/equalization.rb
66
67
  - lib/auom/inspection.rb
68
+ - lib/auom/relational.rb
67
69
  - lib/auom/version.rb
68
70
  - spec/rcov.opts
69
71
  - spec/shared/command_method_behavior.rb
@@ -76,7 +78,6 @@ files:
76
78
  - spec/shared/sunits_shared.rb
77
79
  - spec/spec.opts
78
80
  - spec/spec_helper.rb
79
- - spec/unit/auom/add_spec.rb
80
81
  - spec/unit/auom/algebra/add_spec.rb
81
82
  - spec/unit/auom/algebra/divide_spec.rb
82
83
  - spec/unit/auom/algebra/multiply_spec.rb
@@ -86,6 +87,11 @@ files:
86
87
  - spec/unit/auom/equalization/hash_spec.rb
87
88
  - spec/unit/auom/inspection/class_methods/prettify_unit_part_spec.rb
88
89
  - spec/unit/auom/inspection/inspect_spec.rb
90
+ - spec/unit/auom/relational/greater_than_or_equal_to_spec.rb
91
+ - spec/unit/auom/relational/greater_than_spec.rb
92
+ - spec/unit/auom/relational/less_than_or_equal_to_spec.rb
93
+ - spec/unit/auom/relational/less_than_spec.rb
94
+ - spec/unit/auom/unit/assert_same_unit_spec.rb
89
95
  - spec/unit/auom/unit/class_methods/convert_spec.rb
90
96
  - spec/unit/auom/unit/class_methods/lookup_spec.rb
91
97
  - spec/unit/auom/unit/class_methods/new_spec.rb
@@ -93,6 +99,7 @@ files:
93
99
  - spec/unit/auom/unit/class_methods/units_spec.rb
94
100
  - spec/unit/auom/unit/denominators_spec.rb
95
101
  - spec/unit/auom/unit/numerators_spec.rb
102
+ - spec/unit/auom/unit/same_unit_spec.rb
96
103
  - spec/unit/auom/unit/scalar_spec.rb
97
104
  - spec/unit/auom/unit/unit_spec.rb
98
105
  - spec/unit/auom/unit/unitless_spec.rb
@@ -141,7 +148,6 @@ test_files:
141
148
  - spec/shared/sunits_shared.rb
142
149
  - spec/spec.opts
143
150
  - spec/spec_helper.rb
144
- - spec/unit/auom/add_spec.rb
145
151
  - spec/unit/auom/algebra/add_spec.rb
146
152
  - spec/unit/auom/algebra/divide_spec.rb
147
153
  - spec/unit/auom/algebra/multiply_spec.rb
@@ -151,6 +157,11 @@ test_files:
151
157
  - spec/unit/auom/equalization/hash_spec.rb
152
158
  - spec/unit/auom/inspection/class_methods/prettify_unit_part_spec.rb
153
159
  - spec/unit/auom/inspection/inspect_spec.rb
160
+ - spec/unit/auom/relational/greater_than_or_equal_to_spec.rb
161
+ - spec/unit/auom/relational/greater_than_spec.rb
162
+ - spec/unit/auom/relational/less_than_or_equal_to_spec.rb
163
+ - spec/unit/auom/relational/less_than_spec.rb
164
+ - spec/unit/auom/unit/assert_same_unit_spec.rb
154
165
  - spec/unit/auom/unit/class_methods/convert_spec.rb
155
166
  - spec/unit/auom/unit/class_methods/lookup_spec.rb
156
167
  - spec/unit/auom/unit/class_methods/new_spec.rb
@@ -158,6 +169,7 @@ test_files:
158
169
  - spec/unit/auom/unit/class_methods/units_spec.rb
159
170
  - spec/unit/auom/unit/denominators_spec.rb
160
171
  - spec/unit/auom/unit/numerators_spec.rb
172
+ - spec/unit/auom/unit/same_unit_spec.rb
161
173
  - spec/unit/auom/unit/scalar_spec.rb
162
174
  - spec/unit/auom/unit/unit_spec.rb
163
175
  - spec/unit/auom/unit/unitless_spec.rb
File without changes