amee-data-abstraction 2.2.1 → 2.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/amee-data-abstraction.gemspec +3 -3
- data/lib/amee-data-abstraction/calculation_set.rb +14 -8
- data/lib/amee-data-abstraction/term.rb +21 -14
- data/lib/config/amee_units.rb +2 -1
- data/spec/amee-data-abstraction/calculation_set_spec.rb +24 -0
- data/spec/amee-data-abstraction/term_spec.rb +71 -1
- metadata +43 -43
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2.2.
|
1
|
+
2.2.2
|
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{amee-data-abstraction}
|
8
|
-
s.version = "2.2.
|
8
|
+
s.version = "2.2.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["James Hetherington", "Andrew Berkeley", "James Smith", "George Palmer"]
|
12
|
-
s.date = %q{2011-
|
12
|
+
s.date = %q{2011-11-03}
|
13
13
|
s.description = %q{Part of the AMEEappkit this gem provides a data abstraction layer, decreasing the amount and detail of development required}
|
14
14
|
s.email = %q{help@amee.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -73,7 +73,7 @@ Gem::Specification.new do |s|
|
|
73
73
|
s.homepage = %q{http://github.com/AMEE/amee-data-abstraction}
|
74
74
|
s.licenses = ["BSD 3-Clause"]
|
75
75
|
s.require_paths = ["lib"]
|
76
|
-
s.rubygems_version = %q{1.
|
76
|
+
s.rubygems_version = %q{1.5.3}
|
77
77
|
s.summary = %q{Calculation and form building tool hiding details of AMEEconnect}
|
78
78
|
|
79
79
|
if s.respond_to? :specification_version then
|
@@ -61,7 +61,7 @@ module AMEE
|
|
61
61
|
# filename will be based upon the master file with the extension .lock.rb.
|
62
62
|
#
|
63
63
|
def self.regenerate_lock_file(name,output_path=nil)
|
64
|
-
set =
|
64
|
+
set = load_set(name, :lock => false)
|
65
65
|
set.generate_lock_file(output_path)
|
66
66
|
end
|
67
67
|
|
@@ -86,11 +86,15 @@ module AMEE
|
|
86
86
|
|
87
87
|
protected
|
88
88
|
|
89
|
-
# Load a calculation set based on a filename or full path.
|
90
|
-
|
91
|
-
|
92
|
-
|
89
|
+
# Load a calculation set based on a filename or full path. Set
|
90
|
+
# <tt>:lock => false</tt> to specify loading directly from master
|
91
|
+
# configuration file.
|
92
|
+
#
|
93
|
+
def self.load_set(name,options={})
|
94
|
+
set = CalculationSet.new(name,:file => name) do
|
95
|
+
instance_eval(File.open(self.config_path(options)).read)
|
93
96
|
end
|
97
|
+
set
|
94
98
|
end
|
95
99
|
|
96
100
|
# Find the config file assocaited with <tt>name</tt>. The method first checks
|
@@ -177,10 +181,12 @@ module AMEE
|
|
177
181
|
|
178
182
|
# Returns the path to the configuration file for <tt>self</tt>. If a .lock
|
179
183
|
# file exists, this takes precedence, otherwise the master config file
|
180
|
-
# described by the <tt>#file</tt> attribute is returned.
|
184
|
+
# described by the <tt>#file</tt> attribute is returned. This arrangement
|
185
|
+
# can be overridden by passing <tt>:lock => false</tt>
|
181
186
|
#
|
182
|
-
def config_path
|
183
|
-
|
187
|
+
def config_path(options={})
|
188
|
+
options[:lock] = true unless options[:lock] == false
|
189
|
+
lock_file_exists? && options[:lock] ? lock_file_path : @file
|
184
190
|
end
|
185
191
|
|
186
192
|
# Returns the path to the configuration lock file
|
@@ -369,7 +369,11 @@ module AMEE
|
|
369
369
|
# averaging, etc. Otherwise, returns <tt>false</tt>.
|
370
370
|
#
|
371
371
|
def has_numeric_value?
|
372
|
-
set?
|
372
|
+
is_numeric? && set? && Float(value) rescue false
|
373
|
+
end
|
374
|
+
|
375
|
+
def is_numeric?
|
376
|
+
![:string, :text, :datetime, :time, :date ].include?(type)
|
373
377
|
end
|
374
378
|
|
375
379
|
# Returns a pretty print string representation of <tt>self</tt>
|
@@ -424,23 +428,26 @@ module AMEE
|
|
424
428
|
# my_term.convert_unit(:unit => <Quantify::Unit::SI ... >)
|
425
429
|
#
|
426
430
|
# If <tt>self</tt> does not hold a numeric value or either a unit or per
|
427
|
-
# unit attribute, <tt
|
431
|
+
# unit attribute, <tt>self</tt> is returned.
|
428
432
|
#
|
429
433
|
def convert_unit(options={})
|
430
|
-
return self unless
|
434
|
+
return self unless is_numeric? && (unit || per_unit)
|
435
|
+
|
431
436
|
new = clone
|
432
|
-
if
|
433
|
-
|
434
|
-
|
435
|
-
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
437
|
+
if has_numeric_value?
|
438
|
+
if options[:unit] && unit
|
439
|
+
new_unit = Unit.for(options[:unit])
|
440
|
+
Term.validate_dimensional_equivalence?(unit,new_unit)
|
441
|
+
new.value Quantity.new(new.value,new.unit).to(new_unit).value
|
442
|
+
end
|
443
|
+
if options[:per_unit] && per_unit
|
444
|
+
new_per_unit = Unit.for(options[:per_unit])
|
445
|
+
Term.validate_dimensional_equivalence?(per_unit,new_per_unit)
|
446
|
+
new.value Quantity.new(new.value,(1/new.per_unit)).to(Unit.for(new_per_unit)).value
|
447
|
+
end
|
443
448
|
end
|
449
|
+
new.unit options[:unit] if options[:unit]
|
450
|
+
new.per_unit options[:per_unit] if options[:per_unit]
|
444
451
|
return new
|
445
452
|
end
|
446
453
|
|
data/lib/config/amee_units.rb
CHANGED
@@ -106,6 +106,30 @@ describe CalculationSet do
|
|
106
106
|
set = CalculationSet.find('transport')
|
107
107
|
end
|
108
108
|
|
109
|
+
it "should load set from master file" do
|
110
|
+
CalculationSet.load_set('transport')
|
111
|
+
CalculationSet.sets[:transport].should be_a CalculationSet
|
112
|
+
end
|
113
|
+
|
114
|
+
it "should use lock file if exists" do
|
115
|
+
set = CalculationSet.load_set('transport')
|
116
|
+
set.should be_a CalculationSet
|
117
|
+
set.generate_lock_file
|
118
|
+
set.lock_file_exists?.should be_true
|
119
|
+
set.file.should eql "#{Rails.root}/config/calculations/transport.rb"
|
120
|
+
set.config_path.should eql "#{Rails.root}/config/calculations/transport.lock.rb"
|
121
|
+
end
|
122
|
+
|
123
|
+
it "should use master config file if instructed" do
|
124
|
+
set = CalculationSet.load_set('transport')
|
125
|
+
set.should be_a CalculationSet
|
126
|
+
set.generate_lock_file
|
127
|
+
set.lock_file_exists?.should be_true
|
128
|
+
set.file.should eql "#{Rails.root}/config/calculations/transport.rb"
|
129
|
+
set.config_path.should eql "#{Rails.root}/config/calculations/transport.lock.rb"
|
130
|
+
set.config_path(:lock => false).should eql "#{Rails.root}/config/calculations/transport.rb"
|
131
|
+
end
|
132
|
+
|
109
133
|
it "should generate set from file name using find method" do
|
110
134
|
CalculationSet.sets[:transport].should be_nil
|
111
135
|
set = CalculationSet.find('transport')
|
@@ -239,9 +239,17 @@ describe Term do
|
|
239
239
|
Term.new {path :hello; value 12; default_unit :kg}.has_numeric_value?.should be_true
|
240
240
|
end
|
241
241
|
|
242
|
+
it "string should be recognised as numeric if type not explicitly declared" do
|
243
|
+
Term.new {path :hello; value "12"; default_unit :kg}.has_numeric_value?.should be_true
|
244
|
+
end
|
245
|
+
|
242
246
|
it "should be recognised as non numeric" do
|
243
247
|
Term.new {path :hello; value 'bob'; default_unit :kg}.has_numeric_value?.should be_false
|
244
248
|
end
|
249
|
+
|
250
|
+
it "string should be recognised as non numeric if type declared" do
|
251
|
+
Term.new {path :hello; value '12'; default_unit :kg; type :string}.has_numeric_value?.should be_false
|
252
|
+
end
|
245
253
|
|
246
254
|
it "should convert the input to a String if the type is specified as such" do
|
247
255
|
Term.new {type :string; value 54}.value.should == "54"
|
@@ -287,7 +295,7 @@ describe Term do
|
|
287
295
|
end
|
288
296
|
|
289
297
|
it "should return self if not a numeric unit" do
|
290
|
-
@term = Term.new { value 'plane'; unit :kg }
|
298
|
+
@term = Term.new { value 'plane'; unit :kg; type :string }
|
291
299
|
@term.unit.label.should eql 'kg'
|
292
300
|
@term.value.should eql 'plane'
|
293
301
|
new_term = @term.convert_unit(:unit => :t)
|
@@ -356,6 +364,68 @@ describe Term do
|
|
356
364
|
new_term.value.should eql 1.2000
|
357
365
|
end
|
358
366
|
|
367
|
+
it "should convert unit if value empty" do
|
368
|
+
@term = Term.new { unit :kg }
|
369
|
+
@term.unit.symbol.should eql 'kg'
|
370
|
+
@term.value.should eql nil
|
371
|
+
new_term = @term.convert_unit(:unit => :t)
|
372
|
+
new_term.unit.symbol.should eql 't'
|
373
|
+
new_term.value.should eql nil
|
374
|
+
end
|
375
|
+
|
376
|
+
it "should convert per unit if value empty" do
|
377
|
+
@term = Term.new { unit :kg; per_unit :min }
|
378
|
+
@term.unit.symbol.should eql 'kg'
|
379
|
+
@term.per_unit.symbol.should eql 'min'
|
380
|
+
@term.value.should eql nil
|
381
|
+
new_term = @term.convert_unit(:per_unit => :h)
|
382
|
+
new_term.unit.symbol.should eql 'kg'
|
383
|
+
new_term.per_unit.symbol.should eql 'h'
|
384
|
+
new_term.value.should eql nil
|
385
|
+
end
|
386
|
+
|
387
|
+
it "should convert unit and per unit if value empty" do
|
388
|
+
@term = Term.new { unit :kg; per_unit :min }
|
389
|
+
@term.unit.symbol.should eql 'kg'
|
390
|
+
@term.per_unit.symbol.should eql 'min'
|
391
|
+
@term.value.should eql nil
|
392
|
+
new_term = @term.convert_unit( :unit => :t, :per_unit => :h )
|
393
|
+
new_term.unit.symbol.should eql 't'
|
394
|
+
new_term.per_unit.symbol.should eql 'h'
|
395
|
+
new_term.value.should eql nil
|
396
|
+
end
|
397
|
+
|
398
|
+
it "should convert unit if value 0" do
|
399
|
+
@term = Term.new { value 0; unit :kg }
|
400
|
+
@term.unit.symbol.should eql 'kg'
|
401
|
+
@term.value.should eql 0
|
402
|
+
new_term = @term.convert_unit(:unit => :t)
|
403
|
+
new_term.unit.symbol.should eql 't'
|
404
|
+
new_term.value.should eql 0.0
|
405
|
+
end
|
406
|
+
|
407
|
+
it "should convert per unit if value 0" do
|
408
|
+
@term = Term.new { value 0; unit :kg; per_unit :min }
|
409
|
+
@term.unit.symbol.should eql 'kg'
|
410
|
+
@term.per_unit.symbol.should eql 'min'
|
411
|
+
@term.value.should eql 0
|
412
|
+
new_term = @term.convert_unit(:per_unit => :h)
|
413
|
+
new_term.unit.symbol.should eql 'kg'
|
414
|
+
new_term.per_unit.symbol.should eql 'h'
|
415
|
+
new_term.value.should eql 0.0
|
416
|
+
end
|
417
|
+
|
418
|
+
it "should convert unit and per unit if value 0" do
|
419
|
+
@term = Term.new { value 0; unit :kg; per_unit :min }
|
420
|
+
@term.unit.symbol.should eql 'kg'
|
421
|
+
@term.per_unit.symbol.should eql 'min'
|
422
|
+
@term.value.should eql 0
|
423
|
+
new_term = @term.convert_unit( :unit => :t, :per_unit => :h )
|
424
|
+
new_term.unit.symbol.should eql 't'
|
425
|
+
new_term.per_unit.symbol.should eql 'h'
|
426
|
+
new_term.value.should eql 0.0
|
427
|
+
end
|
428
|
+
|
359
429
|
it "should raise error if trying to convert to non dimensionally equivalent unit" do
|
360
430
|
@term = Term.new { value 20; unit :kg; per_unit :min }
|
361
431
|
@term.unit.symbol.should eql 'kg'
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: amee-data-abstraction
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 3
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 2
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 2.2.
|
9
|
+
- 2
|
10
|
+
version: 2.2.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- James Hetherington
|
@@ -18,13 +18,12 @@ autorequire:
|
|
18
18
|
bindir: bin
|
19
19
|
cert_chain: []
|
20
20
|
|
21
|
-
date: 2011-
|
21
|
+
date: 2011-11-03 00:00:00 +00:00
|
22
22
|
default_executable:
|
23
23
|
dependencies:
|
24
24
|
- !ruby/object:Gem::Dependency
|
25
|
-
|
26
|
-
|
27
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
name: amee
|
26
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
28
27
|
none: false
|
29
28
|
requirements:
|
30
29
|
- - ~>
|
@@ -35,12 +34,12 @@ dependencies:
|
|
35
34
|
- 1
|
36
35
|
- 5
|
37
36
|
version: 4.1.5
|
38
|
-
name: amee
|
39
|
-
version_requirements: *id001
|
40
|
-
- !ruby/object:Gem::Dependency
|
41
37
|
prerelease: false
|
42
38
|
type: :runtime
|
43
|
-
requirement:
|
39
|
+
requirement: *id001
|
40
|
+
- !ruby/object:Gem::Dependency
|
41
|
+
name: uuidtools
|
42
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
44
43
|
none: false
|
45
44
|
requirements:
|
46
45
|
- - "="
|
@@ -51,12 +50,12 @@ dependencies:
|
|
51
50
|
- 1
|
52
51
|
- 2
|
53
52
|
version: 2.1.2
|
54
|
-
name: uuidtools
|
55
|
-
version_requirements: *id002
|
56
|
-
- !ruby/object:Gem::Dependency
|
57
53
|
prerelease: false
|
58
54
|
type: :runtime
|
59
|
-
requirement:
|
55
|
+
requirement: *id002
|
56
|
+
- !ruby/object:Gem::Dependency
|
57
|
+
name: quantify
|
58
|
+
version_requirements: &id003 !ruby/object:Gem::Requirement
|
60
59
|
none: false
|
61
60
|
requirements:
|
62
61
|
- - ~>
|
@@ -67,12 +66,12 @@ dependencies:
|
|
67
66
|
- 0
|
68
67
|
- 0
|
69
68
|
version: 2.0.0
|
70
|
-
name: quantify
|
71
|
-
version_requirements: *id003
|
72
|
-
- !ruby/object:Gem::Dependency
|
73
69
|
prerelease: false
|
74
|
-
type: :
|
75
|
-
requirement:
|
70
|
+
type: :runtime
|
71
|
+
requirement: *id003
|
72
|
+
- !ruby/object:Gem::Dependency
|
73
|
+
name: bundler
|
74
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
76
75
|
none: false
|
77
76
|
requirements:
|
78
77
|
- - ~>
|
@@ -83,12 +82,12 @@ dependencies:
|
|
83
82
|
- 0
|
84
83
|
- 0
|
85
84
|
version: 1.0.0
|
86
|
-
name: bundler
|
87
|
-
version_requirements: *id004
|
88
|
-
- !ruby/object:Gem::Dependency
|
89
85
|
prerelease: false
|
90
86
|
type: :development
|
91
|
-
requirement:
|
87
|
+
requirement: *id004
|
88
|
+
- !ruby/object:Gem::Dependency
|
89
|
+
name: jeweler
|
90
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
92
91
|
none: false
|
93
92
|
requirements:
|
94
93
|
- - ~>
|
@@ -99,12 +98,12 @@ dependencies:
|
|
99
98
|
- 6
|
100
99
|
- 4
|
101
100
|
version: 1.6.4
|
102
|
-
name: jeweler
|
103
|
-
version_requirements: *id005
|
104
|
-
- !ruby/object:Gem::Dependency
|
105
101
|
prerelease: false
|
106
102
|
type: :development
|
107
|
-
requirement:
|
103
|
+
requirement: *id005
|
104
|
+
- !ruby/object:Gem::Dependency
|
105
|
+
name: rspec
|
106
|
+
version_requirements: &id006 !ruby/object:Gem::Requirement
|
108
107
|
none: false
|
109
108
|
requirements:
|
110
109
|
- - "="
|
@@ -115,12 +114,12 @@ dependencies:
|
|
115
114
|
- 6
|
116
115
|
- 0
|
117
116
|
version: 2.6.0
|
118
|
-
name: rspec
|
119
|
-
version_requirements: *id006
|
120
|
-
- !ruby/object:Gem::Dependency
|
121
117
|
prerelease: false
|
122
118
|
type: :development
|
123
|
-
requirement:
|
119
|
+
requirement: *id006
|
120
|
+
- !ruby/object:Gem::Dependency
|
121
|
+
name: flexmock
|
122
|
+
version_requirements: &id007 !ruby/object:Gem::Requirement
|
124
123
|
none: false
|
125
124
|
requirements:
|
126
125
|
- - ">"
|
@@ -131,12 +130,12 @@ dependencies:
|
|
131
130
|
- 8
|
132
131
|
- 6
|
133
132
|
version: 0.8.6
|
134
|
-
name: flexmock
|
135
|
-
version_requirements: *id007
|
136
|
-
- !ruby/object:Gem::Dependency
|
137
133
|
prerelease: false
|
138
134
|
type: :development
|
139
|
-
requirement:
|
135
|
+
requirement: *id007
|
136
|
+
- !ruby/object:Gem::Dependency
|
137
|
+
name: rcov
|
138
|
+
version_requirements: &id008 !ruby/object:Gem::Requirement
|
140
139
|
none: false
|
141
140
|
requirements:
|
142
141
|
- - ">="
|
@@ -145,12 +144,12 @@ dependencies:
|
|
145
144
|
segments:
|
146
145
|
- 0
|
147
146
|
version: "0"
|
148
|
-
name: rcov
|
149
|
-
version_requirements: *id008
|
150
|
-
- !ruby/object:Gem::Dependency
|
151
147
|
prerelease: false
|
152
148
|
type: :development
|
153
|
-
requirement:
|
149
|
+
requirement: *id008
|
150
|
+
- !ruby/object:Gem::Dependency
|
151
|
+
name: rdoc
|
152
|
+
version_requirements: &id009 !ruby/object:Gem::Requirement
|
154
153
|
none: false
|
155
154
|
requirements:
|
156
155
|
- - ">="
|
@@ -159,8 +158,9 @@ dependencies:
|
|
159
158
|
segments:
|
160
159
|
- 0
|
161
160
|
version: "0"
|
162
|
-
|
163
|
-
|
161
|
+
prerelease: false
|
162
|
+
type: :development
|
163
|
+
requirement: *id009
|
164
164
|
description: Part of the AMEEappkit this gem provides a data abstraction layer, decreasing the amount and detail of development required
|
165
165
|
email: help@amee.com
|
166
166
|
executables: []
|
@@ -253,7 +253,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
253
253
|
requirements: []
|
254
254
|
|
255
255
|
rubyforge_project:
|
256
|
-
rubygems_version: 1.
|
256
|
+
rubygems_version: 1.5.3
|
257
257
|
signing_key:
|
258
258
|
specification_version: 3
|
259
259
|
summary: Calculation and form building tool hiding details of AMEEconnect
|