unit 0.2.1 → 0.3.0

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.
File without changes
File without changes
@@ -91,85 +91,65 @@ units:
91
91
  sym: kat
92
92
  def: mole / second
93
93
 
94
- prefixes:
94
+ factors:
95
95
  # SI prefices
96
96
  yotta:
97
- sym: Y
98
- base: 10
99
- exp: 24
97
+ sym: Y
98
+ def: 10^24
100
99
  zetta:
101
- sym: Z
102
- base: 10
103
- exp: 21
100
+ sym: Z
101
+ def: 10^21
104
102
  exa:
105
- sym: E
106
- base: 10
107
- exp: 18
103
+ sym: E
104
+ def: 10^18
108
105
  peta:
109
- sym: P
110
- base: 10
111
- exp: 15
106
+ sym: P
107
+ def: 10^15
112
108
  tera:
113
- sym: T
114
- base: 10
115
- exp: 12
109
+ sym: T
110
+ def: 10^12
116
111
  giga:
117
- sym: G
118
- base: 10
119
- exp: 9
112
+ sym: G
113
+ def: 10^9
120
114
  mega:
121
- sym: M
122
- base: 10
123
- exp: 6
115
+ sym: M
116
+ def: 10^6
124
117
  kilo:
125
- sym: k
126
- base: 10
127
- exp: 3
118
+ sym: k
119
+ def: 10^3
128
120
  hecto:
129
- sym: h
130
- base: 10
131
- exp: 2
121
+ sym: h
122
+ def: 10^2
132
123
  deca:
133
- sym: da
134
- base: 10
135
- exp: 1
124
+ sym: da
125
+ def: 10^1
136
126
  deci:
137
- sym: d
138
- base: 10
139
- exp: -1
127
+ sym: d
128
+ def: 10^-1
140
129
  centi:
141
- sym: c
142
- base: 10
143
- exp: -2
130
+ sym: c
131
+ def: 10^-2
144
132
  milli:
145
- sym: m
146
- base: 10
147
- exp: -3
133
+ sym: m
134
+ def: 10^-3
148
135
  micro:
149
- sym: µ
150
- base: 10
151
- exp: -6
136
+ sym: µ
137
+ def: 10^-6
152
138
  nano:
153
- sym: n
154
- base: 10
155
- exp: -9
139
+ sym: n
140
+ def: 10^-9
156
141
  pico:
157
- sym: p
158
- base: 10
159
- exp: -12
142
+ sym: p
143
+ def: 10^-12
160
144
  femto:
161
- sym: f
162
- base: 10
163
- exp: -15
145
+ sym: f
146
+ def: 10^-15
164
147
  atto:
165
- sym: a
166
- base: 10
167
- exp: -18
148
+ sym: a
149
+ def: 10^-18
168
150
  zepto:
169
- sym: z
170
- base: 10
171
- exp: -21
151
+ sym: z
152
+ def: 10^-21
172
153
  yokto:
173
- sym: y
174
- base: 10
175
- exp: -24
154
+ sym: y
155
+ def: 10^-24
File without changes
@@ -0,0 +1,3 @@
1
+ class Unit < Numeric
2
+ VERSION = '0.3.0'
3
+ end
@@ -0,0 +1,16 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'bacon'
3
+ require 'unit'
4
+ require 'unit/dsl'
5
+
6
+ describe "Errors" do
7
+ describe "TypeError when adding incompatible units" do
8
+ it "should have a nice error message" do
9
+ unit_1 = Unit(1, "meter")
10
+ unit_2 = Unit(1, "second")
11
+ lambda {
12
+ unit_1 + unit_2
13
+ }.should.raise(TypeError).message.should.equal("Incompatible units: #{unit_1.inspect} and #{unit_2.inspect}")
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,24 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'bacon'
3
+ require 'unit'
4
+ require 'unit/dsl'
5
+
6
+ describe "Unit" do
7
+ describe "#default_system" do
8
+ describe "#load" do
9
+ it "should load an IO object" do
10
+ test_file = File.join(File.dirname(__FILE__), "yml", "io.yml")
11
+ File.open(test_file) do |file|
12
+ Unit.default_system.load(file)
13
+ end
14
+ Unit(1, "pim").should.equal Unit(3.14159, "m")
15
+ end
16
+
17
+ it "should load a file" do
18
+ test_file = File.join(File.dirname(__FILE__), "yml", "filename.yml")
19
+ Unit.default_system.load(test_file)
20
+ Unit(2, "dzm").should.equal Unit(24, "m")
21
+ end
22
+ end
23
+ end
24
+ end
@@ -1,9 +1,11 @@
1
- # encoding: utf-8
1
+ # -*- coding: utf-8 -*-
2
+ require 'bacon'
2
3
  require 'unit'
4
+ require 'unit/dsl'
3
5
 
4
- Unit::System::DEFAULT.load(:scientific)
5
- Unit::System::DEFAULT.load(:imperial)
6
- Unit::System::DEFAULT.load(:misc)
6
+ Unit.default_system.load(:scientific)
7
+ Unit.default_system.load(:imperial)
8
+ Unit.default_system.load(:misc)
7
9
 
8
10
  describe 'Unit' do
9
11
  it 'should support multiplication' do
@@ -25,6 +27,11 @@ describe 'Unit' do
25
27
  (Unit(1, 'm') - Unit(1, 'cm')).should.equal Unit(99, 100, 'm')
26
28
  end
27
29
 
