Class: Pal::Handler::Base

Inherits:
Object
  • Object
show all
Includes:
Configuration, Log
Defined in:
lib/pal/handler/base.rb

Direct Known Subclasses

GenericCSVHandlerImpl

Instance Method Summary collapse

Methods included from Log

#log_debug, #log_error, #log_info, #log_warn

Methods included from Configuration

#config, #register_config

Constructor Details

#initialize(runbook) ⇒ Base

Returns a new instance of Base.

Parameters:



12
13
14
# File 'lib/pal/handler/base.rb', line 12

def initialize(runbook)
  @runbook = runbook
end

Instance Method Details

#process_runbookOperation::ProcessorContext

rubocop:disable Metrics/AbcSize



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/pal/handler/base.rb', line 18

def process_runbook
  log_debug("Processing runbook started, setting up context.")
  ctx = Operation::ProcessorContext.new
  ctx.column_type_definitions = retrieve_column_definitions

  # Get CSV parser
  # Each impl needs to return a hash of candidate columns and values
  # Extract headers
  # Extract values

  log_debug("Calling off to parse impl for CSV processing.")

  # Different impls may choose to stream file, so we hand in a location and let it decide.

  config.all_source_files.each_with_index do |file, idx|
    log_info "Opening file [#{file}][#{idx}]"

    _parse_file(ctx, _csv_processor(file)) do |row|
      ctx.add_candidate(row) if should_include?(@runbook.filters, row, ctx.column_headers)
    end
  end

  log_info "Process completed with #{ctx.candidates.size} candidate records found."

  ctx
end

#retrieve_column_definitionsHash?

Returns:

  • (Hash, nil)


57
58
59
60
61
62
63
64
65
# File 'lib/pal/handler/base.rb', line 57

def retrieve_column_definitions
  overrides = @runbook.column_overrides || {}
  path = File.join(File.dirname(__FILE__), "definitions/#{_type}.json")

  return overrides unless File.exist?(path)

  default_defs = JSON.parse(File.read(path))
  default_defs.merge(overrides)
end

#should_include?(filter_eval, row, column_headers) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)


50
51
52
53
54
# File 'lib/pal/handler/base.rb', line 50

def should_include?(filter_eval, row, column_headers)
  return true unless filter_eval

  filter_eval.test_property(row, column_headers)
end