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

#&, #|

Constructor Details

#initialize(*allowed) ⇒ EnumerationConstraint

Returns a new instance of EnumerationConstraint.



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

def initialize(*allowed)
  @allowed = allowed
end

Instance Attribute Details

#allowedObject (readonly)

Returns the value of attribute allowed.



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

def allowed
  @allowed
end

Instance Method Details

#==(other) ⇒ Object



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

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

#value(v) ⇒ Object



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

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

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