Class: FakePipe::Piper

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

Overview

This class cooridinates between all the text blocks. The class is initialized with some input io, an output io, and an adapter.

## Adapter An adapter is created by creating a module directly under fake_pipe. The module must respond to text_blocks which will return all the TextBlock classes needed to call on_config and on_cell.

## General IO Flow The run method is probably the most interesting. It streams in each_line of the input io and will output either the same line or the parsed line from the ‘TextObject#parse`. It’s the responsibility of the TextBlock to extract relevant table, column, cell information. This class will make keep track of when to mutate cell.

Most lines from io should be passed directly to the outputter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io:, outputter:, adapter:) ⇒ Piper

Returns a new instance of Piper.

Parameters:

  • adapter (String)

    should be a module file directly under the ‘fake_pipe’ path



22
23
24
25
26
27
# File 'lib/fake_pipe/piper.rb', line 22

def initialize(io:, outputter:, adapter:)
  self.configs = {}
  self.io = io
  self.outputter = outputter
  register_adapter(adapter)
end

Instance Attribute Details

#configsObject

Returns the value of attribute configs.



19
20
21
# File 'lib/fake_pipe/piper.rb', line 19

def configs
  @configs
end

#ioObject

Returns the value of attribute io.



19
20
21
# File 'lib/fake_pipe/piper.rb', line 19

def io
  @io
end

#outputterObject

Returns the value of attribute outputter.



19
20
21
# File 'lib/fake_pipe/piper.rb', line 19

def outputter
  @outputter
end

#text_blocksObject

Returns the value of attribute text_blocks.



19
20
21
# File 'lib/fake_pipe/piper.rb', line 19

def text_blocks
  @text_blocks
end

Instance Method Details

#detect_and_start_text_block(line) ⇒ Object

Check if a line is the begining of a new text block. When it is, trigger the callbacks so the text block can initialize itself.



59
60
61
62
63
64
65
66
67
# File 'lib/fake_pipe/piper.rb', line 59

def detect_and_start_text_block(line)
  text_blocks.detect do |block|
    matcher = block.match_start_text(line)
    if matcher && block.start_text? 
      block.on_start_text(matcher, line)
      true # result for detect
    end
  end
end

#on_cell(table:, column:, cell:) ⇒ String

Returns The mutated cell or the original if there’s no config for the table/column.

Returns:

  • (String)

    The mutated cell or the original if there’s no config for the table/column.



78
79
80
81
82
83
84
# File 'lib/fake_pipe/piper.rb', line 78

def on_cell(table:, column:, cell:)
  if config = configs[table].try(:[], column)
    Mutator.mutate(config, cell)
  else
    cell
  end
end

#on_config(table:, column:, config:) ⇒ Object

Delegate method to be called by the #text_objects to get config information from a table’s column



71
72
73
74
# File 'lib/fake_pipe/piper.rb', line 71

def on_config(table:, column:, config:)
  table = (configs[table] ||= {})
  table[column] = config
end

#register_adapter(adapter) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fake_pipe/piper.rb', line 29

def register_adapter(adapter)
  adapter_module = "fake_pipe/#{adapter}"
  require adapter_module
  adapter_class = adapter_module.camelize.constantize
  self.text_blocks = adapter_class.text_blocks.map do |block_class|
    block_class.new(delegate: self)
  end

  # AnyBlock is a catch all and needs to come last.
  text_blocks << AnyBlock.new(delegate: self)
end

#runObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fake_pipe/piper.rb', line 41

def run
  # used to track which text_block is currently in use
  current_block = text_blocks.last
  io.each_line do |line|
    if current_block.end_text?(line)
      output line
      current_block = detect_and_start_text_block(line)
    elsif configs[current_block.table]    # optimization: only parse of the text block has a table configuration
      output current_block.parse(line)
    else                                  # otherwise output the original line
      output line
    end
  end
end