Class: Maccro::Matched

Inherits:
Object
  • Object
show all
Defined in:
lib/maccro/matched.rb

Overview

placeholders: name => code_range (in method)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(matches) ⇒ Matched

Returns a new instance of Matched.



10
11
12
# File 'lib/maccro/matched.rb', line 10

def initialize(matches)
  @matches = matches.sort{|a, b| a.range <=> b.range }
end

Instance Attribute Details

#matchesObject (readonly)

Returns the value of attribute matches.



8
9
10
# File 'lib/maccro/matched.rb', line 8

def matches
  @matches
end

Class Method Details

.get_replace_pairs(entire_source, placeholders) ⇒ Object

name => code_ranges



14
15
16
17
18
19
20
# File 'lib/maccro/matched.rb', line 14

def self.get_replace_pairs(entire_source, placeholders) # name => code_ranges
  replace_pairs = {}
  placeholders.each_pair do |name, code_range|
    replace_pairs[name] = CodeUtil.code_range_to_code(entire_source, code_range)
  end
  replace_pairs # name => snippets
end

Instance Method Details

#rewrite(source) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/maccro/matched.rb', line 22

def rewrite(source)
  # TODO: implement @safe_reference
  source = source.dup
  # move tail to head, not to break code positions of unprocessed matches
  @matches.reverse.each do |match|
    replace_pairs = self.class.get_replace_pairs(source, match.placeholders)
    range = CodeUtil.code_range_to_range(source, match.range)
    source[range] = match.rule.after_code(replace_pairs)
  end
  source
end