dimensional 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
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.3"
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
@@ -4,5 +4,5 @@ require 'dimensional/unit'
4
4
  require 'dimensional/measure'
5
5
 
6
6
  module Dimensional
7
- VERSION = "0.0.3"
7
+ VERSION = "0.0.4"
8
8
  end
@@ -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.to_s)}
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
@@ -40,7 +40,7 @@ module Dimensional
40
40
 
41
41
  def units
42
42
  baseline = parent ? parent.units : @units.keys
43
- baseline.sort_by{|u| 1.0 - preference(u)}
43
+ baseline.sort_by{|u| [1.0 - preference(u), u.name, u.system]}
44
44
  end
45
45
 
46
46
  def preferences(u)
@@ -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.name} already exists" if @registry[s.name]
10
- raise "System #{s.name}'s abbreviation already exists" if @abbreviation_registry[s.abbreviation]
11
- @registry[s.name.to_sym] = 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
- end
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.name
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.name
19
+ assert_equal 'British Admiralty', s
20
20
  assert_equal 'BA', s.abbreviation
21
21
  end
22
22
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dimensional
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Hapgood