Class: Vanguard::Matcher::Nullary::Format

Inherits:
Vanguard::Matcher::Nullary show all
Includes:
AbstractType
Defined in:
lib/vanguard/matcher/nullary/format.rb

Overview

Abstract base class for format matchers

Direct Known Subclasses

Regexp

Defined Under Namespace

Classes: Regexp

Constant Summary collapse

EMAIL_ADDRESS_REGEXP =

Almost RFC2822 (No attribution reference available).

This differs in that it does not allow local domains (test@localhost). 99% of the time you do not want to allow these email addresses in a public web application.

begin
 #if (RUBY_VERSION == '1.9.2' && RUBY_ENGINE == 'jruby' && JRUBY_VERSION <= '1.6.3') || RUBY_VERSION == '1.9.3'
    # There is an obscure bug in jruby 1.6 that prevents matching
    # on unicode properties here. Remove this logic branch once
    # a stable jruby release fixes this.
    #
    # http://jira.codehaus.org/browse/JRUBY-5622
    #
    # There is a similar bug in preview releases of 1.9.3
    #
    # http://redmine.ruby-lang.org/issues/5126
    letter = 'a-zA-Z'
 #else
 #  letter = 'a-zA-Z\p{L}'  # Changed from RFC2822 to include unicode chars
 #end
  digit          = '0-9'
  atext          = "[#{letter}#{digit}\!\#\$\%\&\'\*+\/\=\?\^\_\`\{\|\}\~\-]"
  dot_atom_text  = "#{atext}+([.]#{atext}*)+"
  dot_atom       = dot_atom_text
  no_ws_ctl      = '\x01-\x08\x11\x12\x14-\x1f\x7f'
  qtext          = "[^#{no_ws_ctl}\\x0d\\x22\\x5c]"  # Non-whitespace, non-control character except for \ and "
  text           = '[\x01-\x09\x11\x12\x14-\x7f]'
  quoted_pair    = "(\\x5c#{text})"
  qcontent       = "(?:#{qtext}|#{quoted_pair})"
  quoted_string  = "[\"]#{qcontent}+[\"]"
  atom           = "#{atext}+"
  word           = "(?:#{atom}|#{quoted_string})"
  obs_local_part = "#{word}([.]#{word})*"
  local_part     = "(?:#{dot_atom}|#{quoted_string}|#{obs_local_part})"
  dtext          = "[#{no_ws_ctl}\\x21-\\x5a\\x5e-\\x7e]"
  dcontent       = "(?:#{dtext}|#{quoted_pair})"
  domain_literal = "\\[#{dcontent}+\\]"
  obs_domain     = "#{atom}([.]#{atom})+"
  domain         = "(?:#{dot_atom}|#{domain_literal}|#{obs_domain})"
  addr_spec      = "#{local_part}\@#{domain}"
  pattern        = /\A#{addr_spec}\z/u
end.freeze
URL_REGEXP =
/(^$)|(^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}((\:[0-9]{1,5})?\/?.*)?$)/ix.freeze
EMAIL_ADDRESS =
Regexp.new(EMAIL_ADDRESS_REGEXP)
URL =
Regexp.new(URL_REGEXP)

Class Method Summary collapse

Methods inherited from Vanguard::Matcher

#matches?

Class Method Details

.build(value) ⇒ Matcher::Format

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Build format matcher



109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/vanguard/matcher/nullary/format.rb', line 109

def self.build(value)
  case value
  when :email_address
    EMAIL_ADDRESS
  when :url
    URL
  when ::Regexp
    Regexp.new(value)
  when ::Proc
    Proc.new(value)
  else
    raise "Cannot build format matcher from #{value.inspect}"
  end
end