module PIM::Calculations

Public Instance Methods

add_formula(formula) click to toggle source
# File pim.rb, line 9351
def add_formula formula
  raise "No formula was provided." if !formula
  raise "Calculation formula for attribute '#{formula.calculated_attribute.name}' is already defined" if module_formula_by_attribute.has_key?(formula.calculated_attribute)
  module_formula_by_attribute[formula.calculated_attribute] = formula
  formula.dependent_attributes.each do |attribute|
    check_attribute!(attribute, "Attribute '#{attribute}' used in calculation formula for '#{formula.calculated_attribute}' is not defined") unless data_module.has_option?(:ignore_undefined_attributes)
    module_formulas_by_dependent_attribute[attribute] << formula.calculated_attribute
  end
end
all_formulas_by_attributes(*attributes) click to toggle source
# File pim.rb, line 9361
def all_formulas_by_attributes *attributes
  formulas = {}
  formulas.merge!(module_formulas_by_attributes(*attributes))
  add_parent_objects(formulas) { |p| p.module_formulas_by_attributes(*attributes)}
  formulas
end
all_formulas_by_dependent_attribute(attribute) click to toggle source
# File pim.rb, line 9368
def all_formulas_by_dependent_attribute attribute
  formula_attributes = []
  formula_attributes.concat(@module_formulas_by_dependent_attribute[attribute]) if @module_formulas_by_dependent_attribute
  add_parent_objects(formula_attributes) { |p| p.module_formulas_by_dependent_attribute[attribute] }
  formula_attributes.empty? ? {} : all_formulas_by_attributes(*formula_attributes)
end
formula_by_attribute(attribute) click to toggle source
# File pim.rb, line 9375
def formula_by_attribute attribute
  formula = module_formula_by_attribute[attribute] if @module_formula_by_attribute
  formula = get_parent_object { |p| p.formula_by_attribute(attribute) } if formula.nil?
  formula
end

Protected Instance Methods

calculate_attribute(result, attribute_name, new_item, path = [attribute_name]) click to toggle source
# File pim.rb, line 9399
def calculate_attribute result, attribute_name, new_item, path = [attribute_name]
  formula = module_formula_by_attribute[attribute_name]
  if formula
    context = PIM::CalculationContext.new(formula, formula.calculated_attribute, formula.dependent_attributes, new_item)
    begin
      calculation_result = formula.execute(context)
      result.calculated_values_by_attribute_name[attribute_name] = calculation_result
      return calculation_result
    rescue Exception => e
      error_rule = PIM::ValidationRule.new(:execution_error)
      error_params = { :err_formula => attribute_name }
      error_msg = "Error executing calculation for attribute '#{attribute_name}': #{e.message}"
      result.fail error_rule, PIM::ValidationLevel::ERROR, path, error_msg, error_params
      log_warn(error_msg, e)
      return nil
    end
  end
end
module_formula_by_attribute() click to toggle source
# File pim.rb, line 9387
def module_formula_by_attribute
  @module_formula_by_attribute ||= {}
end
module_formulas_by_attributes(*attributes) click to toggle source
# File pim.rb, line 9391
def module_formulas_by_attributes *attributes
  formulas = {}
  if @module_formula_by_attribute
    formulas = @module_formula_by_attribute.select { |k,v| attributes.empty? or attributes.include?(k) }
  end
  formulas
end
module_formulas_by_dependent_attribute() click to toggle source
# File pim.rb, line 9383
def module_formulas_by_dependent_attribute
  @module_formulas_by_dependent_attribute ||= Hash.new { |k, v| k[v] = [] }
end