Class: StrongerParameters::OrConstraint

Inherits:
Constraint
  • Object
show all
Defined in:
lib/stronger_parameters/constraint.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Constraint

#&, #required

Constructor Details

#initialize(*constraints) ⇒ OrConstraint

Returns a new instance of OrConstraint.



35
36
37
# File 'lib/stronger_parameters/constraint.rb', line 35

def initialize(*constraints)
  @constraints = constraints
end

Instance Attribute Details

#constraintsObject (readonly)

Returns the value of attribute constraints.



33
34
35
# File 'lib/stronger_parameters/constraint.rb', line 33

def constraints
  @constraints
end

Instance Method Details

#==(other) ⇒ Object



59
60
61
# File 'lib/stronger_parameters/constraint.rb', line 59

def ==(other)
  super && constraints == other.constraints
end

#required?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/stronger_parameters/constraint.rb', line 63

def required?
  constraints.all?(&:required?)
end

#value(v) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/stronger_parameters/constraint.rb', line 39

def value(v)
  exception = nil

  constraints.each do |c|
    result = c.value(v)
    if result.is_a?(InvalidValue)
      exception ||= result
    else
      return result
    end
  end

  exception
end

#|(other) ⇒ Object



54
55
56
57
# File 'lib/stronger_parameters/constraint.rb', line 54

def |(other)
  constraints << other
  self
end