Class: StrongerParameters::RegexpConstraint

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Constraint

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

Constructor Details

#initialize(regexp) ⇒ RegexpConstraint

Returns a new instance of RegexpConstraint.



9
10
11
12
# File 'lib/stronger_parameters/constraints/regexp_constraint.rb', line 9

def initialize(regexp)
  @regexp = regexp
  @string = StringConstraint.new
end

Instance Attribute Details

#regexpObject (readonly)

Returns the value of attribute regexp.



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

def regexp
  @regexp
end

Instance Method Details

#==(other) ⇒ Object



25
26
27
# File 'lib/stronger_parameters/constraints/regexp_constraint.rb', line 25

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

#value(v) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/stronger_parameters/constraints/regexp_constraint.rb', line 14

def value(v)
  v = @string.value(v)
  return v if v.is_a?(InvalidValue)

  if v&.match?(regexp)
    v
  else
    InvalidValue.new(v, "must match #{regexp.source}")
  end
end