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.



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

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

Instance Attribute Details

#regexpObject (readonly)

Returns the value of attribute regexp.



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

def regexp
  @regexp
end

Instance Method Details

#==(other) ⇒ Object



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

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

#value(v) ⇒ Object



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

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

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