measure 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,54 +6,60 @@
6
6
 
7
7
  require 'measure'
8
8
 
9
- Measure.define_unit :meter, :length
10
- Measure.define_alias :m, :meter
11
-
12
- Measure.define_unit :kilo_meter, :length
13
- Measure.define_alias :km, :kilo_meter
14
-
15
- Measure.define_unit :centi_meter, :length
16
- Measure.define_alias :cm, :centi_meter
17
-
18
- Measure.define_unit :milli_meter, :length
19
- Measure.define_alias :mm, :milli_meter
20
-
21
- Measure.define_unit :micro_meter, :length
22
- Measure.define_alias :um, :micro_meter
23
-
24
- Measure.define_unit :nano_meter, :length
25
- Measure.define_alias :nm, :nano_meter
26
-
27
- Measure.define_unit :inch, :length
28
- Measure.define_alias :in, :inch
29
-
30
- Measure.define_unit :feet, :length
31
- Measure.define_alias :ft, :feet
32
-
33
- Measure.define_unit :yard, :length
34
- Measure.define_alias :yd, :yard
35
-
36
- Measure.define_conversion :m, :cm => 100, :mm => 1000, :um => 1000000, :nm => 1000000000
37
- Measure.define_conversion :km, :m => 1000
38
- Measure.define_conversion :cm, :mm => 10
39
- Measure.define_conversion :mm, :um => 1000
40
- Measure.define_conversion :um, :nm => 1000
41
- Measure.define_conversion :in, :m => 0.254, :cm => 2.54, :mm => 25.4
42
- Measure.define_conversion :ft, :in => 12
43
- Measure.define_conversion :yd, :in => 36, :ft => 3
44
-
45
- # for DTP
46
-
47
- Measure.define_unit :point, :length
48
- Measure.define_alias :pt, :point
49
- Measure.define_alias :didot_point, :point
50
- Measure.define_alias :dp, :didot_point
51
-
52
- Measure.define_unit :big_point, :length
53
- Measure.define_alias :bp, :big_point
54
-
55
- Measure.define_unit :pica, :length
56
- Measure.define_alias :pc, :pica
57
-
58
- Measure.define_conversion :in, :pt => 72.27, :bp => 72.0
59
- Measure.define_conversion :pc, :pt => 12
9
+ class Measure
10
+ {
11
+ :m => :meter,
12
+ :km => :kilo_meter,
13
+ :cm => :centi_meter,
14
+ :mm => :milli_meter,
15
+ :um => :micro_meter,
16
+ :nm => :nano_meter,
17
+ :in => :inch,
18
+ :ft => :feet,
19
+ :yd => :yard,
20
+ }.each do |a, u|
21
+ def_unit u, :length unless has_unit? u
22
+ def_alias a, u
23
+ end
24
+
25
+ def_conversion :m, :cm => 100, :mm => 1000, :um => 1000000, :nm => 1000000000
26
+ def_conversion :km, :m => 1000
27
+ def_conversion :cm, :mm => 10
28
+ def_conversion :mm, :um => 1000
29
+ def_conversion :um, :nm => 1000
30
+ def_conversion :in, :m => 0.254, :cm => 2.54, :mm => 25.4
31
+ def_conversion :ft, :in => 12
32
+ def_conversion :yd, :in => 36, :ft => 3
33
+
34
+ # for Physics
35
+
36
+ {
37
+ :aa => :angstrom,
38
+ :AU => :astronomical_unit,
39
+ :au => :astronomical_unit,
40
+ :ly => :light_year,
41
+ }.each do |a, u|
42
+ def_unit u, :length unless has_unit? u
43
+ def_alias a, u
44
+ end
45
+
46
+ def_conversion :m, :angstrom => 10000000000
47
+ def_conversion :AU, :m => 149_597_870_691
48
+ def_conversion :light_year, :m => 9_460_730_472_580_800
49
+
50
+ # for DTP
51
+
52
+ {
53
+ :pt => :point,
54
+ :didot_point => :point,
55
+ :dp => :didot_point,
56
+ :bp => :big_point,
57
+ :pc => :pica,
58
+ }.each do |a, u|
59
+ def_unit u, :length unless has_unit? u
60
+ def_alias a, u
61
+ end
62
+
63
+ def_conversion :in, :pt => 72.27, :bp => 72.0
64
+ def_conversion :pc, :pt => 12
65
+ end
@@ -22,7 +22,7 @@ module MeasureSupport
22
22
 
23
23
  def method_missing(name, *args)
24
24
  if MeasureSupport.enable?
25
- return Measure.new(self, name) if Measure.defined?(name)
25
+ return Measure.new(self, name) if Measure.has_unit?(name)
26
26
  end
