module AFCSalesforce::Tools::Validation::Rule

Public Instance Methods

errors() click to toggle source

A hash of errors for this object

# File lib/afc_salesforce/tools/validation/validator.rb, line 11
def errors
  @errors ||= {}
end
rule(field, definition) click to toggle source

Define a rule for this object

The rule parameter can be one of the following:

  • a symbol that matches to a class in the AFCSalesforce::Tools::Validation::Rules namespace

    • e.g. rule(:field, :not_empty)

  • a hash containing the rule as the key and it's parameters as the values

    • e.g. rule(:field, :length => { minimum: 3, maximum: 5 })

  • an array combining the two previous types

    • e.g. rule(:field, [:not_empty, length: { minimum: 3, maximum: 5}])

# File lib/afc_salesforce/tools/validation/validator.rb, line 25
def rule(field, definition)
  field = field.to_sym
  rules[field] = [] if rules[field].nil?

  if definition.respond_to?(:each_pair)
    add_parameterized_rules(field, definition)
  elsif definition.respond_to?(:each)
    definition.each do |item|
      if item.respond_to?(:each_pair)
        add_parameterized_rules(field, item)
      else
        add_single_rule(field, item)
      end
    end
  else
    add_single_rule(field, definition)
  end

  self
end
rules() click to toggle source

A hash of rules for this object

# File lib/afc_salesforce/tools/validation/validator.rb, line 6
def rules
  @rules ||= {}
end
valid?() click to toggle source

Determines if this object is valid. When a rule fails for a field, this will stop processing further rules. In this way, you'll only get one error per field

# File lib/afc_salesforce/tools/validation/validator.rb, line 49
def valid?
  valid = true

  rules.each_pair do |field, rules|
    if ! @obj.respond_to?(field)
      raise InvalidKey, "cannot validate non-existent field '#{field}'"
    end

    rules.each do |r|
      value = @obj.send(field)
      if !r.valid_value?(value)
        valid = false
        errors[field] = {rule: r.error_key}.merge(r.error(value))
        break
      end
    end
  end

  @valid = valid
end

Protected Instance Methods

add_parameterized_rules(field, rules) click to toggle source

Adds a set of parameterized rules to this object

# File lib/afc_salesforce/tools/validation/validator.rb, line 87
def add_parameterized_rules(field, rules)
  rules.each_pair do |key, params|
    add_single_rule(field, key, params)
  end
end
add_single_rule(field, key_or_klass, params = nil) click to toggle source

Adds a single rule to this object

# File lib/afc_salesforce/tools/validation/validator.rb, line 73
def add_single_rule(field, key_or_klass, params = nil)
  klass = if key_or_klass.respond_to?(:new)
    key_or_klass
  else
    get_rule_class_by_name(key_or_klass)
  end

  args = [params].compact
  rule = klass.new(*args)
  rule.obj = @obj if rule.respond_to?(:obj=)
  rules[field] << rule
end
camelize(term) click to toggle source

Converts a symbol to a class name, taken from rails

# File lib/afc_salesforce/tools/validation/validator.rb, line 102
def camelize(term)
  string = term.to_s
  string = string.sub(/^[a-z\d]*/) { $&.capitalize }
  string.gsub(/(?:_|(\/))([a-z\d]*)/i) { $2.capitalize }.gsub('/', '::')
end
get_rule_class_by_name(klass) click to toggle source

Resolves the specified rule name to a rule class

# File lib/afc_salesforce/tools/validation/validator.rb, line 94
def get_rule_class_by_name(klass)
  klass = camelize(klass)
  AFCSalesforce::Tools::Validation::Rule.const_get(klass)
rescue NameError => e
  raise InvalidRule.new(e)
end