Class: VerEx

Inherits:
Regexp
  • Object
show all
Defined in:
lib/verbal_expressions.rb

Overview

For documentation and install instructions, see the main Ruby repo: github.com/ryan-endacott/VerbalExpressions.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ VerEx

Returns a new instance of VerEx.



8
9
10
11
12
13
14
15
16
# File 'lib/verbal_expressions.rb', line 8

def initialize(&block)
  @prefixes = ""
  @source = ""
  @suffixes = ""
  @modifiers = "" # TODO: Ruby Regexp option flags
  @self_before_instance_eval = eval "self", block.binding
  instance_eval &block
  super(@prefixes + @source + @suffixes, @modifiers)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



18
19
20
# File 'lib/verbal_expressions.rb', line 18

def method_missing(method, *args, &block)
  @self_before_instance_eval.send method, *args, &block
end

Instance Method Details

#alternatively(value = nil) ⇒ Object

Adds alternative expressions TODO: or is a reserved keyword in ruby, think of better name



142
143
144
145
146
147
# File 'lib/verbal_expressions.rb', line 142

def alternatively(value = nil)
  @prefixes += "(?:" unless @prefixes.include?("(")
  @suffixes = ")" + @suffixes unless @suffixes.include?(")")
  add(")|(?:")
  find(value) if value
end

#any_of(value) ⇒ Object Also known as: any

Any given character



100
101
102
103
# File 'lib/verbal_expressions.rb', line 100

def any_of(value)
  value = sanitize(value)
  add("[#{value}]")
end

#anythingObject

Any character any number of times



50
51
52
# File 'lib/verbal_expressions.rb', line 50

def anything
  add("(?:.*)")
end

#anything_but(value) ⇒ Object

Anything but these characters



55
56
57
58
# File 'lib/verbal_expressions.rb', line 55

def anything_but(value)
  value = sanitize(value)
  add("(?:[^#{value}]*)")
end

#begin_capture(name = nil) ⇒ Object

Capture groups (can optionally name)



150
151
152
153
154
155
156
# File 'lib/verbal_expressions.rb', line 150

def begin_capture(name = nil)
  if name
    add("(?<#{name}>")
  else
    add("(")
  end
end

#capture(name = nil, &block) ⇒ Object



162
163
164
165
166
# File 'lib/verbal_expressions.rb', line 162

def capture(name = nil, &block)
  begin_capture(name)
  yield
  end_capture
end

#digitObject

Any single digit



85
86
87
# File 'lib/verbal_expressions.rb', line 85

def digit
  add('\d')
end

#end_captureObject



158
159
160
# File 'lib/verbal_expressions.rb', line 158

def end_capture
  add(")")
end

#end_of_line(enable = true) ⇒ Object



39
40
41
# File 'lib/verbal_expressions.rb', line 39

def end_of_line(enable = true)
  @suffixes = '$' if enable
end

#find(value) ⇒ Object

We try to keep the syntax as user-friendly as possible. So we can use the “normal” behaviour to split the “sentences” naturally. TODO: then is reserved in ruby, so use find or think of a better name



28
29
30
31
# File 'lib/verbal_expressions.rb', line 28

def find(value)
  value = sanitize(value)
  add("(?:#{value})")
end

#integerObject

Any integer (multiple digits)



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

def integer
  one_or_more { digit }
end

#letterObject

Any single alphanumeric



75
76
77
# File 'lib/verbal_expressions.rb', line 75

def letter
  add('\w')
end

#line_breakObject Also known as: br



63
64
65
# File 'lib/verbal_expressions.rb', line 63

def line_break
  add('(?:\n|(?:\r\n))')
end

#maybe(value) ⇒ Object

Maybe is used to add values with ?



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

def maybe(value)
  value = sanitize(value)
  add("(?:#{value})?")
end

#multiple(value) ⇒ Object

Loops



134
135
136
137
138
# File 'lib/verbal_expressions.rb', line 134

def multiple(value)
  value = sanitize(value)
  value += "+"
  add(value)
end

#one_or_more(&b) ⇒ Object

At least one of some other thing



106
107
108
109
110
# File 'lib/verbal_expressions.rb', line 106

def one_or_more(&b)
  add("(?:")
  yield
  add(")+")
end

#range(*args) ⇒ Object

Usage: range( from, to [, from, to … ] )



121
122
123
124
125
126
127
128
129
130
# File 'lib/verbal_expressions.rb', line 121

def range(*args)
  value = "["
  args.each_slice(2) do |from, to|
    from = sanitize(from)
    to = sanitize(to)
    value += "#{from}-#{to}"
  end
  value += "]"
  add(value)
end

#start_of_line(enable = true) ⇒ Object

start or end of line



35
36
37
# File 'lib/verbal_expressions.rb', line 35

def start_of_line(enable = true)
  @prefixes = '^' if enable
end

#tabObject



70
71
72
# File 'lib/verbal_expressions.rb', line 70

def tab
  add('\t')
end

#whitespaceObject

Any whitespace character



95
96
97
# File 'lib/verbal_expressions.rb', line 95

def whitespace()
  add('\s+')
end

#wordObject

Any word (multiple alphanumerics)



80
81
82
# File 'lib/verbal_expressions.rb', line 80

def word
  one_or_more { letter }
end

#zero_or_more(&b) ⇒ Object



112
113
114
115
116
# File 'lib/verbal_expressions.rb', line 112

def zero_or_more(&b)
  add("(?:")
  yield
  add(")*")
end