Class: StrongerParameters::EnumerationConstraint

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Constraint

#&, #required, #required?, #|

Constructor Details

#initialize(*allowed) ⇒ EnumerationConstraint

Returns a new instance of EnumerationConstraint.



8
9
10
# File 'lib/stronger_parameters/constraints/enumeration_constraint.rb', line 8

def initialize(*allowed)
  @allowed = allowed
end

Instance Attribute Details

#allowedObject (readonly)

Returns the value of attribute allowed.



6
7
8
# File 'lib/stronger_parameters/constraints/enumeration_constraint.rb', line 6

def allowed
  @allowed
end

Instance Method Details

#==(other) ⇒ Object



18
19
20
# File 'lib/stronger_parameters/constraints/enumeration_constraint.rb', line 18

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

#value(v) ⇒ Object



12
13
14
15
16
# File 'lib/stronger_parameters/constraints/enumeration_constraint.rb', line 12

def value(v)
  return v if allowed.include?(v)

  InvalidValue.new(v, "must be one of these: #{allowed.to_sentence}")
end