Class: SimpleRegex

Inherits:
Object
  • Object
show all
Defined in:
lib/simple_regex.rb,
lib/simple_regex/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.0.1'

Instance Method Summary collapse

Constructor Details

#initializeSimpleRegex

Returns a new instance of SimpleRegex.



20
21
22
23
# File 'lib/simple_regex.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/simple_regex.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/simple_regex.rb', line 82

def =~(other)
  regex =~ other
end

#append(*args) ⇒ Object Also known as: then



30
31
32
# File 'lib/simple_regex.rb', line 30

def append(*args)
  write interpret(*args)
end

#at_least(times, pattern) ⇒ Object



61
62
63
# File 'lib/simple_regex.rb', line 61

def at_least(times, pattern)
  between [times, nil], pattern
end

#at_most(times, pattern) ⇒ Object



65
66
67
# File 'lib/simple_regex.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/simple_regex.rb', line 53

def between(range, pattern)
  unless range.length == 2 && range.any? { |i| i.is_a?(Integer) }
    raise SimpleRegex: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/simple_regex.rb', line 35

def end_with(*args)
  write '%s$', args
  @ended = true
  self
end

#inspectObject



98
99
100
# File 'lib/simple_regex.rb', line 98

def inspect
  to_s
end

#maybe(*args) ⇒ Object



41
42
43
# File 'lib/simple_regex.rb', line 41

def maybe(*args)
  write '%s?', args
end

#not(*args) ⇒ Object



45
46
47
# File 'lib/simple_regex.rb', line 45

def not(*args)
  write '(?!%s)', args
end

#one_of(ary) ⇒ Object



49
50
51
# File 'lib/simple_regex.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/simple_regex.rb', line 73

def one_or_more(pattern)
  write '%s+', pattern
end

#regexObject Also known as: get



77
78
79
# File 'lib/simple_regex.rb', line 77

def regex
  Regexp.new(@str)
end

#respond_to?(meth) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/simple_regex.rb', line 90

def respond_to?(meth)
  regex.respond_to?(meth) || super
end

#start_with(*args) ⇒ Object

Raises:



25
26
27
28
# File 'lib/simple_regex.rb', line 25

def start_with(*args)
  raise SimpleRegex::Error.new('#start_with? called multiple times') unless @str.empty?
  write '^%s', args
end

#to_sObject



94
95
96
# File 'lib/simple_regex.rb', line 94

def to_s
  "#<SimpleRegex:#{object_id} regex=/#{@str}/>"
end

#zero_or_more(pattern) ⇒ Object



69
70
71
# File 'lib/simple_regex.rb', line 69

def zero_or_more(pattern)
  write '%s*', pattern
end