27
27
  return measure_support_saved_method_missing(name, *args)
28
28
  end
@@ -31,7 +31,7 @@ module MeasureSupport
31
31
  end
32
32
 
33
33
  class << Measure
34
- def with_short_form
34
+ def short_form
35
35
  begin
36
36
  MeasureSupport.enable
37
37
  return yield
@@ -40,7 +40,17 @@ class << Measure
40
40
  end
41
41
  end
42
42
 
43
- alias form with_short_form
43
+ def enable_short_form
44
+ MeasureSupport.enable
45
+ end
46
+
47
+ def disable_short_form
48
+ MeasureSupport.disable
49
+ end
50
+
51
+ def short_form_available?
52
+ MeasureSupport.enable?
53
+ end
44
54
  end
45
55
 
46
56
  class Integer
@@ -6,7 +6,7 @@ class Measure
6
6
  module VERSION
7
7
  unless defined? MAJOR
8
8
  MAJOR = 0
9
- MINOR = 1
9
+ MINOR = 2
10
10
  TINY = 0
11
11
 
12
12
  STRING = [MAJOR, MINOR, TINY].join('.')
@@ -0,0 +1,43 @@
1
+ # = Definitions of the weight dimension units
2
+ #
3
+ # Author:: Kenta Murata
4
+ # Copyright:: Copyright (C) 2008 Kenta Murata
5
+ # License:: LGPL version 3.0
6
+
7
+ require 'measure'
8
+
9
+ class Measure
10
+ {
11
+ :kg => :kilo_gram,
12
+ :g => :gram,
13
+ :t => :ton,
14
+ :mg => :milli_gram,
15
+ :ug => :micro_gram,
16
+ :gr => :grain,
17
+ :oz => :ounce,
18
+ :oz_av => :ounce,
19
+ :oz_tr => :troy_ounce,
20
+ :toz => :troy_ounce,
21
+ :lb => :pound,
22
+ :lb_av => :pound,
23
+ :lb_tr => :troy_pound,
24
+ :kin => :catty,
25
+ :ryo => :tael,
26
+ }.each do |a, u|
27
+ def_unit u, :weight unless has_unit? u
28
+ def_alias a, u
29
+ end
30
+
31
+ def_conversion :kg, :g => 1_000, :mg => 1_000_000, :ug => 1_000_000_000
32
+ def_conversion :t, :kg => 1_000
33
+ def_conversion :gr, :mg => 64.79891
34
+ def_conversion :oz_av, :gr => 437.5
35
+ def_conversion :oz_tr, :gr => 480, :g => 31.1034768
36
+ def_conversion :lb_av, :gr => 7000, :oz => 16, :kg => 0.45359237
37
+ def_conversion :lb_tr, :gr => 5760, :oz => 12, :kg => 0.3732417216, :lb_av => 144.0/175.0
38
+
39
+ def_unit :momme, :weight
40
+ def_conversion :momme, :g => 3.75
41
+ def_conversion :kin, :momme => 160
42
+ def_conversion :kin, :ryo => 16
43
+ end
@@ -1,36 +1,141 @@
1
1
  require File.join(File.dirname(__FILE__), 'spec_helper')
2
2
  require 'measure'
3
3
 
4
- describe Measure do
4
+ describe Measure, 'with five pre-defined units in order to unit management' do
5
5
  before :each do
6
+ # Pre-define the following conversion scheme
7
+ #
8
+ # 2 5 10
9
+ # a <--- b ---> c <--- d
10
+ # |
11
+ # `----> e
12
+ # 10
13
+ #
14
+ Measure.define_unit :a, :test
15
+ Measure.define_unit :b, :test
16
+ Measure.define_unit :c, :test
17
+ Measure.define_unit :d, :test
18
+ Measure.define_unit :e, :test
19
+ Measure.define_conversion :b, :a => 2
20
+ Measure.define_conversion :b, :c => 5
21
+ Measure.define_conversion :b, :e => 10
22
+ Measure.define_conversion :d, :c => 10
6
23
  end
7
24
 
8
- it 'has no units when nothing is defined' do
9
- Measure.num_units.should be_equal 0
10
- Measure.units.should be_empty
25
+ it 'has five units' do
26
+ Measure.num_units.should == 5
11
27
  end
12
28
 
13
- it 'has the [meter] after this is defined' do
14
- Measure.define_unit :meter, :length
15
- Measure.should have_unit :meter
16
- Measure.should be_defined :meter
29
+ it 'has defined units and has not undefined units' do
30
+ Measure.should have_unit(:a)
31
+ Measure.should have_unit(:b)
32
+ Measure.should have_unit(:c)
33
+ Measure.should have_unit(:d)
34
+ Measure.should have_unit(:e)
35
+ Measure.should_not have_unit(:f)
36
+ Measure.should_not have_unit(:g)
37
+ Measure.should_not have_unit(:h)
17
38
  end
