amee-analytics 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.rvmrc +1 -0
- data/CHANGELOG.txt +4 -0
- data/Gemfile +16 -0
- data/LICENSE.txt +27 -0
- data/README.txt +94 -0
- data/Rakefile +99 -0
- data/VERSION +1 -0
- data/amee-analytics.gemspec +80 -0
- data/init.rb +4 -0
- data/lib/amee/data_abstraction/calculation_collection_analytics_support.rb +236 -0
- data/lib/amee/data_abstraction/result.rb +16 -0
- data/lib/amee/data_abstraction/term_analytics_support.rb +28 -0
- data/lib/amee/data_abstraction/terms_list_analytics_support.rb +347 -0
- data/lib/amee-analytics.rb +8 -0
- data/rails/init.rb +9 -0
- data/spec/amee/analytics/calculation_collection_spec.rb +234 -0
- data/spec/amee/analytics/term_spec.rb +11 -0
- data/spec/amee/analytics/terms_list_spec.rb +428 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +132 -0
- metadata +213 -0
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm 1.8.7@amee-analytics
|
data/CHANGELOG.txt
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
# Dependencies
|
4
|
+
gem "amee-data-abstraction","~> 1.1"
|
5
|
+
gem "amee-data-persistence","~> 1.1"
|
6
|
+
|
7
|
+
# Add dependencies to develop your gem here.
|
8
|
+
# Include everything needed to run rake, tests, features, etc.
|
9
|
+
group :development do
|
10
|
+
gem "jeweler", "~> 1.6.4"
|
11
|
+
gem 'rspec', '1.3.0'
|
12
|
+
gem 'rcov'
|
13
|
+
gem 'rspec_spinner', '1.1.3'
|
14
|
+
gem 'flexmock', '> 0.8.6'
|
15
|
+
gem 'activerecord', '~> 2.3.5'
|
16
|
+
end
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
Copyright (c) 2011, AMEE UK Ltd.
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
Redistributions of source code must retain the above copyright notice,
|
8
|
+
this list of conditions and the following disclaimer.
|
9
|
+
|
10
|
+
Redistributions in binary form must reproduce the above copyright notice,
|
11
|
+
this list of conditions and the following disclaimer in the documentation
|
12
|
+
and/or other materials provided with the distribution.
|
13
|
+
|
14
|
+
Neither the name of AMEE UK Ltd nor the names of its contributors may be
|
15
|
+
used to endorse or promote products derived from this software without
|
16
|
+
specific prior written permission.
|
17
|
+
|
18
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
19
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
20
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
21
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
22
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
23
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
24
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
25
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
26
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
27
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.txt
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
== amee-analytics
|
2
|
+
|
3
|
+
The amee-analytics gem provides support for handling collections of the class
|
4
|
+
<i>AMEE::DataAbstraction::OngoingCalculation</i> and performing analytical
|
5
|
+
operations across the collection.
|
6
|
+
|
7
|
+
Licensed under the BSD 3-Clause license (See LICENSE.txt for details)
|
8
|
+
|
9
|
+
Authors: James Smith, Andrew Berkeley, George Palmer
|
10
|
+
|
11
|
+
Copyright: Copyright (c) 2011 AMEE UK Ltd
|
12
|
+
|
13
|
+
Homepage: http://github.com/AMEE/amee-analytics
|
14
|
+
|
15
|
+
Documentation: http://rubydoc.info/gems/amee-analytics
|
16
|
+
|
17
|
+
== INSTALLATION
|
18
|
+
|
19
|
+
gem install amee-analytics
|
20
|
+
|
21
|
+
== REQUIREMENTS
|
22
|
+
|
23
|
+
* ruby 1.8.7
|
24
|
+
* rubygems >= 1.5
|
25
|
+
|
26
|
+
All gem requirements should be installed as part of the rubygems installation process
|
27
|
+
above, but are listed here for completeness.
|
28
|
+
|
29
|
+
* amee-data-abstraction ~> 1.1
|
30
|
+
* amee-data-persistence ~> 1.1
|
31
|
+
|
32
|
+
== USAGE
|
33
|
+
|
34
|
+
The library extends a number of classes within the <i>AMEE::DataAbstraction</i>
|
35
|
+
module:
|
36
|
+
|
37
|
+
1. <i>AMEE::DataAbstraction::CalculationCollection</i> is extended by the
|
38
|
+
<i>CalculationCollectionReportingSupport</i> module, providing the ability to filter
|
39
|
+
specific calculation terms, sort by term values, standardize units and perform
|
40
|
+
analytical operations on specific terms, such as sums, means, modes, and medians
|
41
|
+
|
42
|
+
2. <i>AMEE::DataAbstraction::TermsList</i> is extended by the
|
43
|
+
<i>TermsListReportingSupport</i> module. This provides much of the functionality
|
44
|
+
used by <i>CalculationCollectionReportingSupport</i>, allowing lists to be sorted
|
45
|
+
and summed, averaged, etc...
|
46
|
+
|
47
|
+
3. <i>AMEE::DataAbstraction::Term</i> is extended by the <i>TermReportingSupport</i>
|
48
|
+
module. This provides the ability to convert the units within a term (changing the
|
49
|
+
term value attribute accordingly), and is used by the operations provided in
|
50
|
+
<i>CalculationCollectionReportingSupport</i> and <i>TermsListReportingSupport</i>.
|
51
|
+
|
52
|
+
4. A new subclass of <i>AMEE::DataAbstraction::Term</i> is defined, <i>Result</i>.
|
53
|
+
This provides a simple container for returning the result of a <i>TermsList</i>
|
54
|
+
analytical operation (e.g. sum, mean) complete with label, value, unit, etc...
|
55
|
+
|
56
|
+
=Example usage
|
57
|
+
|
58
|
+
# find method returns instance of CalculationCollection
|
59
|
+
my_calculations = OngoingCalculation.find_by_type(:all, :electricity)
|
60
|
+
#=> <AMEE::DataAbstraction::CalculationCollection ... >
|
61
|
+
|
62
|
+
# Dynamic label-derived method returns TermsList of the named term from each
|
63
|
+
# calculation in the set
|
64
|
+
|
65
|
+
my_calculations.country #=> <AMEE::DataAbstraction::TermsList ... >
|
66
|
+
|
67
|
+
my_calculations.energy #=> <AMEE::DataAbstraction::TermsList ... >
|
68
|
+
|
69
|
+
my_calculations.co2 #=> <AMEE::DataAbstraction::TermsList ... >
|
70
|
+
|
71
|
+
# Analytical operations can be applied to lists of terms. These return new
|
72
|
+
# objects, of the Result class. #to_s used here for illustrative purposes
|
73
|
+
|
74
|
+
my_calculations.country.sum.to_s #=> "0.0"
|
75
|
+
|
76
|
+
my_calculations.energy.sum.to_s #=> "23456 kWh"
|
77
|
+
|
78
|
+
my_calculations.co2.sum.to_s #=> "12345 kg"
|
79
|
+
|
80
|
+
my_calculations.co2.sum(:lb).to_s #=> "23456 lb"
|
81
|
+
|
82
|
+
my_calculations.country.mode.to_s #=> "Sweden"
|
83
|
+
|
84
|
+
my_calculations.co2.mean.to_s #=> "4512.5 kg"
|
85
|
+
|
86
|
+
my_calculations.co2.mean('t').to_s #=> "4.5125 t"
|
87
|
+
|
88
|
+
my_calculations.co2.median.to_s #=> "4567 kg"
|
89
|
+
|
90
|
+
my_calculations.co2.predominant_unit #=> "kg"
|
91
|
+
|
92
|
+
my_calculations.sort_by_co2 #=> <AMEE::DataAbstraction::CalculationCollection ... >
|
93
|
+
|
94
|
+
my_calculations.sort_by_co2! #=> <AMEE::DataAbstraction::CalculationCollection ... >
|
data/Rakefile
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
require 'spec'
|
14
|
+
require 'spec/rake/spectask'
|
15
|
+
|
16
|
+
task :default => [:spec]
|
17
|
+
|
18
|
+
Spec::Rake::SpecTask.new do |t|
|
19
|
+
t.spec_opts = ['--options', "spec/spec.opts"]
|
20
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
21
|
+
t.rcov = true
|
22
|
+
t.rcov_opts = ['--exclude', 'spec,/*ruby*,/*gems*']
|
23
|
+
end
|
24
|
+
|
25
|
+
require 'jeweler'
|
26
|
+
# Fix for Jeweler to use stable branch
|
27
|
+
class Jeweler
|
28
|
+
module Commands
|
29
|
+
class ReleaseToGit
|
30
|
+
def run
|
31
|
+
unless clean_staging_area?
|
32
|
+
system "git status"
|
33
|
+
raise "Unclean staging area! Be sure to commit or .gitignore everything first. See `git status` above."
|
34
|
+
end
|
35
|
+
repo.checkout('stable')
|
36
|
+
repo.push('origin', 'stable')
|
37
|
+
if release_not_tagged?
|
38
|
+
output.puts "Tagging #{release_tag}"
|
39
|
+
repo.add_tag(release_tag)
|
40
|
+
output.puts "Pushing #{release_tag} to origin"
|
41
|
+
repo.push('origin', release_tag)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
class ReleaseGemspec
|
46
|
+
def run
|
47
|
+
unless clean_staging_area?
|
48
|
+
system "git status"
|
49
|
+
raise "Unclean staging area! Be sure to commit or .gitignore everything first. See `git status` above."
|
50
|
+
end
|
51
|
+
repo.checkout('stable')
|
52
|
+
regenerate_gemspec!
|
53
|
+
commit_gemspec! if gemspec_changed?
|
54
|
+
output.puts "Pushing stable to origin"
|
55
|
+
repo.push('origin', 'stable')
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
Jeweler::Tasks.new do |gem|
|
62
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
63
|
+
gem.name = "amee-analytics"
|
64
|
+
gem.homepage = "http://github.com/AMEE/amee-analytics"
|
65
|
+
gem.license = "MIT"
|
66
|
+
gem.summary = %Q{Analytics module for use with AMEE AppKit}
|
67
|
+
gem.description = %Q{Part of the AMEE AppKit, this gem provides the ability to do mathmatical operations over a set of calculations}
|
68
|
+
gem.email = "help@amee.com"
|
69
|
+
gem.authors = ["James Hetherington", "Andrew Berkeley", "James Smith", "George Palmer"]
|
70
|
+
# dependencies defined in Gemfile
|
71
|
+
end
|
72
|
+
Jeweler::RubygemsDotOrgTasks.new
|
73
|
+
|
74
|
+
require 'rake/testtask'
|
75
|
+
Rake::TestTask.new(:test) do |test|
|
76
|
+
test.libs << 'lib' << 'test'
|
77
|
+
test.pattern = 'test/**/test_*.rb'
|
78
|
+
test.verbose = true
|
79
|
+
end
|
80
|
+
|
81
|
+
require 'rcov/rcovtask'
|
82
|
+
Rcov::RcovTask.new do |test|
|
83
|
+
test.libs << 'test'
|
84
|
+
test.pattern = 'test/**/test_*.rb'
|
85
|
+
test.verbose = true
|
86
|
+
test.rcov_opts << '--exclude "gems/*"'
|
87
|
+
end
|
88
|
+
|
89
|
+
task :default => :test
|
90
|
+
|
91
|
+
require 'rake/rdoctask'
|
92
|
+
Rake::RDocTask.new do |rdoc|
|
93
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
94
|
+
|
95
|
+
rdoc.rdoc_dir = 'rdoc'
|
96
|
+
rdoc.title = "amee-analytics #{version}"
|
97
|
+
rdoc.rdoc_files.include('README*')
|
98
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
99
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.1
|
@@ -0,0 +1,80 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{amee-analytics}
|
8
|
+
s.version = "1.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["James Hetherington", "Andrew Berkeley", "James Smith", "George Palmer"]
|
12
|
+
s.date = %q{2011-08-31}
|
13
|
+
s.description = %q{Part of the AMEE AppKit, this gem provides the ability to do mathmatical operations over a set of calculations}
|
14
|
+
s.email = %q{help@amee.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.txt"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".rvmrc",
|
21
|
+
"CHANGELOG.txt",
|
22
|
+
"Gemfile",
|
23
|
+
"LICENSE.txt",
|
24
|
+
"README.txt",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"amee-analytics.gemspec",
|
28
|
+
"init.rb",
|
29
|
+
"lib/amee-analytics.rb",
|
30
|
+
"lib/amee/data_abstraction/calculation_collection_analytics_support.rb",
|
31
|
+
"lib/amee/data_abstraction/result.rb",
|
32
|
+
"lib/amee/data_abstraction/term_analytics_support.rb",
|
33
|
+
"lib/amee/data_abstraction/terms_list_analytics_support.rb",
|
34
|
+
"rails/init.rb",
|
35
|
+
"spec/amee/analytics/calculation_collection_spec.rb",
|
36
|
+
"spec/amee/analytics/term_spec.rb",
|
37
|
+
"spec/amee/analytics/terms_list_spec.rb",
|
38
|
+
"spec/spec.opts",
|
39
|
+
"spec/spec_helper.rb"
|
40
|
+
]
|
41
|
+
s.homepage = %q{http://github.com/AMEE/amee-analytics}
|
42
|
+
s.licenses = ["MIT"]
|
43
|
+
s.require_paths = ["lib"]
|
44
|
+
s.rubygems_version = %q{1.6.2}
|
45
|
+
s.summary = %q{Analytics module for use with AMEE AppKit}
|
46
|
+
|
47
|
+
if s.respond_to? :specification_version then
|
48
|
+
s.specification_version = 3
|
49
|
+
|
50
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
51
|
+
s.add_runtime_dependency(%q<amee-data-abstraction>, ["~> 1.1"])
|
52
|
+
s.add_runtime_dependency(%q<amee-data-persistence>, ["~> 1.1"])
|
53
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
|
54
|
+
s.add_development_dependency(%q<rspec>, ["= 1.3.0"])
|
55
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
56
|
+
s.add_development_dependency(%q<rspec_spinner>, ["= 1.1.3"])
|
57
|
+
s.add_development_dependency(%q<flexmock>, ["> 0.8.6"])
|
58
|
+
s.add_development_dependency(%q<activerecord>, ["~> 2.3.5"])
|
59
|
+
else
|
60
|
+
s.add_dependency(%q<amee-data-abstraction>, ["~> 1.1"])
|
61
|
+
s.add_dependency(%q<amee-data-persistence>, ["~> 1.1"])
|
62
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
63
|
+
s.add_dependency(%q<rspec>, ["= 1.3.0"])
|
64
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
65
|
+
s.add_dependency(%q<rspec_spinner>, ["= 1.1.3"])
|
66
|
+
s.add_dependency(%q<flexmock>, ["> 0.8.6"])
|
67
|
+
s.add_dependency(%q<activerecord>, ["~> 2.3.5"])
|
68
|
+
end
|
69
|
+
else
|
70
|
+
s.add_dependency(%q<amee-data-abstraction>, ["~> 1.1"])
|
71
|
+
s.add_dependency(%q<amee-data-persistence>, ["~> 1.1"])
|
72
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
73
|
+
s.add_dependency(%q<rspec>, ["= 1.3.0"])
|
74
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
75
|
+
s.add_dependency(%q<rspec_spinner>, ["= 1.1.3"])
|
76
|
+
s.add_dependency(%q<flexmock>, ["> 0.8.6"])
|
77
|
+
s.add_dependency(%q<activerecord>, ["~> 2.3.5"])
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
data/init.rb
ADDED
@@ -0,0 +1,236 @@
|
|
1
|
+
# Copyright (C) 2011 AMEE UK Ltd. - http://www.amee.com
|
2
|
+
# Released as Open Source Software under the BSD 3-Clause license. See LICENSE.txt for details.
|
3
|
+
|
4
|
+
# :title: Module: AMEE::DataAbstraction::CalculationCollectionAnalyticsSupport
|
5
|
+
|
6
|
+
module AMEE
|
7
|
+
module DataAbstraction
|
8
|
+
|
9
|
+
# Mixin module for the <i>AMEE::DataAbstraction::CalculationCollection</i>
|
10
|
+
# class, providing methods for handling collections of calculations.
|
11
|
+
#
|
12
|
+
module CalculationCollectionAnalyticsSupport
|
13
|
+
|
14
|
+
# Returns <tt>true</tt> if all calculations in <tt>self</tt> are
|
15
|
+
# representatives of the same prototype calculation. Otherwise,
|
16
|
+
# returns <tt>false</tt>.
|
17
|
+
#
|
18
|
+
def homogeneous?
|
19
|
+
calculation_labels.size == 1
|
20
|
+
end
|
21
|
+
|
22
|
+
# Returns <tt>true</tt> if all calculations in <tt>self</tt> are NOT
|
23
|
+
# representatives of the same prototype calculation. Otherwise,
|
24
|
+
# returns <tt>false</tt>.
|
25
|
+
#
|
26
|
+
def heterogeneous?
|
27
|
+
!homogeneous?
|
28
|
+
end
|
29
|
+
|
30
|
+
# Returns an array containing all of the unique labels for calculations
|
31
|
+
# held in <tt>self</tt>.
|
32
|
+
#
|
33
|
+
def calculation_labels
|
34
|
+
map(&:label).uniq
|
35
|
+
end
|
36
|
+
|
37
|
+
def +(other_calc_coll)
|
38
|
+
self.class.new(self.to_a + other_calc_coll.to_a)
|
39
|
+
end
|
40
|
+
|
41
|
+
def -(other_calc_coll)
|
42
|
+
other_calc_coll = [other_calc_coll].flatten
|
43
|
+
self.delete_if { |calc| other_calc_coll.include?(calc) }
|
44
|
+
end
|
45
|
+
|
46
|
+
# Similar to <tt>#sort_by!</tt> but returns a new instance of
|
47
|
+
# <i>CalculationCollection</i> arranged according to the values on the
|
48
|
+
# term labelled <tt>term</tt>. E.g.
|
49
|
+
#
|
50
|
+
# my_calculation_collection.sort_by :co2
|
51
|
+
#
|
52
|
+
# #=> <AMEE::DataAbstraction::CalculationCollection ... >
|
53
|
+
#
|
54
|
+
def sort_by(term)
|
55
|
+
term = term.to_sym unless term.is_a? Symbol
|
56
|
+
CalculationCollection.new(send(term).sort_by(:value).map(&:parent))
|
57
|
+
end
|
58
|
+
|
59
|
+
# Sorts the calculation collection in place according to the values on the
|
60
|
+
# term labelled <tt>term</tt>, returning <tt>self</tt>.
|
61
|
+
#
|
62
|
+
# my_calculation_collection.sort_by! :mass
|
63
|
+
#
|
64
|
+
# #=> <AMEE::DataAbstraction::CalculationCollection ... >
|
65
|
+
#
|
66
|
+
def sort_by!(term)
|
67
|
+
replace(sort_by(term))
|
68
|
+
end
|
69
|
+
|
70
|
+
# Returns a new instance of <i>CalculationCollection</i> containing the same
|
71
|
+
# calculations contained within <tt>self</tt> but with the units of the term
|
72
|
+
# <tt>term</tt> standardized on each.
|
73
|
+
#
|
74
|
+
# If no further arguments are provided, the standardized units represent
|
75
|
+
# those which currently predominate amongst the relevent terms. Otherwise,
|
76
|
+
# the specific unit and/or per unit which are required for each instance
|
77
|
+
# of <tt>term</tt> can be explicitly specified as the second and third
|
78
|
+
# arguments respecitively. Units can be specified in any of the formats
|
79
|
+
# which are acceptable to the <tt>Quantify::Unit.for</tt> method (i.e.
|
80
|
+
# stringified unit names or symbols, symbolized labels, or
|
81
|
+
# <tt>Quantify::Unit::Base</tt> instances of the required unit). E.g.
|
82
|
+
#
|
83
|
+
# my_calculation_collection.standardize_units(:mass)
|
84
|
+
#
|
85
|
+
# #=> <AMEE::DataAbstraction::CalculationCollection ... >
|
86
|
+
#
|
87
|
+
# my_calculation_collection.standardize_units(:mass, :lb)
|
88
|
+
#
|
89
|
+
# #=> <AMEE::DataAbstraction::CalculationCollection ... >
|
90
|
+
#
|
91
|
+
# my_calculation_collection.standardize_units(:mass, 'pound')
|
92
|
+
#
|
93
|
+
# #=> <AMEE::DataAbstraction::CalculationCollection ... >
|
94
|
+
#
|
95
|
+
# my_calculation_collection.standardize_units(:mass, <Quantify::Unit::NonSI>)
|
96
|
+
#
|
97
|
+
# #=> <AMEE::DataAbstraction::CalculationCollection ... >
|
98
|
+
#
|
99
|
+
# my_calculation_collection.standardize_units(:distance, :km, 'year')
|
100
|
+
#
|
101
|
+
# #=> <AMEE::DataAbstraction::CalculationCollection ... >
|
102
|
+
#
|
103
|
+
def standardize_units(term,unit=nil,per_unit=nil)
|
104
|
+
term = term.to_sym unless term.is_a? Symbol
|
105
|
+
new_calcs = send(term).standardize_units(unit,per_unit).map do |term|
|
106
|
+
calc = term.parent
|
107
|
+
calc.contents[term.label] = term
|
108
|
+
calc
|
109
|
+
end
|
110
|
+
CalculationCollection.new(new_calcs)
|
111
|
+
end
|
112
|
+
|
113
|
+
# Similar to <tt>#standardize_units</tt> but standardizes units in place,
|
114
|
+
# returning <tt>self</tt>
|
115
|
+
#
|
116
|
+
def standardize_units!(term,unit=nil,per_unit=nil)
|
117
|
+
new_calcs = standardize_units(term,unit,per_unit)
|
118
|
+
replace(new_calcs)
|
119
|
+
end
|
120
|
+
|
121
|
+
# Returns an array of instances of the <tt>Result</tt> class representing the
|
122
|
+
# sums of all outputs represented within the collection
|
123
|
+
#
|
124
|
+
def sum_all_outputs
|
125
|
+
TermsList.new(terms.outputs.visible.labels.uniq.map { |o| send(o).sum })
|
126
|
+
end
|
127
|
+
|
128
|
+
# Returns a new instance of the class <tt>TermsList</tt> representing either
|
129
|
+
# CO2 or CO2e outputs from each calculation
|
130
|
+
#
|
131
|
+
def co2_or_co2e_outputs
|
132
|
+
terms = TermsList.new
|
133
|
+
each do |calculation|
|
134
|
+
if calculation['co2e']
|
135
|
+
terms << calculation['co2e']
|
136
|
+
elsif calculation.outputs.visible.labels.size == 1 && calculation.outputs.visible.labels.first == :co2
|
137
|
+
terms << calculation['co2']
|
138
|
+
end
|
139
|
+
end
|
140
|
+
return terms
|
141
|
+
end
|
142
|
+
|
143
|
+
# Call the <tt>#calculate!</tt> method on all calculations contained within
|
144
|
+
# <tt>self</tt>.
|
145
|
+
#
|
146
|
+
def calculate_all!
|
147
|
+
each { |calc| calc.calculate! }
|
148
|
+
end
|
149
|
+
|
150
|
+
# Call the <tt>#save</tt> method on all calculations contained within
|
151
|
+
# <tt>self</tt>.
|
152
|
+
#
|
153
|
+
def save_all!
|
154
|
+
each { |calc| calc.save }
|
155
|
+
end
|
156
|
+
|
157
|
+
# Returns a terms list of all terms held by calculations contained within
|
158
|
+
# <tt>self</tt>.
|
159
|
+
#
|
160
|
+
def terms
|
161
|
+
TermsList.new( (self.map { |calc| calc.terms.map { |term| term } }).flatten )
|
162
|
+
end
|
163
|
+
|
164
|
+
TermsList::Selectors.each do |sel|
|
165
|
+
delegate sel,:to=>:terms
|
166
|
+
end
|
167
|
+
|
168
|
+
# Return an instance of <i>TermsList</i> containing only terms labelled
|
169
|
+
# :type.
|
170
|
+
#
|
171
|
+
# This method overrides the standard #type method (which is deprecated) and
|
172
|
+
# mimics the functionality provied by the first #method_missing method in
|
173
|
+
# dynamically retrieving a subset of terms according their labels.
|
174
|
+
#
|
175
|
+
def type
|
176
|
+
terms.type
|
177
|
+
end
|
178
|
+
|
179
|
+
def respond_to?(method)
|
180
|
+
if terms.labels.include? method.to_sym
|
181
|
+
return true
|
182
|
+
elsif method.to_s =~ /sort_by_(.*)!/ and terms.labels.include? $1.to_sym
|
183
|
+
return true
|
184
|
+
elsif method.to_s =~ /sort_by_(.*)/ and terms.labels.include? $1.to_sym
|
185
|
+
return true
|
186
|
+
else
|
187
|
+
super
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
# Syntactic sugar for several instance methods.
|
192
|
+
#
|
193
|
+
# ---
|
194
|
+
#
|
195
|
+
# Call a method on <tt>self</tt> which named after a specific term label
|
196
|
+
# contained with an associated calculation and return an instance of the
|
197
|
+
# <tt>TermsList</tt> class contain each of those terms. E.g.,
|
198
|
+
#
|
199
|
+
# my_terms = my_calculation_collection.type #=> <AMEE::DataAbstraction::TermsList>
|
200
|
+
# my_terms.label #=> :type
|
201
|
+
#
|
202
|
+
# my_terms = my_calculation_collection.mass #=> <AMEE::DataAbstraction::TermsList>
|
203
|
+
# my_terms.label #=> :mass
|
204
|
+
#
|
205
|
+
# my_terms = my_calculation_collection.co2 #=> <AMEE::DataAbstraction::TermsList>
|
206
|
+
# my_terms.label #=> :co2
|
207
|
+
#
|
208
|
+
# ---
|
209
|
+
#
|
210
|
+
# Call either the <tt>#sort_by</tt> or <tt>#sort_by!</tt> methods including
|
211
|
+
# the argument term as part of the method name, e.g.,
|
212
|
+
#
|
213
|
+
# my_calculation_collection.sort_by_co2
|
214
|
+
#
|
215
|
+
# #=> <AMEE::DataAbstraction::CalculationCollection ... >
|
216
|
+
#
|
217
|
+
# my_calculation_collection.sort_by_mass!
|
218
|
+
#
|
219
|
+
# #=> <AMEE::DataAbstraction::CalculationCollection ... >
|
220
|
+
#
|
221
|
+
def method_missing(method, *args, &block)
|
222
|
+
if terms.labels.include? method.to_sym
|
223
|
+
terms.send(method.to_sym)
|
224
|
+
elsif method.to_s =~ /sort_by_(.*)!/ and terms.labels.include? $1.to_sym
|
225
|
+
sort_by! $1.to_sym
|
226
|
+
elsif method.to_s =~ /sort_by_(.*)/ and terms.labels.include? $1.to_sym
|
227
|
+
sort_by $1.to_sym
|
228
|
+
else
|
229
|
+
super
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
end
|
234
|
+
|
235
|
+
end
|
236
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# Copyright (C) 2011 AMEE UK Ltd. - http://www.amee.com
|
2
|
+
# Released as Open Source Software under the BSD 3-Clause license. See LICENSE.txt for details.
|
3
|
+
#
|
4
|
+
# :title: Class AMEE::DataAbstraction::Result
|
5
|
+
|
6
|
+
module AMEE
|
7
|
+
module DataAbstraction
|
8
|
+
|
9
|
+
# Subclass of <tt>Term</tt> providing methods and attributes appropriate for
|
10
|
+
# representing the results of cross calculation operations specifically
|
11
|
+
#
|
12
|
+
class Result < Term
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
|
2
|
+
# Copyright (C) 2011 AMEE UK Ltd. - http://www.amee.com
|
3
|
+
# Released as Open Source Software under the BSD 3-Clause license. See LICENSE.txt for details.
|
4
|
+
#
|
5
|
+
# :title: Module: AMEE::DataAbstraction::TermAnalyticsSupport
|
6
|
+
|
7
|
+
module AMEE
|
8
|
+
module DataAbstraction
|
9
|
+
|
10
|
+
# Mixin module for the <i>AMEE::DataAbstraction::Term</i> class, providing
|
11
|
+
# methods for handling collections of calculations.
|
12
|
+
#
|
13
|
+
module TermAnalyticsSupport
|
14
|
+
|
15
|
+
# Returns an instance of <i>Result</i> based upon the attributes of
|
16
|
+
# <tt>self</tt>.
|
17
|
+
#
|
18
|
+
def to_result
|
19
|
+
result_term = Result.new
|
20
|
+
TermsList::TermProperties.each do |attr|
|
21
|
+
result_term.send(attr, self.send(attr))
|
22
|
+
end
|
23
|
+
return result_term
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|