Module: AMEE::Analytics::CalculationCollectionAnalyticsSupport

Defined in:
lib/amee/analytics/calculation_collection_analytics_support.rb

Overview

Mixin module for the AMEE::DataAbstraction::CalculationCollection class, providing methods for handling collections of calculations.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Syntactic sugar for several instance methods.


Call a method on self which named after a specific term label contained with an associated calculation and return an instance of the TermsList class contain each of those terms. E.g.,

my_terms = my_calculation_collection.type  #=> <AMEE::DataAbstraction::TermsList>
my_terms.label                             #=> :type

my_terms = my_calculation_collection.mass  #=> <AMEE::DataAbstraction::TermsList>
my_terms.label                             #=> :mass

my_terms = my_calculation_collection.co2   #=> <AMEE::DataAbstraction::TermsList>
my_terms.label                             #=> :co2

Call either the #sort_by or #sort_by! methods including the argument term as part of the method name, e.g.,

my_calculation_collection.sort_by_co2

                #=> <AMEE::DataAbstraction::CalculationCollection ... >

my_calculation_collection.sort_by_mass!

                #=> <AMEE::DataAbstraction::CalculationCollection ... >


221
222
223
224
225
226
227
228
229
230
231
# File 'lib/amee/analytics/calculation_collection_analytics_support.rb', line 221

def method_missing(method, *args, &block)
  if terms.labels.include? method.to_sym
    terms.send(method.to_sym)
  elsif method.to_s =~ /sort_by_(.*)!/ and terms.labels.include? $1.to_sym
    sort_by! $1.to_sym
  elsif method.to_s =~ /sort_by_(.*)/ and terms.labels.include? $1.to_sym
    sort_by $1.to_sym
  else
    super
  end
end

Instance Method Details

#+(other_calc_coll) ⇒ Object



37
38
39
# File 'lib/amee/analytics/calculation_collection_analytics_support.rb', line 37

def +(other_calc_coll)
  self.class.new(self.to_a + other_calc_coll.to_a)
end

#-(other_calc_coll) ⇒ Object



41
42
43
44
# File 'lib/amee/analytics/calculation_collection_analytics_support.rb', line 41

def -(other_calc_coll)
  other_calc_coll = [other_calc_coll].flatten
  self.delete_if { |calc| other_calc_coll.include?(calc) }
end

#calculate_all!Object

Call the #calculate! method on all calculations contained within self.



146
147
148
# File 'lib/amee/analytics/calculation_collection_analytics_support.rb', line 146

def calculate_all!
  each { |calc| calc.calculate! }
end

#calculation_labelsObject

Returns an array containing all of the unique labels for calculations held in self.



33
34
35
# File 'lib/amee/analytics/calculation_collection_analytics_support.rb', line 33

def calculation_labels
  map(&:label).uniq
end

#co2_or_co2e_outputsObject

Returns a new instance of the class TermsList representing either CO2 or CO2e outputs from each calculation



131
132
133
134
135
136
137
138
139
140
141
# File 'lib/amee/analytics/calculation_collection_analytics_support.rb', line 131

def co2_or_co2e_outputs
  terms = AMEE::DataAbstraction::TermsList.new
  each do |calculation|
    if calculation['co2e']
      terms << calculation['co2e']
    elsif calculation.outputs.visible.labels.size == 1 && calculation.outputs.visible.labels.first == :co2
      terms << calculation['co2']
    end
  end
  return terms
end

#heterogeneous?Boolean

Returns true if all calculations in self are NOT representatives of the same prototype calculation. Otherwise, returns false.

Returns:

  • (Boolean)


26
27
28
# File 'lib/amee/analytics/calculation_collection_analytics_support.rb', line 26

def heterogeneous?
  !homogeneous?
end

#homogeneous?Boolean

Returns true if all calculations in self are representatives of the same prototype calculation. Otherwise, returns false.

Returns:

  • (Boolean)


18
19
20
# File 'lib/amee/analytics/calculation_collection_analytics_support.rb', line 18

def homogeneous?
  calculation_labels.size == 1
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


179
180
181
182
183
184
185
186
187
188
189
# File 'lib/amee/analytics/calculation_collection_analytics_support.rb', line 179

def respond_to?(method)
  if terms.labels.include? method.to_sym
    return true
  elsif method.to_s =~ /sort_by_(.*)!/ and terms.labels.include? $1.to_sym
    return true
  elsif method.to_s =~ /sort_by_(.*)/ and terms.labels.include? $1.to_sym
    return true
  else
    super
  end
