module Validate

Constants

Error

Public Instance Methods

call(subject, state=nil, scenario: nil, scenarios: nil) click to toggle source
# File lib/validate/validate.rb, line 6
def call(subject, state=nil, scenario: nil, scenarios: nil)
  if scenarios.nil?
    scenarios = scenario
  end
  scenario_names = Array(scenarios)

  validator_reflection = validator_reflection(subject)

  if scenario_names.empty?
    validator = validator_reflection.constant
    validate(validator_reflection.constant, subject, state)
  else
    validate_scenarios(validator_reflection, subject, state, scenario_names)
  end
end
validate(validator, subject, state) click to toggle source
# File lib/validate/validate.rb, line 69
def validate(validator, subject, state)
  method = validator.method(:call)

  result = nil
  case method.parameters.length
  when 1
    if !state.nil?
      raise Error, "State argument was supplied but the validator does not provide a state parameter (Validator: #{validator})"
    end

    result = validator.public_send :call, subject
  when 2
    result = validator.public_send :call, subject, state
  end

  unless result.is_a?(TrueClass) || result.is_a?(FalseClass)
    raise Error, "Result must be boolean. The result is a #{result.class}. (Validator: #{validator})"
  end

  result
end
validate_const?(subject_constant) click to toggle source
# File lib/validate/validate.rb, line 44
def validate_const?(subject_constant)
  Reflect.constant?(subject_constant, :Validate)
end
validate_scenarios(validator_reflection, subject, state, scenario_names) click to toggle source
# File lib/validate/validate.rb, line 52
def validate_scenarios(validator_reflection, subject, state, scenario_names)
  result = true
  scenario_names.each do |scenario_name|
    scenario_reflection = validator_reflection.get(scenario_name, strict: false)

    if scenario_reflection.nil?
      raise Error, "#{validator_reflection.constant.name} doesn't have a `#{scenario_name}' scenario accessor"
    end

    validator = scenario_reflection.constant

    result = result & validate(validator, subject, state)
  end

  result
end
validator_const?(subject_constant) click to toggle source
# File lib/validate/validate.rb, line 48
def validator_const?(subject_constant)
  Reflect.constant?(subject_constant, :Validator)
end
validator_name(subject_constant) click to toggle source
# File lib/validate/validate.rb, line 34
def validator_name(subject_constant)
  if validate_const?(subject_constant)
    return :Validate
  elsif validator_const?(subject_constant)
    return :Validator
  else
    return nil
  end
end
validator_reflection(subject) click to toggle source
# File lib/validate/validate.rb, line 22
def validator_reflection(subject)
  subject_constant = Reflect.constant(subject)

  validator_name = validator_name(subject_constant)

  if validator_name.nil?
    raise Error, "#{subject_constant.name} doesn't have a Validate or Validator namespace"
  end

  Reflect.(subject, validator_name, strict: true)
end