class WeakParameters::BaseValidator

Attributes

block[R]
controller[R]
options[R]

Public Class Methods

new(controller, key, options = {}, &block) click to toggle source
# File lib/weak_parameters/base_validator.rb, line 5
def initialize(controller, key, options = {}, &block)
  @controller = controller
  @key = key
  @options = options
  @block = block
  @path = []
end

Public Instance Methods

key() click to toggle source
# File lib/weak_parameters/base_validator.rb, line 30
def key
  path[-1]
end
required?() click to toggle source
# File lib/weak_parameters/base_validator.rb, line 18
def required?
  !!options[:required]
end
strong?() click to toggle source
# File lib/weak_parameters/base_validator.rb, line 22
def strong?
  !!options[:strong]
end
strong_params(*path) click to toggle source
# File lib/weak_parameters/base_validator.rb, line 34
def strong_params(*path)
  @path = path
  exist? && strong? ? {key => value} : {}
end
type() click to toggle source
# File lib/weak_parameters/base_validator.rb, line 26
def type
  self.class.name.split("::").last.sub(/Validator$/, "").underscore.to_sym
end
validate(*path) click to toggle source
# File lib/weak_parameters/base_validator.rb, line 13
def validate(*path)
  @path = path
  handle_failure unless valid?
end

Private Instance Methods

error_message() click to toggle source
# File lib/weak_parameters/base_validator.rb, line 103
def error_message
  "params[#{key.inspect}] must be a valid value"
end
exceptional?() click to toggle source
# File lib/weak_parameters/base_validator.rb, line 64
def exceptional?
  case
  when options[:only].try(:exclude?, value)
    true
  when options[:except].try(:include?, value)
    true
  else
    false
  end
end
exist?() click to toggle source
# File lib/weak_parameters/base_validator.rb, line 60
def exist?
  !nil?
end
handle_failure() click to toggle source
# File lib/weak_parameters/base_validator.rb, line 91
def handle_failure
  if has_handler?
    controller.send(options[:handler])
  else
    raise_error
  end
end
has_handler?() click to toggle source
# File lib/weak_parameters/base_validator.rb, line 115
def has_handler?
  !!options[:handler]
end
invalid_type?() click to toggle source
# File lib/weak_parameters/base_validator.rb, line 111
def invalid_type?
  !valid_type?
end
nil?() click to toggle source
# File lib/weak_parameters/base_validator.rb, line 56
def nil?
  params.nil? || params[key].nil?
end
params() click to toggle source
# File lib/weak_parameters/base_validator.rb, line 81
def params
  path[0...-1].inject(controller.params) { |params, key|
    params[key]
  }
end
path() click to toggle source

key array to validation target

# File lib/weak_parameters/base_validator.rb, line 76
def path
  # Because @key becomes nil at ListValidator, I remove it from path.
  (@path + [ @key ]).compact
end
raise_error() click to toggle source
# File lib/weak_parameters/base_validator.rb, line 99
def raise_error
  raise WeakParameters::ValidationError, error_message
end
valid?() click to toggle source
# File lib/weak_parameters/base_validator.rb, line 41
def valid?
  case
  when required? && nil?
    false
  when exist? && invalid_type?
    false
  when exist? && exceptional?
    false
  when exist? && block && !controller.instance_exec(value, &block)
    false
  else
    true
  end
end
valid_type?() click to toggle source
# File lib/weak_parameters/base_validator.rb, line 107
def valid_type?
  true
end
value() click to toggle source
# File lib/weak_parameters/base_validator.rb, line 87
def value
  params[key]
end