18
39
 
19
- it 'returns the number of defined units when call Measure.num_units' do
20
- Measure.define_unit :meter, :length
21
- Measure.define_unit :inch, :length
22
- Measure.num_units.should == 2
40
+ it 'can define an unit which has not been defined yet' do
41
+ lambda { Measure.define_unit :f, :test }.
42
+ should_not raise_error(Exception)
23
43
  end
24
44
 
25
- it 'raises Measure::UnitRedefinitionError ' +
26
- 'when the [meter] is defined twice with different dimensions' do
27
- Measure.define_unit :meter, :length
28
- lambda { Measure.define_unit :meter, :length }.
45
+ it 'can undefine a unit which has been already defined' do
46
+ Measure.define_unit :f, :test
47
+ Measure.should have_unit(:f)
48
+ lambda { Measure.undefine_unit :f }.
49
+ should_not raise_error(Exception)
50
+ Measure.should_not have_unit(:f)
51
+ end
52
+
53
+ it 'raises UnitRedefinitionError' +
54
+ ' when an unit is about to be redefined as a different dimension' do
55
+ lambda { Measure.define_unit :a, :other }.
56
+ should raise_error(Measure::UnitRedefinitionError)
57
+ end
58
+
59
+ it 'does not raise UnitRedefinitionError' +
60
+ ' when an unit is about to be redefined as a same dimension' do
61
+ lambda { Measure.define_unit :a, :test }.
29
62
  should_not raise_error(Measure::UnitRedefinitionError)
30
- lambda { Measure.define_unit :meter, :weight }.
63
+ end
64
+
65
+ it 'can define and undefine aliases' do
66
+ lambda { Measure.define_alias :alias_a, :a }.
67
+ should_not raise_error(Exception)
68
+ Measure.should have_unit(:alias_a)
69
+ lambda { Measure.undefine_unit :alias_a }.
70
+ should_not raise_error(Exception)
71
+ Measure.should_not have_unit(:alias_a)
72
+ end
73
+
74
+ it 'raises UnitRedefinitionError when the unit is redefined as an alias' do
75
+ lambda { Measure.define_alias :b, :a }.
31
76
  should raise_error(Measure::UnitRedefinitionError)
32
77
  end
33
78
 
79
+ it 'can define a scheme of conversion from an alias' do
80
+ Measure.define_alias :alias_a, :a
81
+ lambda { Measure.define_conversion :alias_a, :b => 10 }.
82
+ should_not raise_error(Exception)
83
+ Measure.should be_direct_compatible(:alias_a, :b)
84
+ end
85
+
86
+ it 'can convert between two connected units' +
87
+ ' through multiple direct conversions' do
88
+ Measure.find_conversion_route(:a, :d).should_not be_nil
89
+ Measure.find_conversion_route(:d, :e).should_not be_nil
90
+ end
91
+
92
+ it 'can define conversion by a Proc' do
93
+ Measure.define_unit :f, :test
94
+ lambda { Measure.define_conversion :f, :a => lambda {|x| x + 2 } }.
95
+ should_not raise_error(Exception)
96
+ end
97
+
98
+ it 'can not convert commutatively by a Proc ' do
99
+ Measure.define_unit :f, :test
100
+ lambda { Measure.define_conversion :f, :a => lambda {|x| x + 2 } }.
101
+ should_not raise_error(Exception)
102
+ Measure.should be_direct_compatible(:f, :a)
103
+ Measure.should_not be_direct_compatible(:a, :f)
104
+ end
105
+
106
+ it 'can not find conversion route include inverse of a Proc ' do
107
+ Measure.define_unit :f, :test
108
+ lambda { Measure.define_conversion :f, :a => lambda {|x| x + 2 } }.
109
+ should_not raise_error(Exception)
110
+ #
111
+ # 2 5 10
112
+ # f ===> a <--- b ---> c <--- d
113
+ # |
114
+ # `----> e
115
+ # 10
116
+ #
117
+ # ===> : incommutative conversion
118
+ Measure.should be_direct_compatible(:f, :a)
119
+ Measure.should_not be_direct_compatible(:a, :f)
120
+ Measure.find_conversion_route(:f, :d).should_not be_nil
121
+ Measure.find_conversion_route(:d, :f).should be_nil
122
+ end
123
+
124
+ after :each do
125
+ Measure.clear_units
126
+ end
127
+ end
128
+
129
+ describe Measure, 'with no pre-defined units' do
130
+ before :each do
131
+ Measure.clear_units
132
+ end
133
+
134
+ it 'has no units when nothing is defined' do
135
+ Measure.num_units.should be_equal(0)
136
+ Measure.units.should be_empty
137
+ end
138
+
34
139
  it 'returns length units when call units(:length)' do
