module AMEE::Analytics::CalculationCollectionAnalyticsSupport

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

Public Instance Methods

+(other_calc_coll) click to toggle source
# 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) click to toggle source
# 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!() click to toggle source

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

# File lib/amee/analytics/calculation_collection_analytics_support.rb, line 146
def calculate_all!
  each { |calc| calc.calculate! }
end
calculation_labels() click to toggle source

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

# File lib/amee/analytics/calculation_collection_analytics_support.rb, line 33
def calculation_labels
  map(&:label).uniq
end
co2_or_co2e_outputs() click to toggle source

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

# 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?() click to toggle source

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

# File lib/amee/analytics/calculation_collection_analytics_support.rb, line 26
def heterogeneous?
  !homogeneous?
end
homogeneous?() click to toggle source

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

# File lib/amee/analytics/calculation_collection_analytics_support.rb, line 18
def homogeneous?
  calculation_labels.size == 1
end
method_missing(method, *args, &block) click to toggle source

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 ... >
Calls superclass method
# 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
respond_to?(method) click to toggle source
Calls superclass method
# 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!() click to toggle source

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

# File lib/amee/analytics/calculation_collection_analytics_support.rb, line 153
def save_all!
  each { |calc| calc.save }
end
sort_by(term) click to toggle source

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 ... >
# 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) click to toggle source

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 ... >
# 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) click to toggle source

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 ... >
# 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) click to toggle source

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

# 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_outputs() click to toggle source

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

# 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
terms() click to toggle source

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

# 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
type() click to toggle source

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.

# File lib/amee/analytics/calculation_collection_analytics_support.rb, line 175
def type
  terms.type
end