Module: Validate
- Extended by:
- Validate
- Included in:
- Validate
- Defined in:
- lib/validate/validate.rb,
lib/validate/controls/no_validator.rb,
lib/validate/controls/validator/state.rb,
lib/validate/controls/validate/default.rb,
lib/validate/controls/validator/default.rb,
lib/validate/controls/validate/scenarios.rb,
lib/validate/controls/validator/scenario.rb,
lib/validate/controls/validator/scenarios.rb,
lib/validate/controls/no_scenario_accessor.rb,
lib/validate/controls/validator/not_boolean_result.rb
Defined Under Namespace
Modules: Controls
Constant Summary collapse
- Error =
Class.new(RuntimeError)
Instance Method Summary collapse
- #call(subject, state = nil, scenario: nil, scenarios: nil) ⇒ Object
- #validate(validator, subject, state) ⇒ Object
- #validate_const?(subject_constant) ⇒ Boolean
- #validate_scenarios(validator_reflection, subject, state, scenario_names) ⇒ Object
- #validator_const?(subject_constant) ⇒ Boolean
- #validator_name(subject_constant) ⇒ Object
- #validator_reflection(subject) ⇒ Object
Instance Method Details
#call(subject, state = nil, scenario: nil, scenarios: nil) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# 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) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# 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) ⇒ Boolean
44 45 46 |
# 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) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# 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) ⇒ Boolean
48 49 50 |
# File 'lib/validate/validate.rb', line 48 def validator_const?(subject_constant) Reflect.constant?(subject_constant, :Validator) end |
#validator_name(subject_constant) ⇒ Object
34 35 36 37 38 39 40 41 42 |
# 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) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# 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 |