class Regularity
Constants
- ESCAPED_CHARS
- PATTERNS
- VERSION
Public Class Methods
new()
click to toggle source
# File lib/regularity.rb, line 20 def initialize @str = '' @ended = false end
Public Instance Methods
=~(other)
click to toggle source
# File lib/regularity.rb, line 82 def =~(other) regex =~ other end
append(*args)
click to toggle source
# File lib/regularity.rb, line 30 def append(*args) write interpret(*args) end
Also aliased as: then
at_least(times, pattern)
click to toggle source
# File lib/regularity.rb, line 61 def at_least(times, pattern) between [times, nil], pattern end
at_most(times, pattern)
click to toggle source
# File lib/regularity.rb, line 65 def at_most(times, pattern) between [nil, times], pattern end
between(range, pattern)
click to toggle source
# 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)
click to toggle source
# File lib/regularity.rb, line 35 def end_with(*args) write '%s$', args @ended = true self end
inspect()
click to toggle source
# File lib/regularity.rb, line 98 def inspect to_s end
maybe(*args)
click to toggle source
# File lib/regularity.rb, line 41 def maybe(*args) write '%s?', args end
method_missing(meth, *args, &block)
click to toggle source
# File lib/regularity.rb, line 86 def method_missing(meth, *args, &block) regex.send(meth, *args, &block) end
not(*args)
click to toggle source
# File lib/regularity.rb, line 45 def not(*args) write '(?!%s)', args end
one_of(ary)
click to toggle source
# File lib/regularity.rb, line 49 def one_of(ary) write '[%s]' % ary.map { |c| escape(c) }.join('|') end
one_or_more(pattern)
click to toggle source
# File lib/regularity.rb, line 73 def one_or_more(pattern) write '%s+', pattern end
regex()
click to toggle source
# File lib/regularity.rb, line 77 def regex Regexp.new(@str) end
Also aliased as: get
respond_to?(meth)
click to toggle source
Calls superclass method
# File lib/regularity.rb, line 90 def respond_to?(meth) regex.respond_to?(meth) || super end
start_with(*args)
click to toggle source
# 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()
click to toggle source
# File lib/regularity.rb, line 94 def to_s "#<Regularity:#{object_id} regex=/#{@str}/>" end
zero_or_more(pattern)
click to toggle source
# File lib/regularity.rb, line 69 def zero_or_more(pattern) write '%s*', pattern end
Private Instance Methods
escape(pattern)
click to toggle source
Escape special regex characters in a string
Ex:
escape("one.two") # => "one\.two"
# File lib/regularity.rb, line 143 def escape(pattern) pattern.to_s.gsub(/.+/) do |char| ESCAPED_CHARS.include?(char) ? "\\#{char}" : char end end
interpret(*args)
click to toggle source
Translate/escape characters etc and return regex-ready string
# File lib/regularity.rb, line 111 def interpret(*args) case args.length when 2 then numbered_constraint(*args) when 1 then patterned_constraint(*args) else raise ArgumentError end end
numbered_constraint(count, type)
click to toggle source
Ex: (2, 'x') or (3, :digits)
# File lib/regularity.rb, line 120 def numbered_constraint(count, type) pattern = patterned_constraint(type) raise Regularity::Error.new('Unrecognized pattern') if pattern.nil? || pattern.empty? '%s{%s}' % [pattern, count] end
patterned_constraint(pattern)
click to toggle source
Ex: ('aa') or ('$')
# File lib/regularity.rb, line 127 def patterned_constraint(pattern) escape translate(pattern) end
singularize(word)
click to toggle source
Remove a trailing 's', if there is one
# File lib/regularity.rb, line 132 def singularize(word) str = word.to_s str.end_with?('s') ? str[0..-2] : str end
translate(pattern)
click to toggle source
Translate an identifier such as :digits to [0-9], etc Returns the original identifier if no character class found
# File lib/regularity.rb, line 151 def translate(pattern) PATTERNS[singularize(pattern)] || pattern end
write(str, args=nil)
click to toggle source
# File lib/regularity.rb, line 104 def write(str, args=nil) raise Regularity::Error.new('#end_with has already been called') if @ended @str << (args.nil? ? str : str % interpret(*args)) self end