unite 1.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.
Files changed (46) hide show
  1. data/.gitignore +3 -0
  2. data/.rspec +4 -0
  3. data/Gemfile +18 -0
  4. data/Gemfile.lock +89 -0
  5. data/Guardfile +15 -0
  6. data/MIT-LICENSE +20 -0
  7. data/README +0 -0
  8. data/Rakefile +3 -0
  9. data/lib/unity/arithmetic.rb +64 -0
  10. data/lib/unity/comparison.rb +33 -0
  11. data/lib/unity/conversion.rb +46 -0
  12. data/lib/unity/dimension/integer.rb +34 -0
  13. data/lib/unity/dimension/vector.rb +43 -0
  14. data/lib/unity/dimension.rb +37 -0
  15. data/lib/unity/fraction.rb +118 -0
  16. data/lib/unity/lookup/definitions.rb +50 -0
  17. data/lib/unity/lookup/derived_unit.rb +24 -0
  18. data/lib/unity/lookup/property.rb +21 -0
  19. data/lib/unity/lookup/simple_unit.rb +26 -0
  20. data/lib/unity/lookup.rb +102 -0
  21. data/lib/unity/quantity.rb +20 -0
  22. data/lib/unity/version.rb +3 -0
  23. data/lib/unity.rb +20 -0
  24. data/spec/arithmetic_spec.rb +6 -0
  25. data/spec/comparison_spec.rb +6 -0
  26. data/spec/conversion_spec.rb +8 -0
  27. data/spec/dimension/integer_spec.rb +7 -0
  28. data/spec/dimension/vector_spec.rb +7 -0
  29. data/spec/dimension_spec.rb +7 -0
  30. data/spec/fabricators/unit_fabricator.rb +14 -0
  31. data/spec/fraction_spec.rb +7 -0
  32. data/spec/lookup/derived_unit_spec.rb +44 -0
  33. data/spec/lookup/property_spec.rb +18 -0
  34. data/spec/lookup/simple_unit_spec.rb +41 -0
  35. data/spec/lookup_spec.rb +135 -0
  36. data/spec/quantity_spec.rb +16 -0
  37. data/spec/spec_helper.rb +28 -0
  38. data/spec/support/load_debugger.rb +7 -0
  39. data/spec/support/shared_examples/units/arithmetic.rb +127 -0
  40. data/spec/support/shared_examples/units/comparison.rb +69 -0
  41. data/spec/support/shared_examples/units/conversion.rb +49 -0
  42. data/spec/support/shared_examples/units/dimension/integer.rb +41 -0
  43. data/spec/support/shared_examples/units/dimension/vector.rb +10 -0
  44. data/spec/support/shared_examples/units/fractions.rb +131 -0
  45. data/unite.gemspec +24 -0
  46. metadata +133 -0
