farski-systeme 0.2.1 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION.yml +2 -2
- data/lib/systeme/imperial.rb +30 -42
- data/lib/systeme/international.rb +62 -0
- data/lib/systeme/localize.rb +21 -0
- data/lib/systeme/metric.rb +8 -51
- data/lib/systeme.rb +6 -1
- data/test/international_test.rb +23 -0
- data/test/metric_test.rb +3 -19
- metadata +4 -1
data/VERSION.yml
CHANGED
data/lib/systeme/imperial.rb
CHANGED
@@ -7,53 +7,41 @@ module Systeme
|
|
7
7
|
end
|
8
8
|
|
9
9
|
module Units
|
10
|
-
|
10
|
+
Imperial = Hash.new
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
12
|
+
Imperial['length'] = Hash.new
|
13
|
+
Imperial['length'][:si] = 0.3048
|
14
|
+
Imperial['length'][:units] = Array.new
|
15
|
+
Imperial['length'][:units] << { :unit => 'thou', :factor => (1.0/12000), :aliases => ['mil'] }
|
16
|
+
Imperial['length'][:units] << { :unit => 'inch', :factor => (1.0/12), :aliases => ['in', 'inches'] }
|
17
|
+
Imperial['length'][:units] << { :unit => 'foot', :factor => 1, :aliases => ['feet', 'ft'] }
|
18
|
+
Imperial['length'][:units] << { :unit => 'yard', :factor => 3, :aliases => ['yd'] }
|
19
|
+
Imperial['length'][:units] << { :unit => 'furlong', :factor => 660 }
|
20
|
+
Imperial['length'][:units] << { :unit => 'mile', :factor => 5280, :aliases => ['mi'] }
|
21
|
+
Imperial['length'][:units] << { :unit => 'league', :factor => 15840 }
|
22
22
|
# maritime units
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
23
|
+
Imperial['length'][:units] << { :unit => 'fathom', :factor => 6 }
|
24
|
+
Imperial['length'][:units] << { :unit => 'cable', :factor => 608 }
|
25
|
+
Imperial['length'][:units] << { :unit => 'nautical_mile', :factor => 6080, :aliases => ['NM', 'nmi', 'M'] }
|
26
|
+
Imperial['length'][:units] << { :unit => 'link', :factor => (66.0/100), :aliases => ['lnk'] }
|
27
|
+
Imperial['length'][:units] << { :unit => 'rod', :factor => (66.0/4), :aliases => ['perch', 'perches', 'pole', 'lug'] }
|
28
|
+
Imperial['length'][:units] << { :unit => 'chain', :factor => 66 }
|
29
29
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
30
|
+
Imperial['weight'] = Hash.new
|
31
|
+
Imperial['weight'][:si] = 0.45359237
|
32
|
+
Imperial['weight'][:units] = Array.new
|
33
|
+
Imperial['weight'][:units] << { :unit => 'grain', :factor => (1.0/7000) }
|
34
|
+
Imperial['weight'][:units] << { :unit => 'drachm', :factor => (1.0/256) }
|
35
|
+
Imperial['weight'][:units] << { :unit => 'ounce', :factor => (1.0/16), :aliases => ['oz'] }
|
36
|
+
Imperial['weight'][:units] << { :unit => 'pound', :factor => 1, :aliases => ['lb', 'lbm', 'lbs'] }
|
37
|
+
Imperial['weight'][:units] << { :unit => 'stone', :factor => 14, :aliases => ['st'] }
|
38
|
+
Imperial['weight'][:units] << { :unit => 'quarter', :factor => 28 }
|
39
|
+
Imperial['weight'][:units] << { :unit => 'hundredweight', :factor => 112, :aliases => ['cwt'] }
|
40
|
+
Imperial['weight'][:units] << { :unit => 'ton', :factor => 2240 }
|
41
41
|
end # Units
|
42
42
|
|
43
43
|
module Declarations
|
44
|
-
Systeme::Imperial::Units::
|
45
|
-
|
46
|
-
define_method(unit[:unit].to_sym) { self * unit[:factor].to_f * data[:si] }
|
47
|
-
class_eval("alias :" + unit[:unit] + "s :" + unit[:unit])
|
48
|
-
Systeme::Registration::names << unit[:unit] << unit[:unit]+"s"
|
49
|
-
if unit[:aliases]
|
50
|
-
unit[:aliases].each do |aka|
|
51
|
-
class_eval("alias :" + aka + " :" + unit[:unit])
|
52
|
-
Systeme::Registration::names << aka
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
end
|
57
|
-
end # Declarations
|
44
|
+
Systeme::Localize::declare_system(Systeme::Imperial::Units::Imperial)
|
45
|
+
end
|
58
46
|
end
|
59
47
|
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
require 'systeme/registration'
|
2
|
+
|
3
|
+
module Systeme
|
4
|
+
module International
|
5
|
+
def self.included(caller)
|
6
|
+
caller.send :include, Systeme::International::Declarations
|
7
|
+
end
|
8
|
+
|
9
|
+
module Units
|
10
|
+
SI = Hash.new
|
11
|
+
SI[:meter] = { :symbol => :m, :measure => "length" }
|
12
|
+
SI[:gram] = { :symbol => :g, :measure => "mass", :factor_offset => -3 }
|
13
|
+
SI[:second] = { :symbol => :s, :measure => "time" }
|
14
|
+
SI[:ampere] = { :symbol => :A, :measure => "electric current" }
|
15
|
+
SI[:kelvin] = { :symbol => :K, :measure => "thermodynamic temperature" }
|
16
|
+
|
17
|
+
Prefixes = Hash.new
|
18
|
+
Prefixes[:yocto] = { :factor => -24, :symbol_prefix => "y" }
|
19
|
+
Prefixes[:zepto] = { :factor => -21, :symbol_prefix => "z" }
|
20
|
+
Prefixes[:atto] = { :factor => -18, :symbol_prefix => "a" }
|
21
|
+
Prefixes[:femto] = { :factor => -15, :symbol_prefix => "f" }
|
22
|
+
Prefixes[:pico] = { :factor => -12, :symbol_prefix => "p" }
|
23
|
+
Prefixes[:nano] = { :factor => -9, :symbol_prefix => "n" }
|
24
|
+
Prefixes[:micro] = { :factor => -6, :symbol_prefix => "u" } # use of 'µ' should be supported with unicode?
|
25
|
+
Prefixes[:milli] = { :factor => -3, :symbol_prefix => "m" }
|
26
|
+
Prefixes[:centi] = { :factor => -2, :symbol_prefix => "c" }
|
27
|
+
Prefixes[:deci] = { :factor => -1, :symbol_prefix => "d" }
|
28
|
+
Prefixes[:deca] = { :factor => 1, :symbol_prefix => "da" }
|
29
|
+
Prefixes[:hecto] = { :factor => 2, :symbol_prefix => "h" }
|
30
|
+
Prefixes[:kilo] = { :factor => 3, :symbol_prefix => "k" }
|
31
|
+
Prefixes[:mega] = { :factor => 6, :symbol_prefix => "M" }
|
32
|
+
Prefixes[:giga] = { :factor => 9, :symbol_prefix => "G" }
|
33
|
+
Prefixes[:tera] = { :factor => 12, :symbol_prefix => "T" }
|
34
|
+
Prefixes[:peta] = { :factor => 15, :symbol_prefix => "P" }
|
35
|
+
Prefixes[:exa] = { :factor => 18, :symbol_prefix => "E" }
|
36
|
+
Prefixes[:zetta] = { :factor => 21, :symbol_prefix => "Z" }
|
37
|
+
Prefixes[:yotta] = { :factor => 24, :symbol_prefix => "Y" }
|
38
|
+
end # Units
|
39
|
+
|
40
|
+
module Declarations
|
41
|
+
Systeme::International::Units::SI.each do |unit, unit_data|
|
42
|
+
unit_factor_offset = (unit_data[:factor_offset] ? unit_data[:factor_offset] : 0 )
|
43
|
+
plural_unit_name = unit.to_s + "s"
|
44
|
+
|
45
|
+
define_method(plural_unit_name.to_sym) { self * (10**(unit_factor_offset)) }
|
46
|
+
class_eval("alias :" + unit_data[:symbol].to_s + " :" + plural_unit_name)
|
47
|
+
class_eval("alias :" + plural_unit_name.chop + " :" + plural_unit_name)
|
48
|
+
Systeme::Registration::names << plural_unit_name << unit_data[:symbol].to_s << plural_unit_name.chop
|
49
|
+
|
50
|
+
Systeme::International::Units::Prefixes.each do |prefix, prefix_data|
|
51
|
+
plural_prefixed_unit_name = prefix.to_s + plural_unit_name
|
52
|
+
prefixed_symbol = prefix_data[:symbol_prefix] + unit_data[:symbol].to_s
|
53
|
+
|
54
|
+
define_method(plural_prefixed_unit_name.to_sym) { self * (10**(prefix_data[:factor] + unit_factor_offset)) }
|
55
|
+
class_eval("alias :" + prefixed_symbol + " :" + plural_prefixed_unit_name)
|
56
|
+
class_eval("alias :" + plural_prefixed_unit_name.chop + " :" + plural_prefixed_unit_name)
|
57
|
+
Systeme::Registration::names << plural_prefixed_unit_name << prefixed_symbol << plural_prefixed_unit_name.chop
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end # Declarations
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'systeme/registration'
|
2
|
+
|
3
|
+
module Systeme
|
4
|
+
module Localize
|
5
|
+
def self.declare_system(dictionary)
|
6
|
+
dictionary.each do |measure, data|
|
7
|
+
data[:units].each do |unit|
|
8
|
+
define_method(unit[:unit].to_sym) { self * unit[:factor].to_f * data[:si] }
|
9
|
+
class_eval("alias :" + unit[:unit] + "s :" + unit[:unit])
|
10
|
+
Systeme::Registration::names << unit[:unit] << unit[:unit]+"s"
|
11
|
+
if unit[:aliases]
|
12
|
+
unit[:aliases].each do |aka|
|
13
|
+
class_eval("alias :" + aka + " :" + unit[:unit])
|
14
|
+
Systeme::Registration::names << aka
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/systeme/metric.rb
CHANGED
@@ -1,62 +1,19 @@
|
|
1
1
|
require 'systeme/registration'
|
2
|
+
require 'systeme/localize'
|
2
3
|
|
3
4
|
module Systeme
|
4
5
|
module Metric
|
5
6
|
def self.included(caller)
|
6
7
|
caller.send :include, Systeme::Metric::Declarations
|
7
|
-
end
|
8
|
-
|
8
|
+
end
|
9
|
+
|
9
10
|
module Units
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
SI_BASE_UNITS[:second] = { :symbol => :s, :measure => "time" }
|
14
|
-
SI_BASE_UNITS[:ampere] = { :symbol => :A, :measure => "electric current" }
|
15
|
-
SI_BASE_UNITS[:kelvin] = { :symbol => :K, :measure => "thermodynamic temperature" }
|
16
|
-
|
17
|
-
SI_UNIT_PREFIXES = Hash.new
|
18
|
-
SI_UNIT_PREFIXES[:yocto] = { :factor => -24, :symbol_prefix => "y" }
|
19
|
-
SI_UNIT_PREFIXES[:zepto] = { :factor => -21, :symbol_prefix => "z" }
|
20
|
-
SI_UNIT_PREFIXES[:atto] = { :factor => -18, :symbol_prefix => "a" }
|
21
|
-
SI_UNIT_PREFIXES[:femto] = { :factor => -15, :symbol_prefix => "f" }
|
22
|
-
SI_UNIT_PREFIXES[:pico] = { :factor => -12, :symbol_prefix => "p" }
|
23
|
-
SI_UNIT_PREFIXES[:nano] = { :factor => -9, :symbol_prefix => "n" }
|
24
|
-
SI_UNIT_PREFIXES[:micro] = { :factor => -6, :symbol_prefix => "u" } # use of 'µ' should be supported with unicode?
|
25
|
-
SI_UNIT_PREFIXES[:milli] = { :factor => -3, :symbol_prefix => "m" }
|
26
|
-
SI_UNIT_PREFIXES[:centi] = { :factor => -2, :symbol_prefix => "c" }
|
27
|
-
SI_UNIT_PREFIXES[:deci] = { :factor => -1, :symbol_prefix => "d" }
|
28
|
-
SI_UNIT_PREFIXES[:deca] = { :factor => 1, :symbol_prefix => "da" }
|
29
|
-
SI_UNIT_PREFIXES[:hecto] = { :factor => 2, :symbol_prefix => "h" }
|
30
|
-
SI_UNIT_PREFIXES[:kilo] = { :factor => 3, :symbol_prefix => "k" }
|
31
|
-
SI_UNIT_PREFIXES[:mega] = { :factor => 6, :symbol_prefix => "M" }
|
32
|
-
SI_UNIT_PREFIXES[:giga] = { :factor => 9, :symbol_prefix => "G" }
|
33
|
-
SI_UNIT_PREFIXES[:tera] = { :factor => 12, :symbol_prefix => "T" }
|
34
|
-
SI_UNIT_PREFIXES[:peta] = { :factor => 15, :symbol_prefix => "P" }
|
35
|
-
SI_UNIT_PREFIXES[:exa] = { :factor => 18, :symbol_prefix => "E" }
|
36
|
-
SI_UNIT_PREFIXES[:zetta] = { :factor => 21, :symbol_prefix => "Z" }
|
37
|
-
SI_UNIT_PREFIXES[:yotta] = { :factor => 24, :symbol_prefix => "Y" }
|
38
|
-
end # Units
|
11
|
+
Metric = Hash.new
|
12
|
+
Metric['carat'] = { :si => 0.0002, :units => [ { :unit => 'carat', :factor => 1, :aliases => ['CD'] } ] }
|
13
|
+
end
|
39
14
|
|
40
15
|
module Declarations
|
41
|
-
Systeme::Metric::Units::
|
42
|
-
|
43
|
-
plural_unit_name = unit.to_s + "s"
|
44
|
-
|
45
|
-
define_method(plural_unit_name.to_sym) { self * (10**(unit_factor_offset)) }
|
46
|
-
class_eval("alias :" + unit_data[:symbol].to_s + " :" + plural_unit_name)
|
47
|
-
class_eval("alias :" + plural_unit_name.chop + " :" + plural_unit_name)
|
48
|
-
Systeme::Registration::names << plural_unit_name << unit_data[:symbol].to_s << plural_unit_name.chop
|
49
|
-
|
50
|
-
Systeme::Metric::Units::SI_UNIT_PREFIXES.each do |prefix, prefix_data|
|
51
|
-
plural_prefixed_unit_name = prefix.to_s + plural_unit_name
|
52
|
-
prefixed_symbol = prefix_data[:symbol_prefix] + unit_data[:symbol].to_s
|
53
|
-
|
54
|
-
define_method(plural_prefixed_unit_name.to_sym) { self * (10**(prefix_data[:factor] + unit_factor_offset)) }
|
55
|
-
class_eval("alias :" + prefixed_symbol + " :" + plural_prefixed_unit_name)
|
56
|
-
class_eval("alias :" + plural_prefixed_unit_name.chop + " :" + plural_prefixed_unit_name)
|
57
|
-
Systeme::Registration::names << plural_prefixed_unit_name << prefixed_symbol << plural_prefixed_unit_name.chop
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end # Declarations
|
16
|
+
Systeme::Localize::declare_system(Systeme::Metric::Units::Metric)
|
17
|
+
end
|
61
18
|
end
|
62
19
|
end
|
data/lib/systeme.rb
CHANGED
@@ -1,12 +1,17 @@
|
|
1
|
+
require 'systeme/international'
|
1
2
|
require 'systeme/metric'
|
2
3
|
require 'systeme/imperial'
|
3
4
|
require 'systeme/conversions'
|
4
5
|
require 'systeme/registration'
|
5
6
|
|
6
7
|
module Systeme
|
8
|
+
include Systeme::International
|
9
|
+
include Systeme::Localize
|
7
10
|
include Systeme::Metric
|
8
11
|
include Systeme::Imperial
|
9
12
|
include Systeme::Conversions
|
10
13
|
end
|
11
14
|
|
12
|
-
Numeric.send :include, Systeme
|
15
|
+
Numeric.send :include, Systeme
|
16
|
+
|
17
|
+
puts 1.carat
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class InternationalTest < Test::Unit::TestCase
|
4
|
+
should "maintain base unit value through declaration" do
|
5
|
+
assert_equal 1, 1.kg
|
6
|
+
assert_equal 1, 1.m
|
7
|
+
end
|
8
|
+
|
9
|
+
should "express non-base unit value in the base unit" do
|
10
|
+
assert_equal 0.001, 1.g
|
11
|
+
assert_equal 1000, 1.km
|
12
|
+
end
|
13
|
+
|
14
|
+
should "convert value from base unit to any other unit" do
|
15
|
+
assert_equal 1000, 1.kg.in_g
|
16
|
+
assert_equal 100, 1.m.in_cm
|
17
|
+
end
|
18
|
+
|
19
|
+
should "convert value between arbitrary units" do
|
20
|
+
assert_equal 1, 1000.mg.in_g
|
21
|
+
assert_equal 10, 1.cm.in_mm
|
22
|
+
end
|
23
|
+
end
|
data/test/metric_test.rb
CHANGED
@@ -1,23 +1,7 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/test_helper'
|
2
2
|
|
3
|
-
class
|
4
|
-
should "
|
5
|
-
assert_equal
|
6
|
-
assert_equal 1, 1.m
|
7
|
-
end
|
8
|
-
|
9
|
-
should "express non-base unit value in the base unit" do
|
10
|
-
assert_equal 0.001, 1.g
|
11
|
-
assert_equal 1000, 1.km
|
12
|
-
end
|
13
|
-
|
14
|
-
should "convert value from base unit to any other unit" do
|
15
|
-
assert_equal 1000, 1.kg.in_g
|
16
|
-
assert_equal 100, 1.m.in_cm
|
17
|
-
end
|
18
|
-
|
19
|
-
should "convert value between arbitrary units" do
|
20
|
-
assert_equal 1, 1000.mg.in_g
|
21
|
-
assert_equal 10, 1.cm.in_mm
|
3
|
+
class ImperialTest < Test::Unit::TestCase
|
4
|
+
should "convert carat to kilogram" do
|
5
|
+
assert_equal 0.0002, 1.carat
|
22
6
|
end
|
23
7
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: farski-systeme
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chris Kalafarski
|
@@ -26,10 +26,13 @@ files:
|
|
26
26
|
- lib/systeme
|
27
27
|
- lib/systeme/conversions.rb
|
28
28
|
- lib/systeme/imperial.rb
|
29
|
+
- lib/systeme/international.rb
|
30
|
+
- lib/systeme/localize.rb
|
29
31
|
- lib/systeme/metric.rb
|
30
32
|
- lib/systeme/registration.rb
|
31
33
|
- lib/systeme.rb
|
32
34
|
- test/imperial_test.rb
|
35
|
+
- test/international_test.rb
|
33
36
|
- test/metric_test.rb
|
34
37
|
- test/test_helper.rb
|
35
38
|
has_rdoc: false
|