Method: Packr::RegexpGroup#exec

Defined in:
lib/packr/base2/regexp_group.rb

#exec(string, override = nil) {|sections| ... } ⇒ Object

Yields:

  • (sections)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/packr/base2/regexp_group.rb', line 17

def exec(string, override = nil)
  string = string.to_s # type-safe
  return string if @keys.empty?
  sections = []
  override = 0 if override == IGNORE
  ret = string.gsub(Regexp.new(self.to_s, @ignore_case && Regexp::IGNORECASE)) do |match|
    removed_section = [$~.begin(0), match.size]
    offset, i, result = 1, 0, match
    arguments = [match] + $~.captures + [$~.begin(0), string]
    # Loop through the items.
    each do |item, key|
      nxt = offset + item.length + 1
      if arguments[offset] # do we have a result?
        replacement = override.nil? ? item.replacement : override
        case replacement
        when Proc
          result = replacement.call(*arguments[offset...nxt])
        when Numeric
          result = arguments[offset + replacement]
        else
          result = replacement
        end
      end
      offset = nxt
    end
    result = result.to_s
    removed_section += [result.size, match, result]
    sections << removed_section if %w[// /*].include?(match[0..1])
    result
  end
  yield(sections) if block_given?
  ret
end