@@ -0,0 +1,69 @@
1
+ # -*- encoding : utf-8 -*-
2
+ shared_examples_for "comparable unit" do
3
+ let(:comparible1) { described_class.new :expression => expression1 }
4
+ let(:comparible2) { described_class.new :expression => expression2 }
5
+
6
+ describe "#compatible?" do
7
+ subject { comparible1.compatible? comparible2 }
8
+
9
+ context "with compatible units" do
10
+ let(:expression1) { "km" }
11
+ let(:expression2) { "m" }
12
+
13
+ it { should be_true }
14
+ end
15
+
16
+
17
+ context "with non compatible units" do
18
+ let(:expression1) { "km" }
19
+ let(:expression2) { "g" }
20
+
21
+ it { should be_false }
22
+ end
23
+ end
24
+
25
+ describe "#compatible!" do
26
+ subject { comparible1.compatible! comparible2 }
27
+
28
+ context "with compatible units" do
29
+ let(:expression1) { "km" }
30
+ let(:expression2) { "m" }
31
+
32
+ it { should be_true }
33
+ end
34
+
35
+
36
+ context "with non compatible units" do
37
+ let(:expression1) { "km" }
38
+ let(:expression2) { "g" }
39
+
40
+ it "should raise exception" do
41
+ lambda { subject }.should raise_exception Unity::IncompatibleError
42
+ end
43
+ end
44
+ end
45
+
46
+ describe "equality #==" do
47
+ subject { comparible1 == comparible2 }
48
+
49
+ context "with comparible units" do
50
+ let(:expression1) { "1*km" }
51
+
52
+ context "and same value" do
53
+ let(:expression2) { "1000*m" }
54
+ it { should be_true }
55
+ end
56
+
57
+ context "and different value" do
58
+ let(:expression2) { "1*m" }
59
+ it { should be_false }
60
+ end
61
+ end
62
+
63
+ context "with non comparible units" do
64
+ let(:expression1) { "1*m" }
65
+ let(:expression2) { "1*g" }
66
+ it { should be_false }
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,49 @@
1
+ # -*- encoding : utf-8 -*-
2
+ shared_examples "convertable value" do
3
+
4
+ describe "converted unit" do
5
+ let(:original) { described_class.new :expression => from }
6
+ let(:final) { described_class.new :expression => to }
7
+ subject { original.convert_to(final) }
8
+
9
+ context "with compatible units" do
10
+ let(:from) { '2*km' }
11
+ let(:to) { 'm' }
12
+
13
+ its(:value) { should == 2000.0 }
14
+ its(:unit) { should == 'm' }
15
+ end
16
+
17
+ context "with exponents on units" do
18
+ let(:from) { '1000000*m^2' }
19
+ let(:to) { 'km^2' }
20
+
21
+ its(:value) { should == 1.0 }
22
+ its(:unit) { should == 'km^2' }
23
+ end
24
+
25
+ context "with non compatible units" do
26
+ let(:from) { '1000000*m' }
27
+ let(:to) { 'km^2' }
28
+ it { should be_nil }
29
+ end
30
+
31
+ context "with derived units" do
32
+ let(:from) { '3.6e7*J' }
33
+ let(:to) { 'kWh' }
34
+
35
+ its(:value) { should == 10.0 }
36
+ its(:unit) { should == 'kWh' }
37
+ end
38
+
39
+ context "with value on the to" do
40
+ let(:from) { '2*km' }
41
+ let(:to) { '10*m' }
42
+
43
+ its(:value) { should == 2000.0 }
44
+ its(:unit) { should == 'm' }
45
+ end
46
+ end
47
+
48
+
49
+ end
@@ -0,0 +1,41 @@
1
+ # -*- encoding : utf-8 -*-
2
+ shared_examples "dimension integer" do
3
+ subject { described_class.new }
4
+ describe "#dimension_int" do
5
+ let(:dimension_vector) { [1,-2,0,0,0,0,0,0] }
6
+ before do
7
+ subject.stubs(:dimension_vector).returns(dimension_vector)
8
+ end
9
+
10
+ its(:dimension_int) { should == -39 }
11
+ end
12
+
13
+ describe "#property" do
14
+ let(:dimension_int) { 10 }
15
+ let(:property_stub) { stub }
16
+ before do
17
+ subject.stubs(:dimension_int => dimension_int)
18
+ Unity::Lookup.stubs(:find_property).with(dimension_int).returns(property_stub)
19
+ end
20
+ its(:property) { should == property_stub }
21
+ end
22
+
23
+ describe "#property_name" do
24
+ before do
25
+ subject.stubs(:property => property_stub)
26
+ end
27
+ context "with existing property" do
28
+ let(:property_name) { :yo }
29
+ let(:property_stub) { stub(:name => property_name) }
30
+
31
+ its(:property_name) { should == property_name }
32
+ end
33
+
34
+ context "with non existing property" do
35
+ let(:property_stub) { nil }
36
+
37
+ its(:property_name) { should == nil }
38
+ end
39
+ end
40
+
41
+ end
@@ -0,0 +1,10 @@
1
+ # -*- encoding : utf-8 -*-
2
+ shared_examples "dimension vectors" do
3
+ subject { described_class.new :expression => expression }
4
+
5
+ let(:expression) { "m/s^2" }
6
+ its(:dimension_vector) { should == [1,-2,0,0,0,0,0,0] }
7
+
8
+ it "should limit the powers of the vector to the max"
9
+
10
+ end
@@ -0,0 +1,131 @@
1
+ # -*- encoding : utf-8 -*-
2
+ shared_examples_for "unit fractions" do
3
+ context "fractions" do
4
+
5
+ subject { described_class.new :expression => expression }
6
+
7
+ describe "required methods" do
8
+ it { should respond_to(:value) }
9
+ it { should respond_to(:value=) }
10
+ it { should respond_to(:numerator) }
11
+ it { should respond_to(:numerator=) }
12
+ it { should respond_to(:denominator=) }
13
+ it { should respond_to(:denominator=) }
14
+ end
15
+
16
+ describe "#expression" do
17
+
18
+ context "with blank string" do
19
+ let(:expression) { '' }
20
+
21
+ its(:unit) { should == '' }
22
+ its(:value) { should == 0.0 }
23
+ its(:expression) { '0.0' }
24
+ end
25
+
26
+ context "with number only" do
27
+ let(:expression) { '5.7' }
28
+
29
+ its(:unit) { should == '' }
30
+ its(:value) { should == 5.7 }
31
+ its(:expression) { should == '5.7' }
32
+ end
33
+
34
+ context "with number and unit" do
35
+ let(:expression) { '10*m/5*s^2' }
36
+
37
+ its(:unit) { should == 'm/s^2' }
38
+ its(:value) { should == 2.0 }
39
+ its(:expression) { should == '2.0*m/s^2' }
40
+ end
41
+
42
+ context "with only unit" do
43
+ let(:expression) { 'm/s' }
44
+
45
+ its(:unit) { should == 'm/s' }
46
+ its(:value) { should == 1.0 }
47
+ its(:expression) { should == '1.0*m/s' }
48
+ end
49
+
50
+ context "with no numerator" do
51
+ let(:expression) { '10/s' }
52
+
53
+ its(:unit) { should == '1.0/s' }
54
+ its(:value) { should == 10.0 }
55
+ its(:expression) { should == '10.0/s' }
56
+ end
57
+
58
+ context 'with invalid format' do
59
+ let(:expression) { 'km/kg/s' }
60
+ it "should raise exception" do
61
+ lambda { subject }.should raise_exception Unity::InvalidFormat
62
+ end
63
+ end
64
+
65
+ context "with spaces" do
66
+ let(:expression) { '10 * km / 5 *h ' }
67
+
68
+ its(:unit) { should == 'km/h' }
69
+ its(:value) { should == 2.0 }
70
+ its(:expression) { should == '2.0*km/h' }
71
+ end
72
+
73
+ end
74
+
75
+ describe 'default' do
76
+ subject { described_class.new }
77
+ its(:unit) { should == '' }
78
+ its(:value) { should == 0.0 }
79
+ its(:denominator) { should == [] }
80
+ its(:numerator) { should == [] }
81
+ end
82
+
83
+ describe "exponents" do
84
+ let(:expression) { 'kg^2*m/s^3' }
85
+
86
+ its(:expanded_numerator) { should =~ ['kg','kg', 'm'] }
87
+ its(:expanded_denominator) { should =~ ['s', 's', 's' ] }
88
+ end
89
+
90
+ describe 'reduction of units' do
91
+
92
+ context 'when of the same type' do
93
+ let(:expression) { 'kg*s^2/s*kg^3' }
94
+
95
+ its(:unit) { should == 's/kg^2' }
96
+ its(:expression) { should == '1.0*s/kg^2' }
97
+ end
98
+
99
+ context "with empty numerator" do
100
+ let(:expression) { 'kg/kg^3' }
101
+
102
+ its(:unit) { should == '1.0/kg^2' }
103
+ its(:expression) { should == '1.0/kg^2' }
104
+ end
105
+
106
+ end
107
+
108
+ describe "inverse" do
109
+ subject { described_class.new(:expression => expression).inverse }
110
+
111
+ context "with unit ane value" do
112
+ let(:expression) { "2*km/s" }
113
+
114
+ its(:unit) { should == 's/km' }
115
+ its(:value) { should == 0.5 }
116
+ its(:expression) { should == '0.5*s/km' }
117
+ end
118
+
119
+ context "with blank expression" do
120
+ let(:expression) { "" }
121
+
122
+ its(:unit) { should == '' }
123
+ its(:value) { should == 0.0 }
124
+ its(:expression) { should == '0.0' }
125
+ end
126
+
127
+
128
+ end
129
+ end
130
+
131
+ end
data/unite.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "unity/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "unite"
7
+ s.version = Unity::VERSION
8
+ s.authors = ["stellard"]
9
+ s.email = ["scott.ellard@gmail.com"]
10
+ s.homepage = ""
11
+ s.summary = %q{Unite provides extensions to your objects to support values with units}
12
+ s.description = %q{Unite provides extensions to your objects to support values with units}
13
+
14
+ s.rubyforge_project = "unity"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency "activesupport", ">= 3.0.0"
22
+ s.add_dependency "activemodel", ">= 3.0.0"
23
+
24
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unite
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - stellard
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-11 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: &70284199592660 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70284199592660
25
+ - !ruby/object:Gem::Dependency
26
+ name: activemodel
27
+ requirement: &70284191477140 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: 3.0.0
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *70284191477140
36
+ description: Unite provides extensions to your objects to support values with units
37
+ email:
38
+ - scott.ellard@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - .rspec
45
+ - Gemfile
46
+ - Gemfile.lock
47
+ - Guardfile
48
+ - MIT-LICENSE
49
+ - README
50
+ - Rakefile
51
+ - lib/unity.rb
52
+ - lib/unity/arithmetic.rb
53
+ - lib/unity/comparison.rb
54
+ - lib/unity/conversion.rb
55
+ - lib/unity/dimension.rb
56
+ - lib/unity/dimension/integer.rb
57
+ - lib/unity/dimension/vector.rb
58
+ - lib/unity/fraction.rb
59
+ - lib/unity/lookup.rb
60
+ - lib/unity/lookup/definitions.rb
61
+ - lib/unity/lookup/derived_unit.rb
62
+ - lib/unity/lookup/property.rb
63
+ - lib/unity/lookup/simple_unit.rb
64
+ - lib/unity/quantity.rb
65
+ - lib/unity/version.rb
66
+ - spec/arithmetic_spec.rb
67
+ - spec/comparison_spec.rb
68
+ - spec/conversion_spec.rb
69
+ - spec/dimension/integer_spec.rb
70
+ - spec/dimension/vector_spec.rb
71
+ - spec/dimension_spec.rb
72
+ - spec/fabricators/unit_fabricator.rb
73
+ - spec/fraction_spec.rb
74
+ - spec/lookup/derived_unit_spec.rb
75
+ - spec/lookup/property_spec.rb
76
+ - spec/lookup/simple_unit_spec.rb
77
+ - spec/lookup_spec.rb
78
+ - spec/quantity_spec.rb
79
+ - spec/spec_helper.rb
80
+ - spec/support/load_debugger.rb
81
+ - spec/support/shared_examples/units/arithmetic.rb
82
+ - spec/support/shared_examples/units/comparison.rb
83
+ - spec/support/shared_examples/units/conversion.rb
84
+ - spec/support/shared_examples/units/dimension/integer.rb
85
+ - spec/support/shared_examples/units/dimension/vector.rb
86
+ - spec/support/shared_examples/units/fractions.rb
87
+ - unite.gemspec
88
+ homepage: ''
89
+ licenses: []
90
+ post_install_message:
91
+ rdoc_options: []
92
+ require_paths:
93
+ - lib
94
+ required_ruby_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project: unity
108
+ rubygems_version: 1.8.6
109
+ signing_key:
110
+ specification_version: 3
111
+ summary: Unite provides extensions to your objects to support values with units
112
+ test_files:
113
+ - spec/arithmetic_spec.rb
114
+ - spec/comparison_spec.rb
115
+ - spec/conversion_spec.rb
116
+ - spec/dimension/integer_spec.rb
117
+ - spec/dimension/vector_spec.rb
118
+ - spec/dimension_spec.rb
119
+ - spec/fabricators/unit_fabricator.rb
120
+ - spec/fraction_spec.rb
121
+ - spec/lookup/derived_unit_spec.rb
122
+ - spec/lookup/property_spec.rb
123
+ - spec/lookup/simple_unit_spec.rb
124
+ - spec/lookup_spec.rb
125
+ - spec/quantity_spec.rb
126
+ - spec/spec_helper.rb
127
+ - spec/support/load_debugger.rb
128
+ - spec/support/shared_examples/units/arithmetic.rb
129
+ - spec/support/shared_examples/units/comparison.rb
130
+ - spec/support/shared_examples/units/conversion.rb
131
+ - spec/support/shared_examples/units/dimension/integer.rb
132
+ - spec/support/shared_examples/units/dimension/vector.rb
133
+ - spec/support/shared_examples/units/fractions.rb