30
+ it "should support adding through zero" do
31
+ (Unit(0, "m") + Unit(1, "m")).should.equal Unit(1, "m")
32
+ (Unit(1, "m") + Unit(-1, "m") + Unit(1, "m")).should.equal Unit(1, "m")
33
+ end
34
+
28
35
  it 'should check unit compatiblity' do
29
36
  should.raise TypeError do
30
37
  (Unit(42, 'm') + Unit(1, 's'))
@@ -32,6 +39,9 @@ describe 'Unit' do
32
39
  should.raise TypeError do
33
40
  (Unit(42, 'g') + Unit(1, 'm'))
34
41
  end
42
+ should.raise TypeError do
43
+ (Unit(0, 'g') + Unit(1, 'm'))
44
+ end
35
45
  end
36
46
 
37
47
  it 'should support exponentiation' do
@@ -95,15 +105,40 @@ describe 'Unit' do
95
105
  end
96
106
 
97
107
  it 'should have dimensionless? method' do
108
+ 100.meter_per_km.should.be.dimensionless
98
109
  100.meter.per_km.should.be.dimensionless
99
110
  100.meter.per_km.should.be.unitless
100
111
  42.meter.per_second.should.not.be.unitless
101
- 100.meter.per_km.should.equal Rational(1, 10).to_unit
112
+ 100.meter.per_km.should.equal Unit(Rational(1, 10))
102
113
  end
103
114
 
104
115
  it 'should be equal to rational if dimensionless' do
105
116
  100.meter.per_km.should.equal Rational(1, 10)
106
117
  100.meter.per_km.approx.should.equal 0.1
107
118
  end
119
+
120
+ it 'should be comparable' do
121
+ Unit(1,'m').should < Unit(2,'m')
122
+ Unit(1,'m').should <= Unit(2,'m')
123
+
124
+ Unit(1,'m').should <= Unit(1,'m')
125
+ Unit(1,'m').should >= Unit(1,'m')
126
+
127
+ Unit(1,'m').should > Unit(0,'m')
128
+ Unit(1,'m').should >= Unit(0,'m')
129
+
130
+ Unit(100, "m").should < Unit(1, "km")
131
+ Unit(100, "m").should > Unit(0.0001, "km")
132
+ end
133
+
134
+ it "should fail comparison on differing units" do
135
+ lambda do
136
+ Unit(1, "second") > Unit(1, "meter")
137
+ end.should.raise(ArgumentError)
138
+ end
139
+
140
+ it "should keep units when the value is zero" do
141
+ Unit(0, "m").unit.should.equal [[:one, :meter, 1]]
142
+ end
108
143
  end
109
144
 
@@ -0,0 +1,4 @@
1
+ units:
2
+ dozen_meter:
3
+ sym: dzm
4
+ def: 12 m
@@ -0,0 +1,4 @@
1
+ units:
2
+ pi_meter:
3
+ sym: pim
4
+ def: 3.14159 m
@@ -1,5 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
- require File.dirname(__FILE__) + '/lib/unit'
2
+ require File.dirname(__FILE__) + '/lib/unit/version'
3
3
 
4
4
  Gem::Specification.new do |s|
5
5
  s.name = %q{unit}
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: unit
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.1
5
+ version: 0.3.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Daniel Mendler
@@ -47,18 +47,28 @@ extra_rdoc_files: []
47
47
  files:
48
48
  - .travis.yml
49
49
  - Gemfile
50
+ - LICENSE
50
51
  - README.markdown
51
52
  - Rakefile
52
- - lib/systems/binary.yml
53
- - lib/systems/degree.yml
54
- - lib/systems/imperial.yml
55
- - lib/systems/misc.yml
56
- - lib/systems/scientific.yml
57
- - lib/systems/si.yml
58
- - lib/systems/time.yml
59
53
  - lib/unit.rb
60
- - lib/units.rb
54
+ - lib/unit/class.rb
55
+ - lib/unit/compatibility.rb
56
+ - lib/unit/constructor.rb
57
+ - lib/unit/dsl.rb
58
+ - lib/unit/system.rb
59
+ - lib/unit/systems/binary.yml
60
+ - lib/unit/systems/degree.yml
61
+ - lib/unit/systems/imperial.yml
62
+ - lib/unit/systems/misc.yml
63
+ - lib/unit/systems/scientific.yml
64
+ - lib/unit/systems/si.yml
65
+ - lib/unit/systems/time.yml
66
+ - lib/unit/version.rb
67
+ - test/error_test.rb
68
+ - test/system_test.rb
61
69
  - test/unit_test.rb
70
+ - test/yml/filename.yml
71
+ - test/yml/io.yml
62
72
  - unit.gemspec
63
73
  has_rdoc: true
64
74
  homepage: http://github.com/minad/unit
@@ -1,41 +0,0 @@
1
- units:
2
- bit:
3
- sym: bit
4
- def: bit
5
- byte:
6
- sym: B
7
- def: 8 * bit
8
-
9
- prefixes:
10
- kibi:
11
- sym: Ki
12
- base: 2
13
- exp: 10
14
- mebi:
15
- sym: Mi
16
- base: 2
17
- exp: 20
18
- gibi:
19
- sym: Gi
20
- base: 2
21
- exp: 30
22
- tebi:
23
- sym: Ti
24
- base: 2
25
- exp: 40
26
- pebi:
27
- sym: Pi
28
- base: 2
29
- exp: 50
30
- exbi:
31
- sym: Ei
32
- base: 2
33
- exp: 60
34
- zebi:
35
- sym: Zi
36
- base: 2
37
- exp: 70
38
- yobi:
39
- sym: Yi
40
- base: 2
41
- exp: 80
@@ -1 +0,0 @@
1
- require 'unit'