Class: ParamsReady::Value::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/params_ready/value/validator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(constraint, strategy: :raise) ⇒ Validator

Returns a new instance of Validator.



36
37
38
39
40
# File 'lib/params_ready/value/validator.rb', line 36

def initialize(constraint, strategy: :raise)
  @constraint = constraint
  @strategy = check_strategy(constraint, strategy)
  freeze
end

Instance Attribute Details

#constraintObject (readonly)

Returns the value of attribute constraint.



6
7
8
# File 'lib/params_ready/value/validator.rb', line 6

def constraint
  @constraint
end

#strategyObject (readonly)

Returns the value of attribute strategy.



6
7
8
# File 'lib/params_ready/value/validator.rb', line 6

def strategy
  @strategy
end

Class Method Details

.instance(name_or_constraint, *args, strategy: :raise, **opts, &block) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/params_ready/value/validator.rb', line 8

def self.instance(name_or_constraint, *args, strategy: :raise, **opts, &block)
  constraint = case name_or_constraint
  when Value::Constraint
    name_or_constraint
  when Symbol
    type = Value::Constraint.constraint_type(name_or_constraint)
    type.build(*args, **opts, &block)
  else
    valid, missing_method = valid_constraint?(name_or_constraint, strategy)
    on_constraint_invalid(missing_method) unless valid
    name_or_constraint
  end
  new(constraint, strategy: strategy)
end

.on_constraint_invalid(missing_method) ⇒ Object

Raises:



23
24
25
# File 'lib/params_ready/value/validator.rb', line 23

def self.on_constraint_invalid(missing_method)
  raise ParamsReadyError, "Not a valid constraint, '#{missing_method}' unimplemented"
end

.valid_constraint?(constraint, strategy) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
30
31
32
33
34
# File 'lib/params_ready/value/validator.rb', line 27

def self.valid_constraint?(constraint, strategy)
  return [false, 'valid?'] unless constraint.respond_to?(:valid?)
  return [false, 'error_message'] unless constraint.respond_to?(:error_message)
  return [true, nil] unless strategy == :clamp
  return [false, 'clamp'] unless constraint.respond_to? :clamp

  [true, nil]
end

Instance Method Details

#check_strategy(constraint, strategy) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/params_ready/value/validator.rb', line 42

def check_strategy(constraint, strategy)
  case strategy.to_sym
  when :raise, :undefine
    strategy.to_sym
  when :clamp
    if constraint.respond_to? :clamp?
      raise ParamsReadyError, 'Clamping not applicable' unless constraint.clamp?
    end
    strategy.to_sym
  else
    raise ParamsReadyError, "Unexpected constraint strategy #{strategy}"
  end
end

#validate(value, result) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/params_ready/value/validator.rb', line 56

def validate(value, result)
  return [value, result] if Extensions::Undefined.value_indefinite?(value)

  if constraint.valid? value
    [value, result]
  else
    case strategy
    when :raise
      e = Constraint::Error.new("value '#{value}' #{constraint.error_message}")
      if result.nil?
        raise e
      else
        result.error! e
      end
      [Extensions::Undefined, result]
    when :clamp
      [constraint.clamp(value), result]
    else
      [Extensions::Undefined, result]
    end
  end
end