Class: Regularity
- Inherits:
-
Object
show all
- Defined in:
- lib/regularity.rb,
lib/regularity/version.rb
Defined Under Namespace
Classes: Error
Constant Summary
collapse
- PATTERNS =
{
'digit' => '[0-9]',
'lowercase' => '[a-z]',
'uppercase' => '[A-Z]',
'letter' => '[A-Za-z]',
'alphanumeric' => '[A-Za-z0-9]',
'whitespace' => '\s',
'space' => ' ',
'tab' => '\t'
}
- ESCAPED_CHARS =
%w(
* . ? ^ + $ | ( ) [ ] { }
)
- VERSION =
'0.2.0'
Instance Method Summary
collapse
Constructor Details
Returns a new instance of Regularity.
20
21
22
23
|
# File 'lib/regularity.rb', line 20
def initialize
@str = ''
@ended = false
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(meth, *args, &block) ⇒ Object
86
87
88
|
# File 'lib/regularity.rb', line 86
def method_missing(meth, *args, &block)
regex.send(meth, *args, &block)
end
|
Instance Method Details
#=~(other) ⇒ Object
82
83
84
|
# File 'lib/regularity.rb', line 82
def =~(other)
regex =~ other
end
|
#append(*args) ⇒ Object
Also known as:
then
30
31
32
|
# File 'lib/regularity.rb', line 30
def append(*args)
write interpret(*args)
end
|
#at_least(times, pattern) ⇒ Object
61
62
63
|
# File 'lib/regularity.rb', line 61
def at_least(times, pattern)
between [times, nil], pattern
end
|
#at_most(times, pattern) ⇒ Object
65
66
67
|
# File 'lib/regularity.rb', line 65
def at_most(times, pattern)
between [nil, times], pattern
end
|
#between(range, pattern) ⇒ Object
53
54
55
56
57
58
59
|
# File 'lib/regularity.rb', line 53
def between(range, pattern)
unless range.length == 2 && range.any? { |i| i.is_a?(Integer) }
raise Regularity:Error.new('must provide an array of 2 elements, one of them must be an integer')
end
write '%s{%s,%s}' % [interpret(pattern), range[0], range[1]]
end
|
#end_with(*args) ⇒ Object
35
36
37
38
39
|
# File 'lib/regularity.rb', line 35
def end_with(*args)
write '%s$', args
@ended = true
self
end
|
#inspect ⇒ Object
98
99
100
|
# File 'lib/regularity.rb', line 98
def inspect
to_s
end
|
#maybe(*args) ⇒ Object
41
42
43
|
# File 'lib/regularity.rb', line 41
def maybe(*args)
write '%s?', args
end
|
#not(*args) ⇒ Object
45
46
47
|
# File 'lib/regularity.rb', line 45
def not(*args)
write '(?!%s)', args
end
|
#one_of(ary) ⇒ Object
49
50
51
|
# File 'lib/regularity.rb', line 49
def one_of(ary)
write '[%s]' % ary.map { |c| escape(c) }.join('|')
end
|
#one_or_more(pattern) ⇒ Object
73
74
75
|
# File 'lib/regularity.rb', line 73
def one_or_more(pattern)
write '%s+', pattern
end
|
#regex ⇒ Object
Also known as:
get
77
78
79
|
# File 'lib/regularity.rb', line 77
def regex
Regexp.new(@str)
end
|
#respond_to?(meth) ⇒ Boolean
90
91
92
|
# File 'lib/regularity.rb', line 90
def respond_to?(meth)
regex.respond_to?(meth) || super
end
|
#start_with(*args) ⇒ Object
25
26
27
28
|
# File 'lib/regularity.rb', line 25
def start_with(*args)
raise Regularity::Error.new('#start_with? called multiple times') unless @str.empty?
write '^%s', args
end
|
#to_s ⇒ Object
94
95
96
|
# File 'lib/regularity.rb', line 94
def to_s
"#<Regularity:#{object_id} regex=/#{@str}/>"
end
|
#zero_or_more(pattern) ⇒ Object
69
70
71
|
# File 'lib/regularity.rb', line 69
def zero_or_more(pattern)
write '%s*', pattern
end
|