Class: Dependency

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

Instance Method Summary collapse

Instance Method Details

#conditions_hash(response_set) ⇒ Object

A hash of the conditions (keyed by rule_key) and their evaluation (boolean) in the context of response_set



44
45
46
47
48
# File 'app/models/dependency.rb', line 44

def conditions_hash(response_set)
  hash = {}
  self.dependency_conditions.each{|dc| hash.merge!(dc.to_hash(response_set))}
  return hash
end

#is_met?(response_set) ⇒ Boolean

Has this dependency has been met in the context of response_set? Substitutes the conditions hash into the rule and evaluates it

Returns:

  • (Boolean)


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

def is_met?(response_set)
  ch = conditions_hash(response_set)
  return false if ch.blank?
  # logger.debug "rule: #{self.rule.inspect}"
  # logger.debug "rexp: #{rgx.inspect}"
  # logger.debug "keyp: #{ch.inspect}"
  # logger.debug "subd: #{self.rule.gsub(rgx){|m| ch[m.to_sym]}}"
  rgx = Regexp.new(self.dependency_conditions.map{|dc| ["a","o"].include?(dc.rule_key) ? "#{dc.rule_key}(?!nd|r)" : dc.rule_key}.join("|")) # exclude and, or
  eval(self.rule.gsub(rgx){|m| ch[m.to_sym]})
end

#question_group_id=(i) ⇒ Object



20
21
22
23
# File 'app/models/dependency.rb', line 20

def question_group_id=(i)
  write_attribute(:question_id, nil) unless i.nil?
  write_attribute(:question_group_id, i)
end

#question_id=(i) ⇒ Object



25
26
27
28
# File 'app/models/dependency.rb', line 25

def question_id=(i)
  write_attribute(:question_group_id, nil) unless i.nil?
  write_attribute(:question_id, i) 
end