amee-data-abstraction 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -2,7 +2,7 @@ source "http://rubygems.org"
2
2
 
3
3
  gem "amee", "~> 3.0"
4
4
  gem 'uuidtools', '2.1.2'
5
- gem 'quantify', '1.1.0'
5
+ gem 'quantify', '1.2.0'
6
6
 
7
7
  # Add dependencies to develop your gem here.
8
8
  # Include everything needed to run rake, tests, features, etc.
data/Gemfile.lock CHANGED
@@ -9,6 +9,7 @@ GEM
9
9
  nokogiri (~> 1.4.3.1)
10
10
  flexmock (0.9.0)
11
11
  git (1.2.5)
12
+ i18n (0.6.0)
12
13
  jeweler (1.6.4)
13
14
  bundler (~> 1.0)
14
15
  git (>= 1.2.5)
@@ -16,8 +17,9 @@ GEM
16
17
  json (1.5.3)
17
18
  log4r (1.1.9)
18
19
  nokogiri (1.4.3.1)
19
- quantify (1.1.0)
20
+ quantify (1.2.0)
20
21
  activesupport
22
+ i18n
21
23
  rake (0.9.2)
22
24
  rcov (0.9.10)
23
25
  rspec (1.3.0)
@@ -34,7 +36,7 @@ DEPENDENCIES
34
36
  bundler (~> 1.0.0)
35
37
  flexmock (> 0.8.6)
36
38
  jeweler (~> 1.6.4)
37
- quantify (= 1.1.0)
39
+ quantify (= 1.2.0)
38
40
  rcov
39
41
  rspec (= 1.3.0)
40
42
  rspec_spinner (= 1.1.3)
data/Rakefile CHANGED
@@ -22,7 +22,7 @@ Spec::Rake::SpecTask.new do |t|
22
22
  t.spec_opts = ['--options', "spec/spec.opts"]
23
23
  t.spec_files = FileList['spec/**/*_spec.rb']
24
24
  t.rcov = true
25
- t.rcov_opts = ['--exclude', 'spec,/*ruby*,']
25
+ t.rcov_opts = ['--exclude', 'spec,/*ruby*,/*gems*']
26
26
  end
27
27
 
28
28
  require 'jeweler'
@@ -99,4 +99,4 @@ Rake::RDocTask.new do |rdoc|
99
99
  rdoc.title = "amee-data-abstraction #{version}"
100
100
  rdoc.rdoc_files.include('README*')
101
101
  rdoc.rdoc_files.include('lib/**/*.rb')
