module Parxer::Dsl

Public Instance Methods

add_parser_option(key, value) click to toggle source
# File lib/parxer/dsl/dsl.rb, line 87
def add_parser_option(key, value)
  in_context { add_config_option(key, value) }
end
after_parse_row(callback_method = nil, &block) click to toggle source
# File lib/parxer/dsl/dsl.rb, line 80
def after_parse_row(callback_method = nil, &block)
  in_context do
    action = callback_method || block
    parser_callbacks.add_callback(:after_parse_row, action)
  end
end
column(id, config, &block) click to toggle source
# File lib/parxer/dsl/dsl.rb, line 49
def column(id, config, &block)
  in_context do
    formatter_name = config.delete(:format)
    @current_attr = attributes.add_attribute(id, name: config.delete(:name))
    format_with(formatter_name) if formatter_name
    block&.call
  end
ensure
  @current_attr = nil
end
ctx_dependencies_map() click to toggle source
# File lib/parxer/dsl/dsl.rb, line 33
def ctx_dependencies_map
  {
    add_parser_option: [],
    validate_file: [],
    validate_row: [],
    column: [],
    after_parse_row: [],
    validate: [:column],
    format_with: [:column]
  }
end
current_context() click to toggle source
# File lib/parxer/dsl/dsl.rb, line 45
def current_context
  @current_context ||= []
end
format_with(*params, &block) click to toggle source
# File lib/parxer/dsl/dsl.rb, line 72
def format_with(*params, &block)
  in_context do
    formatter_name = !params.first.is_a?(Hash) ? params.first : :custom
    config = (params.first.is_a?(Hash) ? params.first : params.second) || {}
    @current_attr.add_formatter(formatter_name, config, &block)
  end
end
in_context() { || ... } click to toggle source
# File lib/parxer/dsl/dsl.rb, line 6
def in_context
  current_method = caller_locations(1, 1)[0].label.to_sym
  validate_context!(current_method)
  current_context << current_method
  yield
ensure
  current_context.pop
end
method_aliases() click to toggle source
# File lib/parxer/dsl/dsl.rb, line 29
def method_aliases
  @method_aliases ||= {}
end
validate(validator_name, config = {}, &block) click to toggle source
# File lib/parxer/dsl/dsl.rb, line 60
def validate(validator_name, config = {}, &block)
  in_context { @current_attr.add_validator(validator_name, config, &block) }
end
validate_context!(current_method) click to toggle source
# File lib/parxer/dsl/dsl.rb, line 15
def validate_context!(current_method)
  dependencies = ctx_dependencies_map[current_method]

  if current_context != dependencies
    msg = if dependencies.any?
            "'#{current_method}' needs to run inside '#{dependencies.last}' block"
          else
            "'#{current_method}' can't run inside '#{current_context.last}' block"
          end

    raise Parxer::DslError.new(msg)
  end
end
validate_file(validator_name, config = {}, &block) click to toggle source
# File lib/parxer/dsl/dsl.rb, line 64
def validate_file(validator_name, config = {}, &block)
  in_context { add_validator(validator_name, config, &block) }
end
validate_row(validator_name, config = {}, &block) click to toggle source
# File lib/parxer/dsl/dsl.rb, line 68
def validate_row(validator_name, config = {}, &block)
  in_context { add_row_validator(validator_name, config, &block) }
end