Class: Vernacular::RegexModifier

Inherits:
Object
  • Object
show all
Defined in:
lib/vernacular/regex_modifier.rb

Overview

Represents a modification to Ruby source that should be injected into the require process that modifies the source via a regex pattern.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, replacement = nil, &block) ⇒ RegexModifier

Returns a new instance of RegexModifier.



9
10
11
12
13
# File 'lib/vernacular/regex_modifier.rb', line 9

def initialize(pattern, replacement = nil, &block)
  @pattern = pattern
  @replacement = replacement
  @block = block
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



7
8
9
# File 'lib/vernacular/regex_modifier.rb', line 7

def block
  @block
end

#patternObject (readonly)

Returns the value of attribute pattern.



7
8
9
# File 'lib/vernacular/regex_modifier.rb', line 7

def pattern
  @pattern
end

#replacementObject (readonly)

Returns the value of attribute replacement.



7
8
9
# File 'lib/vernacular/regex_modifier.rb', line 7

def replacement
  @replacement
end

Instance Method Details

#componentsObject



23
24
25
# File 'lib/vernacular/regex_modifier.rb', line 23

def components
  [pattern, replacement] + (block ? block.source_location : [])
end

#modify(source) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/vernacular/regex_modifier.rb', line 15

def modify(source)
  if replacement
    source.gsub(pattern, replacement)
  else
    source.gsub(pattern, &block)
  end
end