102
- end
102
+ end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.1.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{amee-data-abstraction}
8
- s.version = "1.0.0"
8
+ s.version = "1.1.0"
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-08-11}
12
+ s.date = %q{2011-08-30}
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 = [
@@ -82,7 +82,7 @@ Gem::Specification.new do |s|
82
82
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
83
83
  s.add_runtime_dependency(%q<amee>, ["~> 3.0"])
84
84
  s.add_runtime_dependency(%q<uuidtools>, ["= 2.1.2"])
85
- s.add_runtime_dependency(%q<quantify>, ["= 1.1.0"])
85
+ s.add_runtime_dependency(%q<quantify>, ["= 1.2.0"])
86
86
  s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
87
87
  s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
88
88
  s.add_development_dependency(%q<rspec>, ["= 1.3.0"])
@@ -92,7 +92,7 @@ Gem::Specification.new do |s|
92
92
  else
93
93
  s.add_dependency(%q<amee>, ["~> 3.0"])
94
94
  s.add_dependency(%q<uuidtools>, ["= 2.1.2"])
95
- s.add_dependency(%q<quantify>, ["= 1.1.0"])
95
+ s.add_dependency(%q<quantify>, ["= 1.2.0"])
96
96
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
97
97
  s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
98
98
  s.add_dependency(%q<rspec>, ["= 1.3.0"])
@@ -103,7 +103,7 @@ Gem::Specification.new do |s|
103
103
  else
104
104
  s.add_dependency(%q<amee>, ["~> 3.0"])
105
105
  s.add_dependency(%q<uuidtools>, ["= 2.1.2"])
106
- s.add_dependency(%q<quantify>, ["= 1.1.0"])
106
+ s.add_dependency(%q<quantify>, ["= 1.2.0"])
107
107
  s.add_dependency(%q<bundler>, ["~> 1.0.0"])
108
108
  s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
109
109
  s.add_dependency(%q<rspec>, ["= 1.3.0"])
@@ -127,8 +127,13 @@ module AMEE
127
127
  # Return a string representing the AMEE Explorer URL which is assocaited
128
128
  # with <tt>self</tt>
129
129
  #
130
+ def discover_url
131
+ "http://discover.amee.com/categories#{path}"
132
+ end
133
+
130
134
  def explorer_url
131
- "http://explorer.amee.com/categories#{path}"
135
+ Rails.logger.info "#explorer_url method deprecated. Use #discover_url" if defined? Rails
136
+ discover_url
132
137
  end
133
138
 
134
139
  protected
@@ -235,6 +235,12 @@ module AMEE
235
235
  reset_invalidity_messages
236
236
  end
237
237
 
238
+ def ==(other_calc)
239
+ !terms.inject(false) do |boolean,term|
240
+ boolean || term != other_calc[term.label]
241
+ end && label == other_calc.label
242
+ end
243
+
238
244
  private
239
245
 
240
246
  # Empty the hash of error messages for term choices.
@@ -416,12 +422,9 @@ module AMEE
416
422
  #
417
423
  def create_profile_item
418
424
  raise Exceptions::AlreadyHaveProfileItem unless profile_item_uid.blank?
419
- location = AMEE::Profile::Item.create(profile_category,
420
- # call <tt>#data_item_uid</tt> on drill object rather than <tt>self</tt>
421
- # since there exists no profile item value yet
422
- amee_drill.data_item_uid,
423
- profile_options.merge(:get_item=>false,:name=>amee_name))
424
- self.profile_item_uid=location.split('/').last
425
+ @profile_item = AMEE::Profile::Item.create(profile_category,amee_drill.data_item_uid,
426
+ profile_options.merge(:get_item => true, :name => amee_name))
427
+ self.profile_item_uid = @profile_item.uid
425
428
  end
426
429
 
427
430
  # Methods which should be memoized once per interaction with AMEE to minimise
@@ -341,6 +341,12 @@ module AMEE
341
341
  @visible=true
342
342
  end
343
343
 
344
+ def ==(other_term)
345
+ !TermsList::TermProperties.inject(false) do |boolean,prop|
346
+ boolean || self.send(prop) != other_term.send(prop)
347
+ end
348
+ end
349
+
344
350
  # Returns <tt>true</tt> if <tt>self</tt> has a numeric value. That is, can
345
351
  # it have statistics applied? This method permits handling of term summing,
346
352
  # averaging, etc. Otherwise, returns <tt>false</tt>.
@@ -462,15 +468,11 @@ module AMEE
462
468
  # my_term.to_s :label #=> "12345 ton_us"
463
469
  #
464
470
  def to_s(format=:symbol)
465
- string = "#{value}"
466
- if unit and per_unit
467
- string += " #{(unit/per_unit).send(format)}"
468
- elsif unit
469
- string += " #{unit.send(format)}"
470
- elsif per_unit
471
- string += " #{(1/per_unit).send(format)}"
471
+ if has_numeric_value? && (unit || per_unit)
472
+ self.to_quantity.to_s(format)
473
+ else
474
+ "#{value}"
472
475
  end
473
- return string
474
476
  end
475
477
 
476
478
  # Checks that the units included in <tt>units</tt> are dimensionally
@@ -20,8 +20,11 @@ describe Calculation do
20
20
  it 'should return the outputs' do
21
21
  Transport.outputs.labels.should eql [:co2]
22
22
  end
23
- it 'should generate an explorer URL' do
24
- Transport.explorer_url.should eql 'http://explorer.amee.com/categories/transport/car/generic'
23
+ it 'should generate an discover URL' do
24
+ Transport.discover_url.should eql 'http://discover.amee.com/categories/transport/car/generic'
25
+ end
26
+ it 'should redirect to discover URL' do
27
+ Transport.explorer_url.should eql 'http://discover.amee.com/categories/transport/car/generic'
25
28
  end
26
29
  it 'can return a term via []' do
27
30
  Transport[:co2].label.should eql :co2
@@ -91,7 +91,7 @@ describe OngoingCalculation do
91
91
  mocker.select('size'=>'large')
92
92
  mocker.choices=[]
93
93
  mocker.drill
94
- mocker.profile_list.profile_category.timestamp.create.get
94
+ mocker.profile_list.profile_category.timestamp.create_and_get
95
95
  mycalc=Transport.begin_calculation
96
96
  mycalc.choose!('fuel'=>'diesel','size'=>'large','distance'=>5)
97
97
  mycalc.calculate!
@@ -109,7 +109,7 @@ describe OngoingCalculation do
109
109
  mocker.select('size'=>'large')
110
110
  mocker.choices=[]
111
111
  mocker.drill
112
- mocker.profile_list.profile_category.timestamp.create.get
112
+ mocker.profile_list.profile_category.timestamp.create_and_get
113
113
  mycalc=ElectricityAndTransport[:transport].begin_calculation
114
114
  mycalc.choose!('fuel'=>'diesel','size'=>'large','distance'=>5,'department'=>'stuff')
115
115
  mycalc.calculate!
@@ -166,7 +166,7 @@ describe OngoingCalculation do
166
166
  mocker.profile_list.get(true,true).delete
167
167
  existing_uid=mocker.uid
168
168
  mocker.select('size'=>'small')
169
- mocker.drill.profile_category.timestamp.create.get
169
+ mocker.drill.profile_category.timestamp.create_and_get
170
170
  mycalc.choose!(:profile_item_uid=>existing_uid,'fuel'=>'diesel','size'=>'small','distance'=>7)
171
171
  mycalc.calculate!
172
172
  mycalc.outputs.first.value.should eql :somenumber
@@ -205,7 +205,7 @@ describe OngoingCalculation do
205
205
  mocker.select('size'=>'large')
206
206
  mocker.choices=[]
207
207
  mocker.drill
208
- mocker.profile_list.profile_category.timestamp.create
208
+ mocker.profile_list.profile_category.timestamp.create_and_get
209
209
 
210
210
  mocker.existing={'distance'=>5}
211
211
  mocker.params={'distance'=>9}
@@ -231,11 +231,11 @@ describe OngoingCalculation do
231
231
  mocker.select('size'=>'large')
232
232
  mocker.choices=[]
233
233
  mocker.drill
234
- mocker.profile_list.profile_category.timestamp.create.get(true,true).delete
234
+ mocker.profile_list.profile_category.timestamp.create_and_get.get(true,true).delete
235
235
 
236
236
  mocker.select('size'=>'small')
237
- mocker.drill.create.get
238
-
237
+ mocker.drill.create_and_get
238
+
239
239
  mycalc=Transport.begin_calculation
240
240
  mycalc.choose!('fuel'=>'diesel','size'=>'large','distance'=>5)
241
241
  mycalc.calculate!
@@ -276,7 +276,7 @@ describe OngoingCalculation do
276
276
  mocker.select('size'=>'large')
277
277
  mocker.choices=[]
278
278
  mocker.drill
279
- mocker.profile_list.profile_category.timestamp.create.get
279
+ mocker.profile_list.profile_category.timestamp.create_and_get
280
280
  myproto=Transport.clone
281
281
  myproto.instance_eval{
282
282
  start_and_end_dates
@@ -326,7 +326,7 @@ describe OngoingCalculation do
326
326
  mocker.select('size'=>'large')
327
327
  mocker.choices=[]
328
328
  mocker.drill
329
- mocker.profile_list.profile_category.timestamp.create.get
329
+ mocker.profile_list.profile_category.timestamp.create_and_get
330
330
  mycalc=Transport.begin_calculation
331
331
  mycalc.should be_dirty
332
332
  mycalc.choose!('fuel'=>'diesel','size'=>'large','distance'=>5)
@@ -348,7 +348,7 @@ describe OngoingCalculation do
348
348
  mocker.select('size'=>'large')
349
349
  mocker.choices=[]
350
350
  mocker.drill
351
- mocker.profile_list.profile_category.timestamp.create.get
351
+ mocker.profile_list.profile_category.timestamp.create_and_get
352
352
  mycalc=Transport.begin_calculation
353
353
  mycalc.should be_dirty
354
354
  mycalc.choose!('fuel'=>'diesel','size'=>'large','distance'=>5)
@@ -490,5 +490,6 @@ describe OngoingCalculation do
490
490
  mycalc.invalidity_messages.keys.should be_empty
491
491
  mycalc[:size].value.should be_nil
492
492
  end
493
+
493
494
  end
494
495
 
@@ -161,27 +161,35 @@ describe Term do
161
161
  end
162
162
 
163
163
  it "should represent term as string with unit symbol if no argument provided" do
164
- Term.new {path :hello; value 12; default_unit :kg}.to_s.should == '12 kg'
165
- Term.new {path :hello; value 12; default_unit :kg; per_unit :h}.to_s.should == '12 kg h^-1'
166
- Term.new {path :hello; value 12; per_unit :h}.to_s.should == '12 h^-1'
164
+ Term.new {path :hello; value 12; default_unit :kg}.to_s.should == '12.0 kg'
165
+ Term.new {path :hello; value 12; default_unit :kg; per_unit :h}.to_s.should == '12.0 kg/h'
166
+ Term.new {path :hello; value 12; per_unit :h}.to_s.should == '12.0 h^-1'
167
167
  end
168
168
 
169
169
  it "should represent term as string with unit label" do
170
- Term.new {path :hello; value 12; default_unit :kg}.to_s(:label).should == '12 kg'
171
- Term.new {path :hello; value 12; default_unit :kg; per_unit :h}.to_s(:label).should == '12 kg/h'
172
- Term.new {path :hello; value 12; per_unit :h}.to_s(:label).should == '12 h^-1'
170
+ Term.new {path :hello; value 12; default_unit :kg}.to_s(:label).should == '12.0 kg'
171
+ Term.new {path :hello; value 12; default_unit :kg; per_unit :h}.to_s(:label).should == '12.0 kg/h'
172
+ Term.new {path :hello; value 12; per_unit :h}.to_s(:label).should == '12.0 h^-1'
173
173
  end
174
174
 
175
175
  it "should represent term as string with unit name" do
176
- Term.new {path :hello; value 12; default_unit :kg}.to_s(:name).should == '12 kilogram'
177
- Term.new {path :hello; value 12; default_unit :kg; per_unit :h}.to_s(:name).should == '12 kilogram per hour'
178
- Term.new {path :hello; value 12; per_unit :h}.to_s(:name).should == '12 per hour'
176
+ Term.new {path :hello; value 12; default_unit :kg}.to_s(:name).should == '12.0 kilograms'
177
+ Term.new {path :hello; value 12; default_unit :kg; per_unit :h}.to_s(:name).should == '12.0 kilograms per hour'
178
+ Term.new {path :hello; value 12; per_unit :h}.to_s(:name).should == '12.0 per hour'
179
179
  end
180
180
 
181
181
  it "should represent term as string with unit pluralized name" do
182
- Term.new {path :hello; value 12; default_unit :kg}.to_s(:pluralized_name).should == '12 kilograms'
183
- Term.new {path :hello; value 12; default_unit :kg; per_unit :h}.to_s(:pluralized_name).should == '12 kilograms per hour'
184
- Term.new {path :hello; value 12; per_unit :h}.to_s(:pluralized_name).should == '12 per hour'
182
+ Term.new {path :hello; value 12; default_unit :kg}.to_s(:pluralized_name).should == '12.0 kilograms'
183
+ Term.new {path :hello; value 12; default_unit :kg; per_unit :h}.to_s(:pluralized_name).should == '12.0 kilograms per hour'
184
+ Term.new {path :hello; value 12; per_unit :h}.to_s(:pluralized_name).should == '12.0 per hour'
185
+ end
186
+
187
+ it "should represent term as string with no units" do
188
+ Term.new {path :hello; value 12}.to_s(:pluralized_name).should == '12'
189
+ Term.new {path :hello; value 12}.to_s(:name).should == '12'
190
+ Term.new {path :hello; value 12}.to_s(:symbol).should == '12'
191
+ Term.new {path :hello; value 12}.to_s(:label).should == '12'
192
+ Term.new {path :hello; value 12}.to_s.should == '12'
185
193
  end
186
194
 
187
195
  it "should be recognised as numeric" do
@@ -345,9 +353,9 @@ describe Term do
345
353
  @term = Term.new { value 20; unit :kg; per_unit :h }
346
354
  quantity = @term.to_quantity
347
355
  quantity.unit.name.should eql "kilogram per hour"
348
- quantity.unit.symbol.should eql "kg h^-1"
356
+ quantity.unit.symbol.should eql "kg/h"
349
357
  quantity.value.should eql 20.0
350
- quantity.to_s.should eql "20.0 kg h^-1"
358
+ quantity.to_s.should eql "20.0 kg/h"
351
359
  end
352
360
 
353
361
  it "should return nil with no unit or per unit" do
@@ -381,5 +389,17 @@ describe Term do
381
389
  quantity.to_s.should eql "20.0 h^-1"
382
390
  end
383
391
  end
392
+
393
+ it "should recognise similar terms" do
394
+ @term1 = Term.new { value 20; per_unit :h }
395
+ @term2 = Term.new { value 20; per_unit :h }
396
+ (@term1 == @term2).should be_true
397
+ end
398
+
399
+ it "should recognise dissimilar terms" do
400
+ @term1 = Term.new { value 20; per_unit :h }
401
+ @term2 = Term.new { value 40; per_unit :h }
402
+ (@term1 == @term2).should be_false
403
+ end
384
404
 
385
405
  end
data/spec/spec_helper.rb CHANGED
@@ -176,6 +176,20 @@ class AMEEMocker
176
176
  return self
177
177
  end
178
178
 
179
+ def create_and_get
180
+ mock_pi=test.flexmock(
181
+ :amounts=>test.flexmock(:find=>{:value=>result}),
182
+ :data_item_uid=>dataitemuid,
183
+ :uid=>uid
184
+ )
185
+ test.flexmock(AMEE::Profile::Item).should_receive(:create).
186
+ with(catuid,dataitemuid,
187
+ {:get_item=>true,:name=>:sometimestamp}.merge(params)).
188
+ at_least.once.
189
+ and_return(mock_pi)
190
+ return self
191
+ end
192
+
179
193
  def create
180
194
  test.flexmock(AMEE::Profile::Item).should_receive(:create).
181
195
  with(catuid,dataitemuid,
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: 23
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
+ - 1
8
9
  - 0
9
- - 0
10
- version: 1.0.0
10
+ version: 1.1.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - James Hetherington
@@ -18,7 +18,7 @@ autorequire:
18
18
  bindir: bin
19
19
  cert_chain: []
20
20
 
21
- date: 2011-08-11 00:00:00 +01:00
21
+ date: 2011-08-30 00:00:00 +01:00
22
22
  default_executable:
23
23
  dependencies:
24
24
  - !ruby/object:Gem::Dependency
@@ -60,12 +60,12 @@ dependencies:
60
60
  requirements:
61
61
  - - "="
62
62
  - !ruby/object:Gem::Version
63
- hash: 19
63
+ hash: 31
64
64
  segments:
65
65
  - 1
66
- - 1
66
+ - 2
67
67
  - 0
68
- version: 1.1.0
68
+ version: 1.2.0
69
69
  name: quantify
70
70
  version_requirements: *id003
71
71
  - !ruby/object:Gem::Dependency