35
140
  length_units = [:meter, :feet, :inch]
36
141
  length_units.each {|u| Measure.define_unit u, :length }
@@ -51,8 +156,8 @@ describe Measure do
51
156
  Measure.define_unit :meter, :length
52
157
  Measure.define_unit :inch, :length
53
158
  Measure.define_conversion :meter, :inch => 0.254
54
- Measure.compatible?(:meter, :inch).should be_true
55
- Measure.compatible?(:inch, :meter).should be_true
159
+ Measure.should be_direct_compatible(:meter, :inch)
160
+ Measure.should be_direct_compatible(:inch, :meter)
56
161
  end
57
162
 
58
163
  it 'can convert meter to centimeter after define_conversion(:meter, :centimeter => 100)' do
@@ -68,21 +173,6 @@ describe Measure do
68
173
  Measure(1, :meter).to_s.should == '1 [meter]'
69
174
  end
70
175
 
71
- it 'can define aliases' do
72
- Measure.define_unit :meter, :length
73
- Measure.define_alias :m, :meter
74
- Measure.should be_defined :m
75
- Measure.dimension(:m).should == :length
76
-
77
- Measure.define_unit :centimeter, :length
78
- lambda { Measure.define_alias :centimeter, :meter }.
79
- should raise_error(Measure::UnitRedefinitionError)
80
-
81
- Measure.define_conversion :meter, :centimeter => 100
82
- lambda { Measure(1, :m).as_centimeter }.
83
- should_not raise_error(StandardError)
84
- end
85
-
86
176
  it 'can perform multi-hop conversion' do
87
177
  Measure.define_unit :millimeter, :length
88
178
  Measure.define_unit :inch, :length
@@ -92,8 +182,30 @@ describe Measure do
92
182
  Measure(1, :feet).as_millimeter.should be_close(Measure(12.0 * 25.4, :millimeter), 1e-9)
93
183
  end
94
184
 
185
+ it 'can convert by a Proc' do
186
+ Measure.define_unit :deg_c, :temperature
187
+ Measure.define_unit :deg_f, :temperature
188
+ Measure.define_conversion :deg_f, :deg_c => lambda {|x| 5.0*(x - 32)/9.0 }
189
+ Measure.define_conversion :deg_c, :deg_f => lambda {|x| 9.0*x/5.0 + 32 }
190
+ Measure.should be_direct_compatible(:deg_f, :deg_c)
191
+ Measure.should be_direct_compatible(:deg_c, :deg_f)
192
+ lambda { Measure(40, :deg_c).as_deg_f }.should_not raise_error(Exception)
193
+ end
194
+
195
+ it 'coerce a Number to a Measure with unit 1' do
196
+ Measure.define_unit :a, :test
197
+ Measure(1, :a).coerce(10)[0].unit.should == 1
198
+ end
199
+
200
+ it 'can multiply any units by unit 1' do
201
+ Measure.define_unit :a, :test
202
+ lambda { Measure(1, :a) * Measure(1, 1) }.
203
+ should_not raise_error(Exception)
204
+ lambda { Measure(1, 1) * Measure(1, :a) }.
205
+ should_not raise_error(Exception)
206
+ end
207
+
95
208
  after :each do
96
- Measure.clear_units
97
209
  end
98
210
  end
99
211
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: measure
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kenta Murata
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-08-11 00:00:00 +09:00
12
+ date: 2009-06-13 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -22,6 +22,7 @@ extensions: []
22
22
  extra_rdoc_files:
23
23
  - README
24
24
  - CHANGES
25
+ - COPYING
25
26
  - COPYING.LIB
26
27
  files:
27
28
  - ChangeLog
@@ -34,6 +35,7 @@ files:
34
35
  - lib/measure/length.rb
35
36
  - lib/measure/support.rb
36
37
  - lib/measure/version.rb
38
+ - lib/measure/weight.rb
37
39
  - lib/measure.rb
38
40
  - spec/measure_spec.rb
39
41
  - spec/spec.opts
@@ -65,9 +67,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
65
67
  requirements: []
66
68
 
67
69
  rubyforge_project: measure
68
- rubygems_version: 1.2.0
70
+ rubygems_version: 1.3.1
69
71
  signing_key:
70
72
  specification_version: 2
71
- summary: measure version 0.1.0
73
+ summary: measure version 0.2.0
72
74
  test_files: []
73
75