Class: PIC::Template

Inherits:
Object show all
Defined in:
lib/standard/facets/pic.rb

Overview

Template class encapsulates a picture string, converts it to a regular expression, and delegates to it for matching.

Examples:

t = PIC::Template.new('9[3]-9[3]-9[4]')
t.to_re  #=> /\d{3}\-\d{3}\-\d{4}/
t =~ '555-123-4567'  #=> 0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pic) ⇒ Template

New pic template.

Parameters:

  • pic (String)

    Picture string to use for matching.



81
82
83
# File 'lib/standard/facets/pic.rb', line 81

def initialize(pic)
  @pic = pic
end

Instance Attribute Details

#picString (readonly)

The picture string.

Returns:



89
90
91
# File 'lib/standard/facets/pic.rb', line 89

def pic
  @pic
end

Instance Method Details

#!~(string) ⇒ Boolean

Check for non-match of the picture against a given string.

Parameters:

  • string (String)

    String to match picture against.

Returns:

  • (Boolean)


129
130
131
# File 'lib/standard/facets/pic.rb', line 129

def !~(string)
  to_re !~ string.to_str
end

#=~(string) ⇒ Integer? Also known as: ===

Match picture against a given string.

Parameters:

  • string (String)

    String to match picture against.

Returns:

  • (Integer, nil)

    index position of match, or nil.



116
117
118
# File 'lib/standard/facets/pic.rb', line 116

def =~(string)
  to_re =~ string.to_str
end

#to_reRegexp Also known as: to_regexp

Convert picture to a regular expression.

Examples:

PIC::Template.new('99/99/9999').to_re  #=> /\d\d\/\d\d\/\d\d\d\d/

Returns:



98
99
100
101
102
103
104
# File 'lib/standard/facets/pic.rb', line 98

def to_re
  re = ''
  pic.scan(SCAN) do |s,c|
    re << remap(s) + recnt(c)
  end
  Regexp.new(re)
end