dimensional 0.0.3 → 0.0.4
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.
- data/Rakefile +1 -1
- data/lib/dimensional.rb +1 -1
- data/lib/dimensional/measure.rb +1 -1
- data/lib/dimensional/metric.rb +1 -1
- data/lib/dimensional/system.rb +7 -19
- data/test/system_test.rb +2 -2
- metadata +1 -1
data/Rakefile
CHANGED
@@ -16,7 +16,7 @@ Rake::Task['test'].comment = "Run all tests in test/*_test.rb"
|
|
16
16
|
spec = Gem::Specification.new do |s|
|
17
17
|
s.platform = Gem::Platform::RUBY
|
18
18
|
s.name = %q{dimensional}
|
19
|
-
s.version = "0.0.
|
19
|
+
s.version = "0.0.4"
|
20
20
|
s.required_ruby_version = '>= 1.6.8'
|
21
21
|
s.date = %q{2009-10-09}
|
22
22
|
s.authors = ["Chris Hapgood"]
|
data/lib/dimensional.rb
CHANGED
data/lib/dimensional/measure.rb
CHANGED
@@ -22,7 +22,7 @@ module Dimensional
|
|
22
22
|
units = metric.units
|
23
23
|
elements = str.to_s.scan(NUMERIC_REGEXP).map do |(v, us)|
|
24
24
|
units = units.select{|u| system == u.system} if system
|
25
|
-
unit = us.nil? ? units.first : units.detect{|u| u.match(us
|
25
|
+
unit = us.nil? ? units.first : units.detect{|u| u.match(us)}
|
26
26
|
raise ArgumentError, "Unit cannot be determined (#{us})" unless unit
|
27
27
|
system = unit.system
|
28
28
|
value = unit.dimension.nil? ? v.to_i : v.to_f
|
data/lib/dimensional/metric.rb
CHANGED
data/lib/dimensional/system.rb
CHANGED
@@ -1,14 +1,16 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
|
1
3
|
module Dimensional
|
2
4
|
# Represents a set of units for comprehensive measurement of physical quantities.
|
3
|
-
class System
|
5
|
+
class System < DelegateClass(String)
|
4
6
|
@registry = Hash.new
|
5
7
|
@abbreviation_registry = Hash.new
|
6
8
|
|
7
9
|
def self.register(*args)
|
8
10
|
s = new(*args)
|
9
|
-
raise "System #{s
|
10
|
-
raise "System #{s
|
11
|
-
@registry[s.
|
11
|
+
raise "System #{s} already exists" if @registry[s.to_sym]
|
12
|
+
raise "System #{s}'s abbreviation already exists" if @abbreviation_registry[s.abbreviation]
|
13
|
+
@registry[s.to_sym] = s
|
12
14
|
@abbreviation_registry[s.abbreviation.to_sym] = s if s.abbreviation
|
13
15
|
const_set(s.abbreviation, s) rescue nil # Not all symbols strings are valid constant names
|
14
16
|
s
|
@@ -20,11 +22,6 @@ module Dimensional
|
|
20
22
|
@abbreviation_registry[sym] || @registry[sym]
|
21
23
|
end
|
22
24
|
|
23
|
-
# Systems are expected to be declared 'universally' and always be in context so we only dump the name
|
24
|
-
def self._load(str)
|
25
|
-
@registry[str.to_sym]
|
26
|
-
end
|
27
|
-
|
28
25
|
# Purge all systems from storage.
|
29
26
|
def self.reset!
|
30
27
|
constants.each {|d| remove_const(d)}
|
@@ -35,17 +32,8 @@ module Dimensional
|
|
35
32
|
attr_reader :name, :abbreviation
|
36
33
|
|
37
34
|
def initialize(name, abbreviation = nil)
|
38
|
-
@name = name.to_s.freeze
|
39
35
|
@abbreviation = abbreviation && abbreviation.to_s
|
40
|
-
|
41
|
-
|
42
|
-
# Systems are expected to be declared 'universally' and always be in context so we only dump the name
|
43
|
-
def _dump(depth)
|
44
|
-
name
|
45
|
-
end
|
46
|
-
|
47
|
-
def to_s
|
48
|
-
name rescue super
|
36
|
+
super(name)
|
49
37
|
end
|
50
38
|
end
|
51
39
|
end
|
data/test/system_test.rb
CHANGED
@@ -10,13 +10,13 @@ class SystemTest < Test::Unit::TestCase
|
|
10
10
|
|
11
11
|
def test_create_new_measurement_system
|
12
12
|
assert_instance_of System, s = System.new('British Admiralty')
|
13
|
-
assert_equal 'British Admiralty', s
|
13
|
+
assert_equal 'British Admiralty', s
|
14
14
|
assert_nil s.abbreviation
|
15
15
|
end
|
16
16
|
|
17
17
|
def test_create_new_measurement_system_with_abbreviation
|
18
18
|
assert_instance_of System, s = System.new('British Admiralty', 'BA')
|
19
|
-
assert_equal 'British Admiralty', s
|
19
|
+
assert_equal 'British Admiralty', s
|
20
20
|
assert_equal 'BA', s.abbreviation
|
21
21
|
end
|
22
22
|
|