measure 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +8 -0
- data/COPYING +675 -0
- data/COPYING.LIB +166 -0
- data/ChangeLog +10 -0
- data/README +19 -0
- data/Rakefile +150 -0
- data/TODO +13 -0
- data/lib/measure.rb +348 -0
- data/lib/measure/length.rb +59 -0
- data/lib/measure/support.rb +52 -0
- data/lib/measure/version.rb +17 -0
- data/spec/measure_spec.rb +104 -0
- data/spec/spec.opts +4 -0
- data/spec/spec_helper.rb +4 -0
- metadata +73 -0
@@ -0,0 +1,59 @@
|
|
1
|
+
# = Definitions of the length 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
|
+
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
|
@@ -0,0 +1,52 @@
|
|
1
|
+
# Author:: Kenta Murata
|
2
|
+
# Copyright:: Copyright (C) 2008 Kenta Murata
|
3
|
+
# License:: LGPL version 3.0
|
4
|
+
|
5
|
+
module MeasureSupport
|
6
|
+
def self.enable?
|
7
|
+
Thread.current[:measure_support_enabled]
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.enable
|
11
|
+
Thread.current[:measure_support_enabled] = true
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.disable
|
15
|
+
Thread.current[:measure_support_enabled] = false
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.included(mod)
|
19
|
+
mod.module_eval %Q{
|
20
|
+
alias measure_support_saved_method_missing method_missing
|
21
|
+
private_methods :measure_support_saved_method_missing
|
22
|
+
|
23
|
+
def method_missing(name, *args)
|
24
|
+
if MeasureSupport.enable?
|
25
|
+
return Measure.new(self, name) if Measure.defined?(name)
|
26
|
+
end
|
27
|
+
return measure_support_saved_method_missing(name, *args)
|
28
|
+
end
|
29
|
+
}
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class << Measure
|
34
|
+
def with_short_form
|
35
|
+
begin
|
36
|
+
MeasureSupport.enable
|
37
|
+
return yield
|
38
|
+
ensure
|
39
|
+
MeasureSupport.disable
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
alias form with_short_form
|
44
|
+
end
|
45
|
+
|
46
|
+
class Integer
|
47
|
+
include MeasureSupport
|
48
|
+
end
|
49
|
+
|
50
|
+
class Float
|
51
|
+
include MeasureSupport
|
52
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# Author:: Kenta Murata
|
2
|
+
# Copyright:: Copyright (C) 2008 Kenta Murata
|
3
|
+
# License:: LGPL version 3.0
|
4
|
+
|
5
|
+
class Measure
|
6
|
+
module VERSION
|
7
|
+
unless defined? MAJOR
|
8
|
+
MAJOR = 0
|
9
|
+
MINOR = 1
|
10
|
+
TINY = 0
|
11
|
+
|
12
|
+
STRING = [MAJOR, MINOR, TINY].join('.')
|
13
|
+
|
14
|
+
SUMMARY = "measure version #{STRING}"
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,104 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'spec_helper')
|
2
|
+
require 'measure'
|
3
|
+
|
4
|
+
describe Measure do
|
5
|
+
before :each do
|
6
|
+
end
|
7
|
+
|
8
|
+
it 'has no units when nothing is defined' do
|
9
|
+
Measure.num_units.should be_equal 0
|
10
|
+
Measure.units.should be_empty
|
11
|
+
end
|
12
|
+
|
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
|
17
|
+
end
|
18
|
+
|
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
|
23
|
+
end
|
24
|
+
|
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 }.
|
29
|
+
should_not raise_error(Measure::UnitRedefinitionError)
|
30
|
+
lambda { Measure.define_unit :meter, :weight }.
|
31
|
+
should raise_error(Measure::UnitRedefinitionError)
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'returns length units when call units(:length)' do
|
35
|
+
length_units = [:meter, :feet, :inch]
|
36
|
+
length_units.each {|u| Measure.define_unit u, :length }
|
37
|
+
Measure.define_unit :kilogram, :weight
|
38
|
+
Measure.define_unit :second, :time
|
39
|
+
set_equal?(Measure.units(:length), length_units).should be_true
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'raises Measure::InvalidUnitError when conversion is defined with undefined unit' do
|
43
|
+
Measure.define_unit :meter, :length
|
44
|
+
lambda { Measure.define_conversion :inch, :meter => 1/0.254 }.
|
45
|
+
should raise_error(Measure::InvalidUnitError)
|
46
|
+
lambda { Measure.define_conversion :meter, :inch => 1/0.254 }.
|
47
|
+
should raise_error(Measure::InvalidUnitError)
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'has commutative compatibility' do
|
51
|
+
Measure.define_unit :meter, :length
|
52
|
+
Measure.define_unit :inch, :length
|
53
|
+
Measure.define_conversion :meter, :inch => 0.254
|
54
|
+
Measure.compatible?(:meter, :inch).should be_true
|
55
|
+
Measure.compatible?(:inch, :meter).should be_true
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'can convert meter to centimeter after define_conversion(:meter, :centimeter => 100)' do
|
59
|
+
Measure.define_unit :meter, :length
|
60
|
+
Measure.define_unit :centimeter, :length
|
61
|
+
Measure.define_conversion :meter, :centimeter => 100
|
62
|
+
(Measure(1, :meter).as_centimeter).should == Measure(100, :centimeter)
|
63
|
+
(Measure(1, :centimeter).as_meter).should == Measure(0.01, :meter)
|
64
|
+
end
|
65
|
+
|
66
|
+
it 'can convert to string' do
|
67
|
+
Measure.define_unit :meter, :length
|
68
|
+
Measure(1, :meter).to_s.should == '1 [meter]'
|
69
|
+
end
|
70
|
+
|
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
|
+
it 'can perform multi-hop conversion' do
|
87
|
+
Measure.define_unit :millimeter, :length
|
88
|
+
Measure.define_unit :inch, :length
|
89
|
+
Measure.define_unit :feet, :length
|
90
|
+
Measure.define_conversion :inch, :millimeter => 25.4
|
91
|
+
Measure.define_conversion :feet, :inch => 12.0
|
92
|
+
Measure(1, :feet).as_millimeter.should be_close(Measure(12.0 * 25.4, :millimeter), 1e-9)
|
93
|
+
end
|
94
|
+
|
95
|
+
after :each do
|
96
|
+
Measure.clear_units
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
private
|
101
|
+
|
102
|
+
def set_equal?(a1, a2)
|
103
|
+
(a1 - a2).empty?
|
104
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: measure
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kenta Murata
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-08-11 00:00:00 +09:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Measure is a library to handle measurement numbers.
|
17
|
+
email: measure-devel@rubyforge.org
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
- CHANGES
|
25
|
+
- COPYING.LIB
|
26
|
+
files:
|
27
|
+
- ChangeLog
|
28
|
+
- CHANGES
|
29
|
+
- COPYING
|
30
|
+
- COPYING.LIB
|
31
|
+
- Rakefile
|
32
|
+
- README
|
33
|
+
- TODO
|
34
|
+
- lib/measure/length.rb
|
35
|
+
- lib/measure/support.rb
|
36
|
+
- lib/measure/version.rb
|
37
|
+
- lib/measure.rb
|
38
|
+
- spec/measure_spec.rb
|
39
|
+
- spec/spec.opts
|
40
|
+
- spec/spec_helper.rb
|
41
|
+
has_rdoc: true
|
42
|
+
homepage: http://measure.rubyforge.org
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options:
|
45
|
+
- --title
|
46
|
+
- Measure
|
47
|
+
- --line-numbers
|
48
|
+
- --inline-source
|
49
|
+
- --main
|
50
|
+
- README
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: "0"
|
58
|
+
version:
|
59
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: "0"
|
64
|
+
version:
|
65
|
+
requirements: []
|
66
|
+
|
67
|
+
rubyforge_project: measure
|
68
|
+
rubygems_version: 1.2.0
|
69
|
+
signing_key:
|
70
|
+
specification_version: 2
|
71
|
+
summary: measure version 0.1.0
|
72
|
+
test_files: []
|
73
|
+
|