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

#&, #|

Constructor Details

#initialize(regexp) ⇒ RegexpConstraint

Returns a new instance of RegexpConstraint.



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

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

Instance Attribute Details

#regexpObject (readonly)

Returns the value of attribute regexp.



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

def regexp
  @regexp
end

Instance Method Details

#==(other) ⇒ Object



22
23
24
# File 'lib/stronger_parameters/constraints/regexp_constraint.rb', line 22

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

#value(v) ⇒ Object



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

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