measure 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +12 -0
- data/COPYING +379 -370
- data/COPYING.LIB +79 -78
- data/ChangeLog +67 -0
- data/Rakefile +5 -3
- data/TODO +8 -3
- data/lib/measure.rb +114 -48
- data/lib/measure/length.rb +57 -51
- data/lib/measure/support.rb +13 -3
- data/lib/measure/version.rb +1 -1
- data/lib/measure/weight.rb +43 -0
- data/spec/measure_spec.rb +147 -35
- metadata +6 -4
data/COPYING.LIB
CHANGED
@@ -1,154 +1,156 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
= GNU LESSER GENERAL PUBLIC LICENSE
|
2
|
+
|
3
|
+
Version:: 3
|
4
|
+
Date:: 29 June 2007
|
3
5
|
|
4
6
|
Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
|
5
7
|
Everyone is permitted to copy and distribute verbatim copies
|
6
8
|
of this license document, but changing it is not allowed.
|
7
9
|
|
8
10
|
|
9
|
-
|
11
|
+
This version of the GNU Lesser General Public License incorporates
|
10
12
|
the terms and conditions of version 3 of the GNU General Public
|
11
13
|
License, supplemented by the additional permissions listed below.
|
12
14
|
|
13
|
-
|
15
|
+
== Additional Definitions.
|
14
16
|
|
15
|
-
|
17
|
+
As used herein, "this License" refers to version 3 of the GNU Lesser
|
16
18
|
General Public License, and the "GNU GPL" refers to version 3 of the GNU
|
17
19
|
General Public License.
|
18
20
|
|
19
|
-
|
21
|
+
"The Library" refers to a covered work governed by this License,
|
20
22
|
other than an Application or a Combined Work as defined below.
|
21
23
|
|
22
|
-
|
24
|
+
An "Application" is any work that makes use of an interface provided
|
23
25
|
by the Library, but which is not otherwise based on the Library.
|
24
26
|
Defining a subclass of a class defined by the Library is deemed a mode
|
25
27
|
of using an interface provided by the Library.
|
26
28
|
|
27
|
-
|
29
|
+
A "Combined Work" is a work produced by combining or linking an
|
28
30
|
Application with the Library. The particular version of the Library
|
29
31
|
with which the Combined Work was made is also called the "Linked
|
30
32
|
Version".
|
31
33
|
|
32
|
-
|
34
|
+
The "Minimal Corresponding Source" for a Combined Work means the
|
33
35
|
Corresponding Source for the Combined Work, excluding any source code
|
34
36
|
for portions of the Combined Work that, considered in isolation, are
|
35
37
|
based on the Application, and not on the Linked Version.
|
36
38
|
|
37
|
-
|
39
|
+
The "Corresponding Application Code" for a Combined Work means the
|
38
40
|
object code and/or source code for the Application, including any data
|
39
41
|
and utility programs needed for reproducing the Combined Work from the
|
40
42
|
Application, but excluding the System Libraries of the Combined Work.
|
41
43
|
|
42
|
-
|
44
|
+
== Exception to Section 3 of the GNU GPL.
|
43
45
|
|
44
|
-
|
46
|
+
You may convey a covered work under sections 3 and 4 of this License
|
45
47
|
without being bound by section 3 of the GNU GPL.
|
46
48
|
|
47
|
-
|
49
|
+
== Conveying Modified Versions.
|
48
50
|
|
49
|
-
|
51
|
+
If you modify a copy of the Library, and, in your modifications, a
|
50
52
|
facility refers to a function or data to be supplied by an Application
|
51
53
|
that uses the facility (other than as an argument passed when the
|
52
54
|
facility is invoked), then you may convey a copy of the modified
|
53
55
|
version:
|
54
56
|
|
55
|
-
|
57
|
+
a. under this License, provided that you make a good faith effort to
|
56
58
|
ensure that, in the event an Application does not supply the
|
57
59
|
function or data, the facility still operates, and performs
|
58
60
|
whatever part of its purpose remains meaningful, or
|
59
61
|
|
60
|
-
|
61
|
-
|
62
|
+
b. under the GNU GPL, with none of the additional permissions of this
|
63
|
+
License applicable to that copy.
|
62
64
|
|
63
|
-
|
65
|
+
== Object Code Incorporating Material from Library Header Files.
|
64
66
|
|
65
|
-
|
67
|
+
The object code form of an Application may incorporate material from
|
66
68
|
a header file that is part of the Library. You may convey such object
|
67
69
|
code under terms of your choice, provided that, if the incorporated
|
68
70
|
material is not limited to numerical parameters, data structure
|
69
71
|
layouts and accessors, or small macros, inline functions and templates
|
70
72
|
(ten or fewer lines in length), you do both of the following:
|
71
73
|
|
72
|
-
|
73
|
-
Library is used in it and that the Library and its use are
|
74
|
-
|
74
|
+
a. Give prominent notice with each copy of the object code that the
|
75
|
+
Library is used in it and that the Library and its use are covered
|
76
|
+
by this License.
|
75
77
|
|
76
|
-
|
77
|
-
document.
|
78
|
+
b. Accompany the object code with a copy of the GNU GPL and this
|
79
|
+
license document.
|
78
80
|
|
79
|
-
|
81
|
+
== Combined Works.
|
80
82
|
|
81
|
-
|
83
|
+
You may convey a Combined Work under terms of your choice that,
|
82
84
|
taken together, effectively do not restrict modification of the
|
83
85
|
portions of the Library contained in the Combined Work and reverse
|
84
86
|
engineering for debugging such modifications, if you also do each of
|
85
87
|
the following:
|
86
88
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
document.
|
93
|
-
|
94
|
-
|
95
|
-
execution, include the copyright notice for the Library among
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
be required to provide such information under section 6 of the
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
89
|
+
a. Give prominent notice with each copy of the Combined Work that the
|
90
|
+
Library is used in it and that the Library and its use are covered
|
91
|
+
by this License.
|
92
|
+
|
93
|
+
b. Accompany the Combined Work with a copy of the GNU GPL and this
|
94
|
+
license document.
|
95
|
+
|
96
|
+
c. For a Combined Work that displays copyright notices during
|
97
|
+
execution, include the copyright notice for the Library among these
|
98
|
+
notices, as well as a reference directing the user to the copies of
|
99
|
+
the GNU GPL and this license document.
|
100
|
+
|
101
|
+
d. Do one of the following:
|
102
|
+
|
103
|
+
0. Convey the Minimal Corresponding Source under the terms of this
|
104
|
+
License, and the Corresponding Application Code in a form
|
105
|
+
suitable for, and under terms that permit, the user to recombine
|
106
|
+
or relink the Application with a modified version of the Linked
|
107
|
+
Version to produce a modified Combined Work, in the manner
|
108
|
+
specified by section 6 of the GNU GPL for conveying
|
109
|
+
Corresponding Source.
|
110
|
+
|
111
|
+
1. Use a suitable shared library mechanism for linking with the
|
112
|
+
Library. A suitable mechanism is one that (a) uses at run time a
|
113
|
+
copy of the Library already present on the user's computer
|
114
|
+
system, and (b) will operate properly with a modified version of
|
115
|
+
the Library that is interface-compatible with the Linked
|
116
|
+
Version.
|
117
|
+
|
118
|
+
e. Provide Installation Information, but only if you would otherwise
|
119
|
+
be required to provide such information under section 6 of the GNU
|
120
|
+
GPL, and only to the extent that such information is necessary to
|
121
|
+
install and execute a modified version of the Combined Work
|
122
|
+
produced by recombining or relinking the Application with a
|
123
|
+
modified version of the Linked Version. (If you use option 4d0, the
|
124
|
+
Installation Information must accompany the Minimal Corresponding
|
125
|
+
Source and Corresponding Application Code. If you use option 4d1,
|
126
|
+
you must provide the Installation Information in the manner
|
127
|
+
specified by section 6 of the GNU GPL for conveying Corresponding
|
128
|
+
Source.)
|
129
|
+
|
130
|
+
== Combined Libraries
|
131
|
+
|
132
|
+
You may place library facilities that are a work based on the
|
131
133
|
Library side by side in a single library together with other library
|
132
134
|
facilities that are not Applications and are not covered by this
|
133
135
|
License, and convey such a combined library under terms of your
|
134
136
|
choice, if you do both of the following:
|
135
137
|
|
136
|
-
|
138
|
+
a. Accompany the combined library with a copy of the same work based
|
137
139
|
on the Library, uncombined with any other library facilities,
|
138
140
|
conveyed under the terms of this License.
|
139
141
|
|
140
|
-
|
141
|
-
|
142
|
+
b. Give prominent notice with the combined library that part of it is
|
143
|
+
a work based on the Library, and explaining where to find the
|
142
144
|
accompanying uncombined form of the same work.
|
143
145
|
|
144
|
-
|
146
|
+
== Revised Versions of the GNU Lesser General Public License.
|
145
147
|
|
146
|
-
|
148
|
+
The Free Software Foundation may publish revised and/or new versions
|
147
149
|
of the GNU Lesser General Public License from time to time. Such new
|
148
150
|
versions will be similar in spirit to the present version, but may
|
149
151
|
differ in detail to address new problems or concerns.
|
150
152
|
|
151
|
-
|
153
|
+
Each version is given a distinguishing version number. If the
|
152
154
|
Library as you received it specifies that a certain numbered version
|
153
155
|
of the GNU Lesser General Public License "or any later version"
|
154
156
|
applies to it, you have the option of following the terms and
|
@@ -158,9 +160,8 @@ received it does not specify a version number of the GNU Lesser
|
|
158
160
|
General Public License, you may choose any version of the GNU Lesser
|
159
161
|
General Public License ever published by the Free Software Foundation.
|
160
162
|
|
161
|
-
|
163
|
+
If the Library as you received it specifies that a proxy can decide
|
162
164
|
whether future versions of the GNU Lesser General Public License shall
|
163
165
|
apply, that proxy's public statement of acceptance of any version is
|
164
166
|
permanent authorization for you to choose that version for the
|
165
167
|
Library.
|
166
|
-
|
data/ChangeLog
CHANGED
@@ -1,3 +1,70 @@
|
|
1
|
+
2009-03-10 Kenta Murata <mrkn@mrkn.jp>
|
2
|
+
|
3
|
+
* lib/measure/support.rb (Measure): add short_form_avaiable? method.
|
4
|
+
|
5
|
+
* lib/measure/support.rb (Measure): rename with_short_form to
|
6
|
+
short_form and add enable_ and disable_short_form methods.
|
7
|
+
|
8
|
+
2008-09-04 Kenta Murata <mrkn@mrkn.jp>
|
9
|
+
|
10
|
+
* spec/measure_spec.rb: modified to avoid warnings ``warning:
|
11
|
+
parenthesize argument(s) for future version'' in 1.8.6.
|
12
|
+
|
13
|
+
* lib/measure/length.rb: add some physical length units.
|
14
|
+
|
15
|
+
* lib/measure/length.rb: enclose unit definitions by ``class
|
16
|
+
Measure / end'' syntax.
|
17
|
+
|
18
|
+
* lib/measure/support.rb (MeasureSupport#method_missing): replace
|
19
|
+
Measure.defined? Measure.has_unit?.
|
20
|
+
|
21
|
+
* lib/measure.rb (def_unit, def_alias, def_conversion): new
|
22
|
+
aliases for short definition form (merged changed by walf443).
|
23
|
+
|
24
|
+
2008-08-24 Kenta Murata <mrkn@mrkn.jp>
|
25
|
+
|
26
|
+
* Rakefile, lib/measure.rb, spec/measure_spec.rb: merge the
|
27
|
+
changes by walf443 for fixing warnings "warning: parenthesize
|
28
|
+
argument(s) for future version" in 1.8.6.
|
29
|
+
|
30
|
+
* spec/measure_spec.rb: some specs are relocated and modified appropriately.
|
31
|
+
|
32
|
+
* spec/measure_spec.rb: replace compatible to direct_compatible.
|
33
|
+
|
34
|
+
* .gitignore: add doc/rdoc.
|
35
|
+
|
36
|
+
* TODO: rdocnize and add some items.
|
37
|
+
|
38
|
+
* COPYING, COPYING.LIB: rdocnize.
|
39
|
+
|
40
|
+
* spec/measure_spec.rb: s/cummutative/commutative/g.
|
41
|
+
|
42
|
+
* Rakefile (rdoc): modify output directory.
|
43
|
+
|
44
|
+
* lib/measure.rb: some documentation comments are written.
|
45
|
+
(Measure.defined?): it is removed.
|
46
|
+
(Measure.resolve_alias): it now raise InvalidUnitError if the
|
47
|
+
given unit is invalid.
|
48
|
+
(Measure.compatible?): it is renamed to more intuitively name
|
49
|
+
`Measure.direct_compatible?', and the original is removed now.
|
50
|
+
(Measure.num_units): it now returns the number of both defined
|
51
|
+
units and aliases.
|
52
|
+
(Measure.define_unit): it method returns Measure class if
|
53
|
+
succeeded.
|
54
|
+
(Measure.define_conversion, Measure.neighbors, Measure#convert):
|
55
|
+
it now supports conversions by Proc objects.
|
56
|
+
(Measure.undefine_unit): it is a new method to undefine an unit.
|
57
|
+
(Measure.find_multi_hop_conversion): it is renamed to more
|
58
|
+
intuitively name `Measure.find_conversion_route', and the original
|
59
|
+
is now an alias.
|
60
|
+
(Measure#*, Measure#/): it now supports operation with values with
|
61
|
+
unit `1'.
|
62
|
+
(Measure#to_i, Measure#to_f): they are removed because of the
|
63
|
+
behavior of Numeric#coerce.
|
64
|
+
|
65
|
+
* lib/measure/version.rb (Measure::VERSION): change development
|
66
|
+
version number to `0.2.0'.
|
67
|
+
|
1
68
|
2008-08-11 Kenta Murata <muraken@gmail.com>
|
2
69
|
|
3
70
|
* Rakefile: replace all Spec::VERSION module to Measure::VERSION.
|
data/Rakefile
CHANGED
@@ -29,6 +29,7 @@ end
|
|
29
29
|
desc 'Run all specs and store html output in doc/output/report.html'
|
30
30
|
Spec::Rake::SpecTask.new('spec_html') do |t|
|
31
31
|
t.spec_files = FileList['spec/**/*_spec.rb']
|
32
|
+
t.warning = true
|
32
33
|
t.spec_opts = [ '--format html:../../../../doc/output/report.html',
|
33
34
|
'--format progress',
|
34
35
|
'--backtrace' ]
|
@@ -36,10 +37,11 @@ end
|
|
36
37
|
|
37
38
|
desc 'Generate RDoc'
|
38
39
|
rd = Rake::RDocTask.new do |rdoc|
|
39
|
-
rdoc.rdoc_dir = File.join('
|
40
|
+
rdoc.rdoc_dir = File.join('doc', 'rdoc')
|
40
41
|
rdoc.options << '--title' << 'Measure' << '--line-numbers'
|
41
42
|
rdoc.options << '--inline-source' << '--main' << 'README'
|
42
|
-
rdoc.rdoc_files.include('README', 'CHANGES', 'COPYING
|
43
|
+
rdoc.rdoc_files.include('README', 'CHANGES', 'COPYING', 'COPYING.LIB',
|
44
|
+
'lib/**/*.rb')
|
43
45
|
end
|
44
46
|
|
45
47
|
spec = Gem::Specification.new do |s|
|
@@ -135,7 +137,7 @@ end
|
|
135
137
|
|
136
138
|
desc 'Publish news on RubyForge'
|
137
139
|
task :publish_news => [ :verify_user ] do
|
138
|
-
unless Measure::VERSION::RELEASE_CANDIDATE
|
140
|
+
unless defined? Measure::VERSION::RELEASE_CANDIDATE and Measure::VERSION::RELEASE_CANDIDATE
|
139
141
|
require 'meta_project'
|
140
142
|
require 'rake/contrib/xforge'
|
141
143
|
|
data/TODO
CHANGED
@@ -1,13 +1,18 @@
|
|
1
|
-
|
1
|
+
== TODOs
|
2
2
|
|
3
|
+
=== Toward the version 1.0.0
|
4
|
+
|
5
|
+
- Defined units should be managed by Hash instead of Array because of search speed.
|
6
|
+
- The unit management system should be implemented as thread safe.
|
3
7
|
- To write a spec for Measure#convert method with a unit same as receiver's one.
|
4
|
-
- To write specs for Measure#to_{a,
|
8
|
+
- To write specs for Measure#to_{a,s}.
|
5
9
|
- To write a spec for Measure#coerce.
|
6
10
|
- To write specs for binary operation.
|
7
11
|
- To write specs for comparison methods.
|
8
12
|
- To write RDoc documents.
|
13
|
+
+ Supports incommutative conversion by Proc objects.
|
9
14
|
|
10
|
-
|
15
|
+
=== Future works
|
11
16
|
|
12
17
|
- To support composite dimension.
|
13
18
|
- To define well-known physical constants.
|
data/lib/measure.rb
CHANGED
@@ -20,22 +20,50 @@ class Measure
|
|
20
20
|
end
|
21
21
|
|
22
22
|
def has_unit?(unit)
|
23
|
-
|
24
|
-
|
23
|
+
begin
|
24
|
+
unit = resolve_alias unit
|
25
|
+
return @@units.include?(unit)
|
26
|
+
rescue InvalidUnitError
|
27
|
+
return false
|
28
|
+
end
|
25
29
|
end
|
26
|
-
alias defined? has_unit?
|
27
30
|
|
28
|
-
|
31
|
+
#
|
32
|
+
# Resolves an alias and returns the entity.
|
33
|
+
# The returned unit is NOT invalid always.
|
34
|
+
# InvalidUnitError is raised if a given unit is undefined.
|
35
|
+
#
|
36
|
+
def resolve_alias(unit)
|
37
|
+
# NOTE: mustn't use has_unit? method to avoid infinite recursion
|
38
|
+
return unit if @@units.include? unit
|
39
|
+
unless @@alias_map.has_key? unit
|
40
|
+
raise InvalidUnitError, "unit `#{unit}' is undefined"
|
41
|
+
end
|
42
|
+
while @@alias_map.has_key? unit
|
43
|
+
unit = @@alias_map[unit]
|
44
|
+
end
|
45
|
+
return unit
|
46
|
+
end
|
47
|
+
|
48
|
+
#
|
49
|
+
# Test direct compatibility between two units, u1 and u2.
|
50
|
+
#
|
51
|
+
def direct_compatible?(u1, u2)
|
29
52
|
u1 = resolve_alias u1
|
30
|
-
raise InvalidUnitError, "unknown unit: #{u1}" unless self.defined? u1
|
31
53
|
u2 = resolve_alias u2
|
32
|
-
raise InvalidUnitError, "unknown unit: #{u2}" unless self.defined? u2
|
33
54
|
return true if u1 == u2
|
34
|
-
|
35
|
-
|
55
|
+
if @@conversion_map.has_key? u1 and @@conversion_map[u1].has_key? u2
|
56
|
+
return true
|
57
|
+
end
|
58
|
+
if @@conversion_map.has_key? u2 and @@conversion_map[u2].has_key? u1
|
59
|
+
return true unless Proc === @@conversion_map[u2][u1]
|
60
|
+
end
|
36
61
|
return false
|
37
62
|
end
|
38
63
|
|
64
|
+
#
|
65
|
+
# Clear all defined units.
|
66
|
+
#
|
39
67
|
def clear_units
|
40
68
|
@@units.clear
|
41
69
|
@@dimension_map.clear
|
@@ -44,15 +72,26 @@ class Measure
|
|
44
72
|
return nil
|
45
73
|
end
|
46
74
|
|
75
|
+
#
|
76
|
+
# Returns defined units. If dimension is specified, returning
|
77
|
+
# units are of only the dimension.
|
78
|
+
#
|
47
79
|
def units(dimension=nil)
|
48
80
|
return @@units.dup if dimension.nil?
|
49
81
|
@@dimension_map.select {|k, v| v == dimension }.collect{|k, v| k }
|
50
82
|
end
|
51
83
|
|
84
|
+
#
|
85
|
+
# The number of defined units.
|
86
|
+
#
|
52
87
|
def num_units
|
53
|
-
return @@units.length
|
88
|
+
return @@units.length + @@alias_map.length
|
54
89
|
end
|
55
90
|
|
91
|
+
#
|
92
|
+
# Defines a unit. The default dimension is 1.
|
93
|
+
# Measure::UnitRedefinitionError is raised when the unit is redefined.
|
94
|
+
#
|
56
95
|
def define_unit(unit, dimension=1)
|
57
96
|
if @@units.include?(unit)
|
58
97
|
if self.dimension(unit) != dimension
|
@@ -61,42 +100,64 @@ class Measure
|
|
61
100
|
else
|
62
101
|
@@units << unit
|
63
102
|
@@dimension_map[unit] = dimension
|
103
|
+
return self
|
64
104
|
end
|
65
105
|
end
|
66
106
|
|
107
|
+
alias def_unit define_unit
|
108
|
+
|
109
|
+
#
|
110
|
+
# Defines an alias.
|
111
|
+
# Measure::UnitRedefinitionError is raised when the alias is redefined.
|
112
|
+
# Measure::InvalidUnitError is raised when the base unit is not defined.
|
113
|
+
#
|
67
114
|
def define_alias(unit, base)
|
68
|
-
if self.
|
115
|
+
if self.has_unit?(unit)
|
69
116
|
raise UnitRedefinitionError, "unit [#{unit}] is already defined"
|
70
117
|
end
|
71
|
-
|
72
|
-
@@alias_map[unit] = base
|
118
|
+
@@alias_map[unit] = resolve_alias base
|
73
119
|
end
|
74
120
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
121
|
+
alias def_alias define_alias
|
122
|
+
|
123
|
+
#
|
124
|
+
# Defines conversions.
|
125
|
+
#
|
126
|
+
#
|
127
|
+
def define_conversion(origin, conversion)
|
128
|
+
origin = resolve_alias origin
|
129
|
+
@@conversion_map[origin] ||= {}
|
130
|
+
conversion.each {|target, conv|
|
131
|
+
target = resolve_alias target
|
132
|
+
@@conversion_map[origin][target] = conv
|
83
133
|
}
|
84
134
|
return nil
|
85
135
|
end
|
86
136
|
|
87
|
-
|
88
|
-
|
137
|
+
alias def_conversion define_conversion
|
138
|
+
|
139
|
+
def undefine_unit(unit)
|
140
|
+
if @@units.include? unit
|
141
|
+
@@conversion_map.delete unit
|
142
|
+
@@conversion_map.each {|k, v| v.delete unit }
|
143
|
+
@@units.delete unit
|
144
|
+
return true
|
145
|
+
elsif @@alias_map.has_key? unit
|
146
|
+
@@alias_map.delete unit
|
147
|
+
return true
|
148
|
+
end
|
149
|
+
return false
|
89
150
|
end
|
90
|
-
alias dim dimension
|
91
151
|
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
return unit
|
152
|
+
#
|
153
|
+
#
|
154
|
+
#
|
155
|
+
def dimension(unit)
|
156
|
+
return @@dimension_map[resolve_alias(unit)]
|
97
157
|
end
|
158
|
+
alias dim dimension
|
98
159
|
|
99
|
-
def
|
160
|
+
def find_conversion_route(u1, u2)
|
100
161
|
visited = []
|
101
162
|
queue = [[u1]]
|
102
163
|
while route = queue.shift
|
@@ -108,6 +169,7 @@ class Measure
|
|
108
169
|
end
|
109
170
|
return nil
|
110
171
|
end
|
172
|
+
alias find_multi_hop_conversion find_conversion_route
|
111
173
|
|
112
174
|
# def encode_dimension(dim)
|
113
175
|
# case dim
|
@@ -127,10 +189,9 @@ class Measure
|
|
127
189
|
|
128
190
|
def neighbors(unit)
|
129
191
|
res = []
|
130
|
-
if @@conversion_map.has_key?(unit)
|
131
|
-
|
132
|
-
|
133
|
-
@@conversion_map.each {|k, v| res << k if v.has_key? unit }
|
192
|
+
res += @@conversion_map[unit].keys if @@conversion_map.has_key?(unit)
|
193
|
+
@@conversion_map.each {|k, v|
|
194
|
+
res << k if v.has_key? unit and not Proc === v[unit] }
|
134
195
|
return res
|
135
196
|
end
|
136
197
|
end # class << self
|
@@ -174,9 +235,9 @@ class Measure
|
|
174
235
|
|
175
236
|
def ==(other)
|
176
237
|
return self.value == other.value if self.unit == other.unit
|
177
|
-
if Measure.
|
238
|
+
if Measure.direct_compatible? self.unit, other.unit
|
178
239
|
return self == other.convert(self.unit)
|
179
|
-
elsif Measure.
|
240
|
+
elsif Measure.direct_compatible? other.unit, self.unit
|
180
241
|
return self.convert(other.unit) == other
|
181
242
|
else
|
182
243
|
return false
|
@@ -226,8 +287,9 @@ class Measure
|
|
226
287
|
def *(other)
|
227
288
|
case other
|
228
289
|
when Measure
|
229
|
-
# TODO: dimension
|
230
290
|
return other * self.value if self.unit == 1
|
291
|
+
return Measure(self.value * other.value, self.unit) if other.unit == 1
|
292
|
+
# TODO: dimension
|
231
293
|
raise NotImplementedError, "this feature has not implemented yet"
|
232
294
|
# if self.unit == other.unit
|
233
295
|
# return Measure(self.value * other.value, self.unit)
|
@@ -281,14 +343,6 @@ class Measure
|
|
281
343
|
return "#{self.value} [#{self.unit}]"
|
282
344
|
end
|
283
345
|
|
284
|
-
def to_i
|
285
|
-
return self.value.to_i
|
286
|
-
end
|
287
|
-
|
288
|
-
def to_f
|
289
|
-
return self.value.to_f
|
290
|
-
end
|
291
|
-
|
292
346
|
def to_a
|
293
347
|
return [self.value, self.unit]
|
294
348
|
end
|
@@ -296,12 +350,18 @@ class Measure
|
|
296
350
|
def convert(unit)
|
297
351
|
return self if unit == self.unit
|
298
352
|
to_unit = Measure.resolve_alias unit
|
299
|
-
raise InvalidUnitError, "unknown unit: #{unit}" unless Measure.
|
353
|
+
raise InvalidUnitError, "unknown unit: #{unit}" unless Measure.has_unit? unit
|
300
354
|
from_unit = Measure.resolve_alias self.unit
|
301
|
-
if Measure.
|
355
|
+
if Measure.direct_compatible? from_unit, to_unit
|
302
356
|
# direct conversion
|
303
357
|
if @@conversion_map.has_key? from_unit and @@conversion_map[from_unit].has_key? to_unit
|
304
|
-
|
358
|
+
conv = @@conversion_map[from_unit][to_unit]
|
359
|
+
case conv
|
360
|
+
when Proc
|
361
|
+
value = conv[self.value]
|
362
|
+
else
|
363
|
+
value = self.value * conv
|
364
|
+
end
|
305
365
|
else
|
306
366
|
value = self.value / @@conversion_map[to_unit][from_unit].to_f
|
307
367
|
end
|
@@ -310,7 +370,13 @@ class Measure
|
|
310
370
|
value = self.value
|
311
371
|
while u2 = route.shift
|
312
372
|
if @@conversion_map.has_key? u1 and @@conversion_map[u1].has_key? u2
|
313
|
-
|
373
|
+
conv = @@conversion_map[u1][u2]
|
374
|
+
case conv
|
375
|
+
when Proc
|
376
|
+
value = conv[vaule]
|
377
|
+
else
|
378
|
+
value *= conv
|
379
|
+
end
|
314
380
|
else
|
315
381
|
value /= @@conversion_map[u2][u1].to_f
|
316
382
|
end
|