class FakePipe::Piper
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`
Attributes
Public Class Methods
@param [String] adapter should be a module file directly under the 'fake_pipe' path
# 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
Public Instance Methods
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.
# 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
@return [String] The mutated cell or the original if there's no config for
the table/column.
# 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
Delegate method to be called by the text_objects to get config information from a table's column
# File lib/fake_pipe/piper.rb, line 71 def on_config(table:, column:, config:) table = (configs[table] ||= {}) table[column] = config end
# 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
# 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
Private Instance Methods
Simple wrapper to print to the configured outputter
# File lib/fake_pipe/piper.rb, line 89 def output(text) outputter.puts text end