Class: Dependency

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/dependency.rb

Instance Method Summary collapse

Instance Method Details

#keyed_conditions(response_set) ⇒ Object

Pairs up the substitution key with the evaluated condition result for substitution into the rule Example: If you have two dependency conditions with rule keys “A” and “B” in the rule “A or B” calling keyed_condition_pairs will return => true, :B => false



27
28
29
30
31
32
33
# File 'app/models/dependency.rb', line 27

def keyed_conditions(response_set)
  keyed_pairs = {}
  self.dependency_conditions.each do |dc|
    keyed_pairs.merge!(dc.to_evaluation_hash(response_set))
  end
  return(keyed_pairs)
end

#met?(response_set) ⇒ Boolean

Is the method that determines if this dependency has been met within the provided response set

Returns:

  • (Boolean)


16
17
18
19
20
21
22
# File 'app/models/dependency.rb', line 16

def met?(response_set)
  if keyed_pairs = keyed_conditions(response_set)
    return(rule_evaluation(keyed_pairs))
  else
    return(false)
  end
end

#rule_evaluation(keyed_pairs) ⇒ Object

Does the substiution and evaluation of the dependency rule with the keyed pairs



36
37
38
39
40
41
# File 'app/models/dependency.rb', line 36

def rule_evaluation(keyed_pairs)
  # subtitute into rule for evaluation
  rgx = Regexp.new(self.dependency_conditions.map{|dc| dc.rule_key}.join("|")) # Making a regexp to only look for the keys used in the child conditions
  #logger.debug("rexp: #{rgx.inspect} FOO: #{keyed_pairs.inspect} --- subbed rules: #{rule.gsub(rgx){|m| keyed_pairs[m.to_sym]}} --> #{eval(self.rule.gsub(rgx){|m| keyed_pairs[m.to_sym]})}")
  eval(self.rule.gsub(rgx){|m| keyed_pairs[m.to_sym]}) # returns the evaluation of the rule and the conditions
end