Module: Surveyor::Models::ValidationMethods
- Included in:
- Validation
- Defined in:
- lib/surveyor/models/validation_methods.rb
Class Method Summary (collapse)
Instance Method Summary (collapse)
-
- (Object) conditions_hash(response_set)
A hash of the conditions (keyed by rule_key) and their evaluation (boolean) in the context of response_set.
-
- (Boolean) is_valid?(response_set)
Instance Methods.
Class Method Details
+ (Object) included(base)
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/surveyor/models/validation_methods.rb', line 4 def self.included(base) # Associations base.send :belongs_to, :answer base.send :has_many, :validation_conditions, :dependent => :destroy # Scopes @@validations_already_included ||= nil unless @@validations_already_included # Validations base.send :validates_presence_of, :rule base.send :validates_format_of, :rule, :with => /^(?:and|or|\)|\(|[A-Z]|\s)+$/ # this causes issues with building and saving # base.send :validates_numericality_of, :answer_id @@validations_already_included = true end end |
Instance Method Details
- (Object) conditions_hash(response_set)
A hash of the conditions (keyed by rule_key) and their evaluation (boolean) in the context of response_set
36 37 38 39 40 41 42 |
# File 'lib/surveyor/models/validation_methods.rb', line 36 def conditions_hash(response_set) hash = {} response = response_set.responses.detect{|r| r.answer_id.to_i == self.answer_id.to_i} # logger.debug "r: #{response.inspect}" self.validation_conditions.each{|vc| hash.merge!(vc.to_hash(response))} return hash end |
- (Boolean) is_valid?(response_set)
Instance Methods
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/surveyor/models/validation_methods.rb', line 24 def is_valid?(response_set) ch = conditions_hash(response_set) rgx = Regexp.new(self.validation_conditions.map{|vc| ["a","o"].include?(vc.rule_key) ? "#{vc.rule_key}(?!nd|r)" : vc.rule_key}.join("|")) # exclude and, or # logger.debug "v: #{self.inspect}" # 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]}}" eval(self.rule.gsub(rgx){|m| ch[m.to_sym]}) end |