auom 0.0.3

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.
Files changed (54) hide show
  1. data/.gitignore +3 -0
  2. data/.rspec +1 -0
  3. data/.travis.yml +13 -0
  4. data/Gemfile +10 -0
  5. data/Gemfile.devtools +52 -0
  6. data/Guardfile +8 -0
  7. data/LICENSE +20 -0
  8. data/README.md +92 -0
  9. data/Rakefile +5 -0
  10. data/TODO +3 -0
  11. data/auom.gemspec +21 -0
  12. data/config/flay.yml +3 -0
  13. data/config/flog.yml +2 -0
  14. data/config/heckle.yml +9 -0
  15. data/config/roodi.yml +20 -0
  16. data/config/site.reek +95 -0
  17. data/config/yardstick.yml +2 -0
  18. data/lib/auom.rb +287 -0
  19. data/lib/auom/algebra.rb +129 -0
  20. data/lib/auom/equalization.rb +90 -0
  21. data/lib/auom/inspection.rb +103 -0
  22. data/lib/auom/version.rb +3 -0
  23. data/spec/rcov.opts +7 -0
  24. data/spec/shared/command_method_behavior.rb +7 -0
  25. data/spec/shared/each_method_behaviour.rb +15 -0
  26. data/spec/shared/hash_method_behavior.rb +17 -0
  27. data/spec/shared/idempotent_method_behavior.rb +7 -0
  28. data/spec/shared/incompatible_operation_behavior.rb +5 -0
  29. data/spec/shared/invertible_method_behaviour.rb +9 -0
  30. data/spec/shared/operation_behavior.rb +12 -0
  31. data/spec/shared/sunits_shared.rb +6 -0
  32. data/spec/spec.opts +3 -0
  33. data/spec/spec_helper.rb +13 -0
  34. data/spec/unit/auom/add_spec.rb +0 -0
  35. data/spec/unit/auom/algebra/add_spec.rb +59 -0
  36. data/spec/unit/auom/algebra/divide_spec.rb +74 -0
  37. data/spec/unit/auom/algebra/multiply_spec.rb +74 -0
  38. data/spec/unit/auom/algebra/substract_spec.rb +59 -0
  39. data/spec/unit/auom/equalization/eql_spec.rb +40 -0
  40. data/spec/unit/auom/equalization/equal_value_spec.rb +57 -0
  41. data/spec/unit/auom/equalization/hash_spec.rb +13 -0
  42. data/spec/unit/auom/inspection/class_methods/prettify_unit_part_spec.rb +27 -0
  43. data/spec/unit/auom/inspection/inspect_spec.rb +41 -0
  44. data/spec/unit/auom/unit/class_methods/convert_spec.rb +35 -0
  45. data/spec/unit/auom/unit/class_methods/lookup_spec.rb +21 -0
  46. data/spec/unit/auom/unit/class_methods/new_spec.rb +156 -0
  47. data/spec/unit/auom/unit/class_methods/try_convert_spec.rb +31 -0
  48. data/spec/unit/auom/unit/class_methods/units_spec.rb +11 -0
  49. data/spec/unit/auom/unit/denominators_spec.rb +16 -0
  50. data/spec/unit/auom/unit/numerators_spec.rb +16 -0
  51. data/spec/unit/auom/unit/scalar_spec.rb +16 -0
  52. data/spec/unit/auom/unit/unit_spec.rb +16 -0
  53. data/spec/unit/auom/unit/unitless_spec.rb +18 -0
  54. metadata +164 -0
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe AUOM::Unit,'.convert' do
4
+ subject { object.convert(value) }
5
+
6
+ let(:object) { AUOM::Unit }
7
+
8
+ context 'with nil' do
9
+ let(:value) { nil }
10
+
11
+ it 'should raise error' do
12
+ expect { subject }.to raise_error(ArgumentError,'Cannot convert nil to AUOM::Unit')
13
+ end
14
+ end
15
+
16
+ context 'with fixnum' do
17
+ let(:value) { 1 }
18
+
19
+ it { should eql(AUOM::Unit.new(1)) }
20
+ end
21
+
22
+ context 'with rational' do
23
+ let(:value) { Rational(2,1) }
24
+
25
+ it { should eql(AUOM::Unit.new(2)) }
26
+ end
27
+
28
+ context 'with Object' do
29
+ let(:value) { Object.new }
30
+
31
+ it 'should raise error' do
32
+ expect { subject }.to raise_error(ArgumentError,"Cannot convert #{value.inspect} to AUOM::Unit")
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ describe AUOM::Unit,'.lookup' do
4
+ subject { object.send(:lookup,value) }
5
+
6
+ let(:object) { described_class }
7
+
8
+ context 'with existing symbol' do
9
+ let(:value) { :meter }
10
+
11
+ it { should eql([1,:meter]) }
12
+ end
13
+
14
+ context 'with inexisting symbol' do
15
+ let(:value) { :foo }
16
+
17
+ it 'should raise error' do
18
+ expect { subject }.to raise_error(ArgumentError,'Unknown unit :foo')
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,156 @@
1
+ require 'spec_helper'
2
+
3
+ describe AUOM::Unit,'#new' do
4
+ let(:object) { described_class }
5
+
6
+ subject do
7
+ described_class.new(*arguments)
8
+ end
9
+
10
+ shared_examples_for 'invalid unit' do
11
+ it 'should raise ArgumentError' do
12
+ expect { subject }.to raise_error(ArgumentError)
13
+ end
14
+ end
15
+
16
+ let(:expected_scalar) { 1 }
17
+ let(:expected_numerators) { [] }
18
+ let(:expected_denominators) { [] }
19
+
20
+ shared_examples_for 'valid unit' do
21
+ it { should be_frozen }
22
+
23
+ its(:scalar) { should == expected_scalar }
24
+ its(:scalar) { should be_frozen }
25
+ its(:numerators) { should == expected_numerators }
26
+ its(:numerators) { should be_frozen }
27
+ its(:denominators) { should == expected_denominators }
28
+ its(:denominators) { should be_frozen }
29
+ end
30
+
31
+ describe 'without arguments' do
32
+ let(:arguments) { [] }
33
+ it_should_behave_like 'invalid unit'
34
+ end
35
+
36
+ describe 'with one scalar argument' do
37
+ let(:arguments) { [argument] }
38
+
39
+ context 'when scalar is a string' do
40
+ let(:argument) { '10.31' }
41
+ it 'should raise error' do
42
+ expect { subject }.to raise_error(ArgumentError,'"10.31" cannot be converted to rational')
43
+ end
44
+ end
45
+
46
+ context 'when argument is a Fixnum' do
47
+ let(:argument) { 1 }
48
+
49
+ it_should_behave_like 'unitless unit'
50
+ it_should_behave_like 'valid unit'
51
+ end
52
+
53
+ context 'when argument is a Rational' do
54
+ let(:argument) { Rational(1,1) }
55
+
56
+ it_should_behave_like 'unitless unit'
57
+ it_should_behave_like 'valid unit'
58
+ end
59
+
60
+ context 'when argument is something else' do
61
+ let(:argument) { 1.0 }
62
+
63
+ it_should_behave_like 'invalid unit'
64
+ end
65
+ end
66
+
67
+ describe 'with scalar and numerator argument' do
68
+ let(:arguments) { [1,argument] }
69
+
70
+ context 'when argument is a valid unit' do
71
+ let(:argument) { :kilogramm }
72
+ let(:expected_numerators) { [:kilogramm] }
73
+
74
+ it_should_behave_like 'valid unit'
75
+ end
76
+
77
+ context 'when argument is a valid unit alias' do
78
+ let(:argument) { :kilometer }
79
+ let(:expected_numerators) { [:meter] }
80
+ let(:expected_scalar) { 1000 }
81
+
82
+ it_should_behave_like 'valid unit'
83
+ end
84
+
85
+ context 'when argument is an array of valid units' do
86
+ let(:argument) { [:kilogramm,:meter] }
87
+ let(:expected_numerators) { [:kilogramm,:meter] }
88
+
89
+ it_should_behave_like 'valid unit'
90
+ end
91
+
92
+ context 'when argument is an array with invalid unit' do
93
+ let(:argument) { [:kilogramm,:nonsense] }
94
+
95
+ it_should_behave_like 'invalid unit'
96
+ end
97
+
98
+ context 'when argument is an invalid unit' do
99
+ let(:argument) { :nonsense }
100
+
101
+ it_should_behave_like 'invalid unit'
102
+ end
103
+ end
104
+
105
+ describe 'with scalar, numerator and denominator argument' do
106
+ let(:arguments) { [1,:kilogramm,argument] }
107
+ let(:expected_numerators) { [:kilogramm] }
108
+
109
+ context 'when argument is a valid unit' do
110
+ let(:argument) { :meter }
111
+ let(:expected_denominators) { [:meter] }
112
+
113
+ it_should_behave_like 'valid unit'
114
+ end
115
+
116
+ context 'when argument is a valid unit alias' do
117
+ let(:argument) { :kilometer }
118
+
119
+ let(:expected_denominators) { [:meter] }
120
+ let(:expected_scalar) { Rational(1,1000) }
121
+
122
+ it_should_behave_like 'valid unit'
123
+ end
124
+
125
+
126
+ context 'when argument is an array of valid units' do
127
+ let(:argument) { [:euro,:meter] }
128
+ let(:expected_denominators) { [:euro,:meter] }
129
+
130
+ it_should_behave_like 'valid unit'
131
+ end
132
+
133
+ context 'when argument is an array with invalid unit' do
134
+ let(:argument) { [:euro,:nonsense] }
135
+
136
+ it_should_behave_like 'invalid unit'
137
+ end
138
+
139
+ context 'when argument is an invalid unit' do
140
+ let(:argument) { :nonsense }
141
+
142
+ it_should_behave_like 'invalid unit'
143
+ end
144
+ end
145
+
146
+ context 'when numerators and denominators overlap' do
147
+ let(:arguments) { [1,numerators,denominators] }
148
+ let(:numerators) { [:kilogramm,:meter,:euro] }
149
+ let(:denominators) { [:meter,:meter] }
150
+
151
+ let(:expected_numerators) { [:euro,:kilogramm] }
152
+ let(:expected_denominators) { [:meter] }
153
+
154
+ it_should_behave_like 'valid unit'
155
+ end
156
+ end
@@ -0,0 +1,31 @@
1
+ require 'spec_helper'
2
+
3
+ describe AUOM,'.try_convert' do
4
+ subject { object.try_convert(value) }
5
+
6
+ let(:object) { AUOM::Unit }
7
+
8
+ context 'with nil' do
9
+ let(:value) { nil }
10
+
11
+ it { should be(nil) }
12
+ end
13
+
14
+ context 'with fixnum' do
15
+ let(:value) { 1 }
16
+
17
+ it { should eql(AUOM::Unit.new(1)) }
18
+ end
19
+
20
+ context 'with rational' do
21
+ let(:value) { Rational(2,1) }
22
+
23
+ it { should eql(AUOM::Unit.new(2)) }
24
+ end
25
+
26
+ context 'with Object' do
27
+ let(:value) { Object.new }
28
+
29
+ it { should be(nil) }
30
+ end
31
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe AUOM::Unit,'.units' do
4
+ subject { object.units }
5
+
6
+ let(:object) { described_class }
7
+
8
+ it { should be_a(Hash) }
9
+
10
+ it_should_behave_like 'an idempotent method'
11
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe AUOM::Unit,'#denominators' do
4
+ subject { object.denominators }
5
+
6
+ let(:object) { described_class.new(1,*unit) }
7
+ let(:unit) { [[:meter],[:euro]] }
8
+
9
+ it 'should return denominators' do
10
+ should eql([:euro])
11
+ end
12
+
13
+ it { should be_frozen }
14
+
15
+ it_should_behave_like 'an idempotent method'
16
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe AUOM::Unit,'#numerators' do
4
+ subject { object.numerators }
5
+
6
+ let(:object) { described_class.new(1,*unit) }
7
+ let(:unit) { [[:meter],[:euro]] }
8
+
9
+ it 'should return numerators' do
10
+ should eql([:meter])
11
+ end
12
+
13
+ it { should be_frozen }
14
+
15
+ it_should_behave_like 'an idempotent method'
16
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe AUOM::Unit,'#denominators' do
4
+ subject { object.scalar }
5
+
6
+ let(:object) { described_class.new(scalar) }
7
+ let(:scalar) { Rational(1,2) }
8
+
9
+ it 'should return scalar' do
10
+ should eql(scalar)
11
+ end
12
+
13
+ it { should be_frozen }
14
+
15
+ it_should_behave_like 'an idempotent method'
16
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe AUOM::Unit,'#unit' do
4
+ subject { object.unit }
5
+
6
+ let(:object) { described_class.new(1,*unit) }
7
+ let(:unit) { [[:meter],[:euro]] }
8
+
9
+ it 'should return unit of unit instance' do
10
+ should eql(unit)
11
+ end
12
+
13
+ it { should be_frozen }
14
+
15
+ it_should_behave_like 'an idempotent method'
16
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe AUOM::Unit,'#unitless?' do
4
+ subject { object.unitless? }
5
+
6
+ let(:object) { described_class.new(1,*unit) }
7
+
8
+ context 'when unit is unitless' do
9
+ let(:unit) { [] }
10
+ it { should be(true) }
11
+ end
12
+
13
+ context 'when unit is NOT unitless' do
14
+ let(:unit) { [:meter] }
15
+
16
+ it { should be(false) }
17
+ end
18
+ end
metadata ADDED
@@ -0,0 +1,164 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: auom
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 3
10
+ version: 0.0.3
11
+ platform: ruby
12
+ authors:
13
+ - Markus Schirp
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-11-19 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: backports
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 17
29
+ segments:
30
+ - 2
31
+ - 6
32
+ - 3
33
+ version: 2.6.3
34
+ type: :runtime
35
+ version_requirements: *id001
36
+ description:
37
+ email: mbj@seonic.net
38
+ executables: []
39
+
40
+ extensions: []
41
+
42
+ extra_rdoc_files:
43
+ - TODO
44
+ - LICENSE
45
+ files:
46
+ - .gitignore
47
+ - .rspec
48
+ - .travis.yml
49
+ - Gemfile
50
+ - Gemfile.devtools
51
+ - Guardfile
52
+ - LICENSE
53
+ - README.md
54
+ - Rakefile
55
+ - TODO
56
+ - auom.gemspec
57
+ - config/flay.yml
58
+ - config/flog.yml
59
+ - config/heckle.yml
60
+ - config/roodi.yml
61
+ - config/site.reek
62
+ - config/yardstick.yml
63
+ - lib/auom.rb
64
+ - lib/auom/algebra.rb
65
+ - lib/auom/equalization.rb
66
+ - lib/auom/inspection.rb
67
+ - lib/auom/version.rb
68
+ - spec/rcov.opts
69
+ - spec/shared/command_method_behavior.rb
70
+ - spec/shared/each_method_behaviour.rb
71
+ - spec/shared/hash_method_behavior.rb
72
+ - spec/shared/idempotent_method_behavior.rb
73
+ - spec/shared/incompatible_operation_behavior.rb
74
+ - spec/shared/invertible_method_behaviour.rb
75
+ - spec/shared/operation_behavior.rb
76
+ - spec/shared/sunits_shared.rb
77
+ - spec/spec.opts
78
+ - spec/spec_helper.rb
79
+ - spec/unit/auom/add_spec.rb
80
+ - spec/unit/auom/algebra/add_spec.rb
81
+ - spec/unit/auom/algebra/divide_spec.rb
82
+ - spec/unit/auom/algebra/multiply_spec.rb
83
+ - spec/unit/auom/algebra/substract_spec.rb
84
+ - spec/unit/auom/equalization/eql_spec.rb
85
+ - spec/unit/auom/equalization/equal_value_spec.rb
86
+ - spec/unit/auom/equalization/hash_spec.rb
87
+ - spec/unit/auom/inspection/class_methods/prettify_unit_part_spec.rb
88
+ - spec/unit/auom/inspection/inspect_spec.rb
89
+ - spec/unit/auom/unit/class_methods/convert_spec.rb
90
+ - spec/unit/auom/unit/class_methods/lookup_spec.rb
91
+ - spec/unit/auom/unit/class_methods/new_spec.rb
92
+ - spec/unit/auom/unit/class_methods/try_convert_spec.rb
93
+ - spec/unit/auom/unit/class_methods/units_spec.rb
94
+ - spec/unit/auom/unit/denominators_spec.rb
95
+ - spec/unit/auom/unit/numerators_spec.rb
96
+ - spec/unit/auom/unit/scalar_spec.rb
97
+ - spec/unit/auom/unit/unit_spec.rb
98
+ - spec/unit/auom/unit/unitless_spec.rb
99
+ homepage: http://github.com/mbj/auom
100
+ licenses: []
101
+
102
+ post_install_message:
103
+ rdoc_options: []
104
+
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ hash: 3
113
+ segments:
114
+ - 0
115
+ version: "0"
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ hash: 3
122
+ segments:
123
+ - 0
124
+ version: "0"
125
+ requirements: []
126
+
127
+ rubyforge_project:
128
+ rubygems_version: 1.8.24
129
+ signing_key:
130
+ specification_version: 3
131
+ summary: Algebra (with) Units Of Measurement
132
+ test_files:
133
+ - spec/rcov.opts
134
+ - spec/shared/command_method_behavior.rb
135
+ - spec/shared/each_method_behaviour.rb
136
+ - spec/shared/hash_method_behavior.rb
137
+ - spec/shared/idempotent_method_behavior.rb
138
+ - spec/shared/incompatible_operation_behavior.rb
139
+ - spec/shared/invertible_method_behaviour.rb
140
+ - spec/shared/operation_behavior.rb
141
+ - spec/shared/sunits_shared.rb
142
+ - spec/spec.opts
143
+ - spec/spec_helper.rb
144
+ - spec/unit/auom/add_spec.rb
145
+ - spec/unit/auom/algebra/add_spec.rb
146
+ - spec/unit/auom/algebra/divide_spec.rb
147
+ - spec/unit/auom/algebra/multiply_spec.rb
148
+ - spec/unit/auom/algebra/substract_spec.rb
149
+ - spec/unit/auom/equalization/eql_spec.rb
150
+ - spec/unit/auom/equalization/equal_value_spec.rb
151
+ - spec/unit/auom/equalization/hash_spec.rb
152
+ - spec/unit/auom/inspection/class_methods/prettify_unit_part_spec.rb
153
+ - spec/unit/auom/inspection/inspect_spec.rb
154
+ - spec/unit/auom/unit/class_methods/convert_spec.rb
155
+ - spec/unit/auom/unit/class_methods/lookup_spec.rb
156
+ - spec/unit/auom/unit/class_methods/new_spec.rb
157
+ - spec/unit/auom/unit/class_methods/try_convert_spec.rb
158
+ - spec/unit/auom/unit/class_methods/units_spec.rb
159
+ - spec/unit/auom/unit/denominators_spec.rb
160
+ - spec/unit/auom/unit/numerators_spec.rb
161
+ - spec/unit/auom/unit/scalar_spec.rb
162
+ - spec/unit/auom/unit/unit_spec.rb
163
+ - spec/unit/auom/unit/unitless_spec.rb
164
+ has_rdoc: