dimensional 0.1.1 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,2 +1,3 @@
1
1
  0.1.0
2
- 0.1.1 Simpler conversion system-to-system and redefinition of preference semantics.
2
+ 0.1.1 Simpler conversion system-to-system and redefinition of preference semantics.
3
+ 0.1.2 Cleanup tests to work with gem check -t
data/Rakefile CHANGED
@@ -1,11 +1,32 @@
1
+ require 'rubygems'
1
2
  require 'rake'
2
3
  require 'rake/testtask'
3
4
  require 'rake/rdoctask'
4
- require 'rake/gempackagetask'
5
- require 'rubygems'
5
+ require 'jeweler'
6
6
  require 'lib/dimensional/version'
7
7
 
8
- task :default => [:test]
8
+ spec = Gem::Specification.new do |spec|
9
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
10
+ spec.platform = Gem::Platform::RUBY
11
+ spec.name = %q{dimensional}
12
+ spec.version = Dimensional::VERSION
13
+ spec.required_ruby_version = '>= 1.6.8'
14
+ spec.date = Time.now.strftime("%Y-%m-%d")
15
+ spec.authors = ["Chris Hapgood"]
16
+ spec.email = %q{cch1@hapgoods.com}
17
+ spec.summary = %q{Dimensional provides handling for numbers with units.}
18
+ spec.homepage = %q{http://cho.hapgoods.com/dimensional}
19
+ spec.description = <<-EOF
20
+ Dimensional provides handling for dimensional values (numbers with units). Dimensional values
21
+ can be parsed, stored, converted and formatted for output.
22
+ EOF
23
+ spec.files = Dir['lib/**/*.rb'] + Dir['test/**/*.rb']
24
+ spec.files += ["README", "CHANGELOG", "LICENSE", "Rakefile", "test/helper.rb"]
25
+ spec.test_files = ["test/helper.rb"] + Dir['test/**/*_test.rb']
26
+ end
27
+
28
+ Jeweler::Tasks.new(spec)
29
+ Jeweler::GemcutterTasks.new
9
30
 
10
31
  Rake::TestTask.new do |t|
11
32
  t.libs << 'test'
@@ -14,26 +35,14 @@ Rake::TestTask.new do |t|
14
35
  end
15
36
  Rake::Task['test'].comment = "Run all tests in test/*_test.rb"
16
37
 
17
- spec = Gem::Specification.new do |s|
18
- s.platform = Gem::Platform::RUBY
19
- s.name = %q{dimensional}
20
- s.version = Dimensional::VERSION
21
- s.required_ruby_version = '>= 1.6.8'
22
- s.date = Time.now.strftime("%Y-%m-%d")
23
- s.authors = ["Chris Hapgood"]
24
- s.email = %q{cch1@hapgoods.com}
25
- s.summary = %q{Dimensional provides handling for numbers with units.}
26
- s.homepage = %q{http://cho.hapgoods.com/dimensional}
27
- s.description = <<-EOF
28
- Dimensional provides handling for dimensional values (numbers with units). Dimensional values
29
- can be parsed, stored, converted and formatted for output.
30
- EOF
31
- s.files = Dir['lib/**/*.rb'] + Dir['test/**/*.rb']
32
- s.files += ["README", "CHANGELOG", "LICENSE", "Rakefile"]
33
- s.test_files = Dir['test/**/*.rb']
38
+ Rake::RDocTask.new do |rdoc|
39
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
40
+
41
+ rdoc.rdoc_dir = 'rdoc'
42
+ rdoc.title = "tttt #{version}"
43
+ rdoc.rdoc_files.include('README*')
44
+ rdoc.rdoc_files.include('lib/**/*.rb')
34
45
  end
35
46
 
36
- Rake::GemPackageTask.new(spec) do |pkg|
37
- pkg.need_zip = true
38
- pkg.need_tar = false
39
- end
47
+ task :test => :check_dependencies
48
+ task :default => :test
@@ -1,3 +1,3 @@
1
1
  module Dimensional
2
- VERSION = "0.1.1"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -1,4 +1,4 @@
1
- require 'test/unit'
1
+ require 'helper'
2
2
  require 'dimensional/configurator'
3
3
 
4
4
  class ConfiguratorTest < Test::Unit::TestCase
@@ -1,5 +1,4 @@
1
- require 'test/unit'
2
- require 'dimensional/dimension'
1
+ require 'helper'
3
2
 
4
3
  class DimensionTest < Test::Unit::TestCase
5
4
  include Dimensional
@@ -1,5 +1,4 @@
1
- require 'test/unit'
2
- require 'dimensional'
1
+ require 'helper'
3
2
 
4
3
  class DimensionalTest < Test::Unit::TestCase
5
4
  include Dimensional
@@ -0,0 +1,9 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+
4
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ require 'dimensional'
7
+
8
+ class Test::Unit::TestCase
9
+ end
@@ -1,5 +1,4 @@
1
- require 'test/unit'
2
- require 'dimensional/metric'
1
+ require 'helper'
3
2
  require 'rational'
4
3
 
5
4
  class MetricTest < Test::Unit::TestCase
@@ -1,5 +1,4 @@
1
- require 'test/unit'
2
- require 'dimensional/system'
1
+ require 'helper'
3
2
 
4
3
  class SystemTest < Test::Unit::TestCase
5
4
  include Dimensional
@@ -1,5 +1,4 @@
1
- require 'test/unit'
2
- require 'dimensional/unit'
1
+ require 'helper'
3
2
 
4
3
  class UnitTest < Test::Unit::TestCase
5
4
  include Dimensional
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.1.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Hapgood
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-05 00:00:00 -05:00
12
+ date: 2010-02-06 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -19,36 +19,36 @@ executables: []
19
19
 
20
20
  extensions: []
21
21
 
22
- extra_rdoc_files: []
23
-
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README
24
25
  files:
25
- - lib/dimensional/configuration.rb
26
+ - CHANGELOG
27
+ - LICENSE
28
+ - README
29
+ - Rakefile
30
+ - lib/dimensional.rb
26
31
  - lib/dimensional/configurator.rb
27
32
  - lib/dimensional/dimension.rb
28
33
  - lib/dimensional/metric.rb
29
34
  - lib/dimensional/system.rb
30
35
  - lib/dimensional/unit.rb
31
36
  - lib/dimensional/version.rb
32
- - lib/dimensional.rb
33
- - test/configuration_test.rb
34
37
  - test/configurator_test.rb
35
38
  - test/demo.rb
36
39
  - test/dimension_test.rb
37
40
  - test/dimensional_test.rb
41
+ - test/helper.rb
38
42
  - test/metric_test.rb
39
43
  - test/system_test.rb
40
44
  - test/unit_test.rb
41
- - README
42
- - CHANGELOG
43
- - LICENSE
44
- - Rakefile
45
45
  has_rdoc: true
46
46
  homepage: http://cho.hapgoods.com/dimensional
47
47
  licenses: []
48
48
 
49
49
  post_install_message:
50
- rdoc_options: []
51
-
50
+ rdoc_options:
51
+ - --charset=UTF-8
52
52
  require_paths:
53
53
  - lib
54
54
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -71,9 +71,8 @@ signing_key:
71
71
  specification_version: 3
72
72
  summary: Dimensional provides handling for numbers with units.
73
73
  test_files:
74
- - test/configuration_test.rb
74
+ - test/helper.rb
75
75
  - test/configurator_test.rb
76
- - test/demo.rb
77
76
  - test/dimension_test.rb
78
77
  - test/dimensional_test.rb
79
78
  - test/metric_test.rb
@@ -1,121 +0,0 @@
1
- require 'dimensional/dimension'
2
- require 'dimensional/system'
3
- require 'dimensional/unit'
4
- require 'dimensional/metric'
5
- require 'forwardable'
6
-
7
- # Encapsulates the application-specific configuration of Dimensional elements, including
8
- # * Dimensions
9
- # * Systems, including a prioritized array of Systems to be consulted during parsing.
10
- # * Metrics, including associated Unit formatting and parsing options
11
- # * Units
12
- # Configurations can be constructed with a Configurator. They can also be copied and extended.
13
- #
14
- # Metric == context
15
- #
16
- # Configuration#options(unit, context) => options_hash
17
- # a hash of formatting and parsing options for the given unit in the given context
18
-
19
- # Configuration#format(unit, context = nil) => format_string
20
- # #strfmeasure-compatible format string for given unit and given context
21
-
22
- # Configuration#detectors(context = nil) => detector_hash
23
- # Measure#parse-compatible detector->unit pairs suitable for given context
24
-
25
- # Measure.parse(str, detectors) => measure
26
- # instance of measure with unit matched by detectors
27
-
28
- # Measure.new(value, unit, context) => measure
29
- # instance of measure with given value and unit, and with the given context.
30
-
31
- # Measure#strfmeasure(format) => str
32
- # formatted string representing the given measure
33
-
34
- # Measure#change_system(system) => measure
35
- # a
36
-
37
- # Create a Measure from a Numeric and Unit, optionally for a given Metric (load attribute from DB)
38
- # Create a Measure from a String for a given Metric with a preferred System (parse form input)
39
- # Convert a Measure from a given Unit to the most appropriate Unit in a given System
40
- # Format a Measure as a String given a specific format String
41
-
42
- class Configuration
43
- # A (unordered and unique) set of dimensions with generous lookup semantics
44
- # TODO: Prevent a composite dimension from being added unless its fundamentals are already included
45
- class Dimensions < Set
46
- def [](str)
47
- raise unless str
48
- detect{|d| d.name == str.to_s || d.symbol == str.to_s}
49
- end
50
- end
51
-
52
- # An ordered and unique collection of systems with generous lookup semantics and re-orderability. Order
53
- # is not defined prior to invoking #priortize.
54
- class Systems < DelegateClass(Set)
55
- include Enumerable
56
- attr_reader :priority
57
-
58
- def initialize(*args)
59
- @priority = []
60
- super(args)
61
- end
62
-
63
- # Prioritize the system according to the given array of lookup keys
64
- def prioritize(keys)
65
- new = keys.map{|k| self[k]}.uniq.compact
66
- priority.replace(new)
67
- end
68
-
69
- # Iteration is in priority order. We directly access the delegate object so that enumerable
70
- # methods needed here don't recurse (#each being called from a mixin apparently triggers this behavior)
71
- def each(&block)
72
- @_dc_obj.sort_by{|s| priority.index(s) || priority.size}.each(&block)
73
- end
74
-
75
- def [](str)
76
- detect {|s| (s == str.to_s) || (s.abbreviation == str.to_s) }
77
- end
78
- end
79
-
80
- attr_reader :dimensions, :systems, :metrics
81
-
82
- include Enumerable
83
- extend Forwardable
84
-
85
- def initialize
86
- @dimensions = Dimensions.new
87
- @systems = Systems.new
88
- @units = Set.new
89
- end
90
-
91
- def_delegators :@units, :each
92
- def_delegators :to_set, :size, :length, :empty?
93
-
94
- def add(u)
95
- systems << u.system
96
- # TODO: Add fundamental dimensions before adding composite dimension
97
- dimensions << u.dimension
98
- @units << u
99
- end
100
- alias << add
101
-
102
- def [](dim, sys, str)
103
- us = select{|u| u.dimension == dim && u.system == sys}
104
- us.detect{|u| str == u.name || (u.abbreviation && str == u.abbreviation)}
105
- end
106
-
107
- # Returns a new configuration with only the units with the specified dimension
108
- def dimension(d)
109
- scope(@units.select{|u| u.dimension == d})
110
- end
111
-
112
- private
113
- # Returns a new configuration scoped to the supplied units
114
- def scope(us)
115
- config = self.dup
116
- config.instance_eval do
117
- @units = us
118
- end
119
- config
120
- end
121
- end
@@ -1,217 +0,0 @@
1
- require 'test/unit'
2
- require 'dimensional/configuration'
3
-
4
- class ConfigurationTest < Test::Unit::TestCase
5
- include Dimensional
6
-
7
- def setup
8
- @s0 = System.new('International System', 'SI', "International System (kg, m)")
9
- @s1 = System.new('United States Customary', 'US', "US Customary (ft, lbs)")
10
- @dL = Dimension.new('Length')
11
- @dM = Dimension.new('Mass')
12
- @um = Unit.new('meter', @s0, @dL)
13
- @ug = Unit.new('gram', @s0, @dM)
14
- # System.register('British Admiralty', 'BA')
15
- # Dimension.register('Area', 'A', {Dimension::L => 2})
16
- end
17
-
18
- def test_create_configuration
19
- assert_instance_of Configuration, c = Configuration.new
20
- assert c.dimensions.empty?
21
- assert c.systems.empty?
22
- assert c.empty?
23
- end
24
-
25
- def test_add_dimension
26
- c = Configuration.new
27
- assert c.dimensions << @dL
28
- assert_same @dL, c.dimensions['Length']
29
- assert_same @dL, c.dimensions['L']
30
- assert_same @dL, c.dimensions[:L]
31
- end
32
-
33
- def test_add_system
34
- c = Configuration.new
35
- assert c.systems << @s0
36
- assert_same @s0, c.systems['SI']
37
- assert_same @s0, c.systems['International System']
38
- assert_same @s0, c.systems[:SI]
39
- end
40
-
41
- def test_prioritize_systems
42
- c = Configuration.new
43
- assert c.systems << @s0
44
- assert c.systems << @s1
45
- c.systems.prioritize([:US, :SI])
46
- assert_equal @s1, c.systems.first
47
- assert_equal [@s1, @s0], c.systems.to_a
48
- c.systems.prioritize([:SI, :US])
49
- assert_equal [@s0, @s1], c.systems.to_a
50
- end
51
-
52
- def test_add_unit
53
- c = Configuration.new
54
- assert c << @um
55
- assert c.include?(@um)
56
- assert c.dimensions.include?(@dL)
57
- assert c.systems.include?(@s0)
58
- assert_same @um, c[@dL, @s0, 'meter']
59
- end
60
-
61
- def test_scope
62
- c = Configuration.new
63
- c << @um
64
- c << @ug
65
- assert c.include?(@ug)
66
- c1 = c.dimension(@dL)
67
- assert c1.include?(@um)
68
- assert_same @um, c1[@dL, @s0, 'meter']
69
- assert !c1.include?(@ug)
70
- c2 = c1.dimension(@dM)
71
- assert c2.empty?
72
- assert !c2.any?
73
- assert c.include?(@ug)
74
- end
75
-
76
- # def test_start_configurator
77
- # assert Configurator.start
78
- # assert Configurator.start {true}
79
- # assert !Configurator.start {false}
80
- # end
81
- #
82
- # def test_start_configurator_with_context_args
83
- # assert_same Dimension::L, Configurator.start(:dimension => Dimension::L){context.dimension}
84
- # end
85
- #
86
- # def test_change_dimension_context_for_duration_of_block
87
- # test_context = self
88
- # Configurator.start do
89
- # dimension(Dimension::L) do
90
- # test_context.assert_equal Dimension::L, context.dimension
91
- # true
92
- # end
93
- # test_context.assert_nil context.dimension
94
- # end
95
- # end
96
- #
97
- # def test_change_system_context_for_duration_of_block
98
- # test_context = self
99
- # Configurator.start do
100
- # system(System::SI) do
101
- # test_context.assert_equal System::SI, context.system
102
- # true
103
- # end
104
- # test_context.assert_nil context.system
105
- # end
106
- # end
107
- #
108
- # def test_preserve_context_within_block
109
- # test_context = self
110
- # Dimensional::Configurator.start do
111
- # dimension(:L) do
112
- # system(:SI) do
113
- # base('meter') do
114
- # test_context.assert uc = context.unit
115
- # derive('centimeter', 'cm', 1e-2)
116
- # test_context.assert_same uc, context.unit
117
- # end
118
- # end
119
- # end
120
- # end
121
- # end
122
- #
123
- # def test_build_base_unit
124
- # Configurator.start(:system => System::SI, :dimension => Dimension::L) do
125
- # base('meter', 'm', :detector => /\A(meters?|m)\Z/)
126
- # end
127
- # assert_instance_of Unit, u = Unit[Dimension::L, System::SI, 'meter']
128
- # assert_same System::SI, u.system
129
- # assert_same Dimension::L, u.dimension
130
- # assert u.base?
131
- # assert_equal 'm', u.abbreviation
132
- # assert_instance_of Metric, m = Metric[:L]
133
- # assert m.preferences(u)[:detector]
134
- # end
135
- #
136
- # def test_build_derived_unit
137
- # Configurator.start(:system => System::SI, :dimension => Dimension::L) do
138
- # base('meter', 'm', :detector => /\A(meters?|m)\Z/) do
139
- # derive('centimeter', 'cm', 1e-2, :detector => /\A(centimeters?|cm)\Z/)
140
- # end
141
- # end
142
- # u0 = Unit[Dimension::L, System::SI, 'meter']
143
- # assert_instance_of Unit, u = Unit[Dimension::L, System::SI, 'centimeter']
144
- # assert_same System::SI, u.system
145
- # assert_same Dimension::L, u.dimension
146
- # assert_same u0, u.base
147
- # assert_equal 1E-2, u.factor
148
- # assert_equal 'cm', u.abbreviation
149
- # end
150
- #
151
- # def test_build_aliased_unit
152
- # Configurator.start(:system => System::SI, :dimension => Dimension::L) do
153
- # base('meter', 'm', :detector => /\A(meters?|m)\Z/) do
154
- # self.alias('decadecimeter')
155
- # end
156
- # end
157
- # u0 = Unit[Dimension::L, System::SI, 'meter']
158
- # assert_instance_of Unit, u = Unit[Dimension::L, System::SI, 'decadecimeter']
159
- # assert_same System::SI, u.system
160
- # assert_same Dimension::L, u.dimension
161
- # assert_same u0, u.base
162
- # assert_equal 1, u.factor
163
- # end
164
- #
165
- # def test_build_referenced_unit
166
- # Configurator.start(:system => System::SI, :dimension => Dimension::L) do
167
- # base('meter', 'm', :detector => /\A(meters?|m)\Z/)
168
- # system(:US) do
169
- # reference('yard', 'yd', Unit[:L, :SI, 'meter'], 0.9144, :detector => /\A(yards?|yds?)\Z/)
170
- # end
171
- # end
172
- # u0 = Unit[Dimension::L, System::SI, 'meter']
173
- # assert_instance_of Unit, u = Unit[Dimension::L, System::US, 'yard']
174
- # assert_equal 0.9144, u.factor
175
- # assert_same u0, u.base
176
- # end
177
- #
178
- # def test_build_combined_unit
179
- # Configurator.start(:system => System::SI, :dimension => Dimension::L) do
180
- # base('meter', 'm', :detector => /\A(meters?|m)\Z/)
181
- # system(:US) do
182
- # reference('yard', 'yd', Unit[:L, :SI, 'meter'], 0.9144, :detector => /\A(yards?|yds?)\Z/)
183
- # dimension(:A) do
184
- # combine('square yard', 'yd2', [Unit[:L, :US, 'yard'], Unit[:L, :US, 'yard']], :detector => /\A(yd|yard)2\Z/)
185
- # end
186
- # end
187
- # end
188
- # u1 = Unit[Dimension::L, System::US, 'yard']
189
- # assert_instance_of Unit, u = Unit[:A, :US, 'square yard']
190
- # assert_equal Dimension::A, u.dimension
191
- # assert_equal 0.83612736, u.factor
192
- # assert_equal [u1.base, u1.base], u.base
193
- # end
194
- #
195
- # def test_add_default_preferences
196
- # Configurator.start(:system => System::SI, :dimension => Dimension::L) do
197
- # base('meter', 'm')
198
- # end
199
- # u = Unit[Dimension::L, System::SI, 'meter']
200
- # m = Metric[:L]
201
- # assert d = m.preferences(u)[:detector]
202
- # assert_match d, 'meter'
203
- # assert f = m.preferences(u)[:format]
204
- # end
205
- #
206
- # def test_register_metric_options
207
- # Configurator.start(:system => System::SI, :dimension => Dimension::L) do
208
- # base('meter', 'm', :detector => /\A(meters?|m)\Z/) do
209
- # prefer(:length_over_all, :precision => 0.01)
210
- # end
211
- # end
212
- # u = Unit[:L, :SI, 'm']
213
- # assert_instance_of Metric, m = Metric[:length_over_all]
214
- # assert_same Metric[:L], m.parent
215
- # assert_equal 0.01, m.preferences(u)[:precision]
216
- # end
217
- end