end

#save_all!Object

Call the #save method on all calculations contained within self.



153
154
155
# File 'lib/amee/analytics/calculation_collection_analytics_support.rb', line 153

def save_all!
  each { |calc| calc.save }
end

#sort_by(term) ⇒ Object

Similar to #sort_by! but returns a new instance of CalculationCollection arranged according to the values on the term labelled term. E.g.

my_calculation_collection.sort_by :co2

                #=> <AMEE::DataAbstraction::CalculationCollection ... >


54
55
56
57
# File 'lib/amee/analytics/calculation_collection_analytics_support.rb', line 54

def sort_by(term)
  term = term.to_sym unless term.is_a? Symbol
  AMEE::DataAbstraction::CalculationCollection.new(send(term).sort_by(:value).map(&:parent))
end

#sort_by!(term) ⇒ Object

Sorts the calculation collection in place according to the values on the term labelled term, returning self.

my_calculation_collection.sort_by! :mass

                #=> <AMEE::DataAbstraction::CalculationCollection ... >


66
67
68
# File 'lib/amee/analytics/calculation_collection_analytics_support.rb', line 66

def sort_by!(term)
  replace(sort_by(term))
end

#standardize_units(term, unit = nil, per_unit = nil) ⇒ Object

Returns a new instance of CalculationCollection containing the same calculations contained within self but with the units of the term term standardized on each.

If no further arguments are provided, the standardized units represent those which currently predominate amongst the relevent terms. Otherwise, the specific unit and/or per unit which are required for each instance of term can be explicitly specified as the second and third arguments respecitively. Units can be specified in any of the formats which are acceptable to the Quantify::Unit.for method (i.e. stringified unit names or symbols, symbolized labels, or Quantify::Unit::Base instances of the required unit). E.g.

my_calculation_collection.standardize_units(:mass)

                #=> <AMEE::DataAbstraction::CalculationCollection ... >

my_calculation_collection.standardize_units(:mass, :lb)

                #=> <AMEE::DataAbstraction::CalculationCollection ... >

my_calculation_collection.standardize_units(:mass, 'pound')

                #=> <AMEE::DataAbstraction::CalculationCollection ... >

my_calculation_collection.standardize_units(:mass, <Quantify::Unit::NonSI>)

                #=> <AMEE::DataAbstraction::CalculationCollection ... >

my_calculation_collection.standardize_units(:distance, :km, 'year')

                #=> <AMEE::DataAbstraction::CalculationCollection ... >


103
104
105
106
107
108
109
110
111
# File 'lib/amee/analytics/calculation_collection_analytics_support.rb', line 103

def standardize_units(term,unit=nil,per_unit=nil)
  term = term.to_sym unless term.is_a? Symbol
  send(term).standardize_units(unit,per_unit).map do |term|
    calc = term.parent
    calc.contents[term.label] = term
    calc
  end
  AMEE::DataAbstraction::CalculationCollection.new(self)
end

#standardize_units!(term, unit = nil, per_unit = nil) ⇒ Object

Similar to #standardize_units but standardizes units in place, returning self



116
117
118
119
# File 'lib/amee/analytics/calculation_collection_analytics_support.rb', line 116

def standardize_units!(term,unit=nil,per_unit=nil)
  new_calcs = standardize_units(term,unit,per_unit)
  replace(new_calcs)
end

#sum_all_outputsObject

Returns an array of instances of the Result class representing the sums of all outputs represented within the collection



124
125
126
# File 'lib/amee/analytics/calculation_collection_analytics_support.rb', line 124

def sum_all_outputs
  AMEE::DataAbstraction::TermsList.new(terms.outputs.visible.labels.uniq.map { |o| send(o).sum })
end

#termsObject

Returns a terms list of all terms held by calculations contained within self.



160
161
162
# File 'lib/amee/analytics/calculation_collection_analytics_support.rb', line 160

def terms
  AMEE::DataAbstraction::TermsList.new( (self.map { |calc| calc.terms.map { |term| term } }).flatten )
end

#typeObject

Return an instance of TermsList containing only terms labelled :type.

This method overrides the standard #type method (which is deprecated) and mimics the functionality provied by the first #method_missing method in dynamically retrieving a subset of terms according their labels.



175
176
177
# File 'lib/amee/analytics/calculation_collection_analytics_support.rb', line 175

def type
  terms.type
end