Class: Scripref::Processor

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/scripref/processor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text = nil, *mods) ⇒ Processor

Returns a new instance of Processor.

Parameters:

  • text (defaults to: nil)

    text to parse

  • mods

    one or more modules to include in processor and parser



15
16
17
18
19
20
21
22
# File 'lib/scripref/processor.rb', line 15

def initialize text=nil, *mods
  @text = text
  @mods = mods
  mods.each do |m|
    extend m
  end
  @parser = Parser.new(*mods)
end

Instance Attribute Details

#textObject

Returns the value of attribute text.



11
12
13
# File 'lib/scripref/processor.rb', line 11

def text
  @text
end

Instance Method Details

#eachObject

Iterate over each piece of str (text and parsed references) if block given, gets an iterator over the pieces otherwise.



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/scripref/processor.rb', line 40

def each
  if block_given?
    scanner = StringScanner.new(text)
    while scanner.scan(/(.*?)(#{Regexp.new(reference_re.to_s)})/)
      yield scanner[1]
      yield @parser.parse(scanner[2])
    end
    yield scanner.rest if scanner.rest
    self
  else
    enum_for :each
  end
end

#each_refObject

Iterate over each parsed reference if block given, gets an iterator over each parsed reference otherwise.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/scripref/processor.rb', line 26

def each_ref
  if block_given?
    scanner = StringScanner.new(text)
    while scanner.scan_until(reference_re)
      yield @parser.parse(scanner.matched)
    end
    self
  else
    enum_for :each_ref
  end
end

#inspectObject



54
55
56
# File 'lib/scripref/processor.rb', line 54

def inspect
  "#<#{self.class} #{@mods.inspect}>"
end