Class: Predicates::Pattern

Inherits:
Base
  • Object
show all
Defined in:
lib/predicates/pattern.rb

Overview

Provides a generic pattern predicate, which is extended and used by other pattern-based validations.

WARNING: If you define a pattern, you probably want to use A and Z instead of ^ and $. The latter anchors are per-line, which means that multi-line strings can sneak stuff past your regular expression. For example:

@predicate.like = /^hello world$/
assert @predicate.validate("malicious\nhello world\ntext", nil), 'must match only one line'
@predicate.like = /\Ahello world\Z/
assert !@predicate.validate("malicious\nhello world\ntext", nil), 'must match entire string'

Options

  • :like - a regular expression matching pattern

Example

field_has_a_pattern :like => /\Aim in ur [a-z]+, [a-z]+ ur [a-z]+\Z/

Direct Known Subclasses

HexColor, UsaZipCode

Instance Attribute Summary collapse

Attributes inherited from Base

#error_message, #full_message, #or_empty, #validate_if, #validate_on

Instance Method Summary collapse

Methods inherited from Base

#allow_empty?, #error, #error_binds, #initialize, #normalize, #to_human

Constructor Details

This class inherits a constructor from Predicates::Base

Instance Attribute Details

#likeObject

Returns the value of attribute like.



16
17
18
# File 'lib/predicates/pattern.rb', line 16

def like
  @like
end

Instance Method Details

#validate(value, record) ⇒ Object



18
19
20
21
# File 'lib/predicates/pattern.rb', line 18

def validate(value, record)
  value = value.to_s if [Symbol, Fixnum].include?(value.class)
  value.match(self.like)
end