Class: Regex::Replacer::Matcher

Inherits:
Object
  • Object
show all
Defined in:
lib/regex/replacer.rb

Overview

Basically a Regex but handles a couple extra options.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, options = {}) ⇒ Matcher

Returns a new instance of Matcher.



206
207
208
209
210
211
# File 'lib/regex/replacer.rb', line 206

def initialize(pattern, options={})
  options.each do |k,v|
    __send__("#{k}=", v) if respond_to?("#{k}=")
  end
  @regexp = parse(pattern)
end

Instance Attribute Details

#escapeObject

Returns the value of attribute escape.



197
198
199
# File 'lib/regex/replacer.rb', line 197

def escape
  @escape
end

#globalObject

Returns the value of attribute global.



194
195
196
# File 'lib/regex/replacer.rb', line 194

def global
  @global
end

#insensitiveObject

Returns the value of attribute insensitive.



203
204
205
# File 'lib/regex/replacer.rb', line 203

def insensitive
  @insensitive
end

#multilineObject

Returns the value of attribute multiline.



200
201
202
# File 'lib/regex/replacer.rb', line 200

def multiline
  @multiline
end

Instance Method Details

#=~(string) ⇒ Object



214
215
216
# File 'lib/regex/replacer.rb', line 214

def =~(string)
  @regexp =~ string
end

#match(string) ⇒ Object



219
220
221
# File 'lib/regex/replacer.rb', line 219

def match(string)
  @regexp.match(string)
end

#parse(pattern) ⇒ Object

Parse pattern matcher.



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/regex/replacer.rb', line 229

def parse(pattern)
  case pattern
  when Regexp
    pattern
  when /^\$/
    Templates.const_get($'.upcase)
  when /^\/(.*?)\/(\w+)$/
    flags = []
    @global = true if $2.index('g')
    flags << Regexp::MULTILINE  if $2.index('m') or multiline
    flags << Regexp::IGNORECASE if $2.index('i') or insensitive
    if $2.index('e') or escape
      Regexp.new(Regexp.escape($1), *flags)
    else
      Regexp.new($1, *flags)
    end
  else
    flags = []
    flags << Regexp::MULTILINE  if multiline
    flags << Regexp::IGNORECASE if insensitive
    if escape
      Regexp.new(Regexp.escape(pattern), *flags)
    else
      Regexp.new(pattern, *flags)
    end
  end
end

#to_reObject



224
225
226
# File 'lib/regex/replacer.rb', line 224

def to_re
  @regexp
end