Class: Scrubber

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

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Scrubber

Returns a new instance of Scrubber.



2
3
4
# File 'lib/scrubber.rb', line 2

def initialize(&block)
  config(&block) if block_given?
end

Instance Method Details

#config {|@config| ... } ⇒ Object

Yields:



6
7
8
9
# File 'lib/scrubber.rb', line 6

def config
  @config = Scrub::Config.new
  yield @config
end

#scrub(data, splitter = /\n/) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/scrubber.rb', line 11

def scrub(data, splitter = /\n/)
  lines = data.split(splitter)
  while(line = lines.shift)
    line.strip!
    @config.matchers.each do |matcher, prok|
      if match = line.match(matcher)
        prok.call(line, *match.captures)
      end
    end
    @config.between_matchers.each do |matcher, options|
      if line =~ matcher
        end_matcher, prok, conditions = options
        next_line = nil
        while(next_line = lines.shift) && (next_line !~ end_matcher)
          if conditions[:when]
            if match = next_line.match(conditions[:when])
              prok.call(next_line, *match.captures) 
            end
          else
            prok.call(next_line) 
          end
        end
      end
    end
  end
end