class Csv2hash::Main

Attributes

break_on_failure[RW]
data[RW]
definition[RW]
errors[RW]
file_path_or_data[RW]
notifier[RW]
options[RW]

Public Class Methods

[](definition_name) click to toggle source
# File lib/csv2hash.rb, line 61
def [] definition_name
  @@registry[definition_name.to_sym]
end
[]=(definition_name, role) click to toggle source
# File lib/csv2hash.rb, line 65
def []= definition_name, role
  @@registry[definition_name.to_sym] = role
end
generate_definition(name, &block) click to toggle source
# File lib/csv2hash.rb, line 56
def generate_definition name, &block
  definition = Definition.new name, &block
  Main[name] = definition
end
new(*args) { || ... } click to toggle source
# File lib/csv2hash.rb, line 74
def initialize *args
  self.options = args.extract_options!
  definition_file_or_symbol, file_path_or_data = args

  unless block_given? ^ file_path_or_data
    raise ArgumentError, 'Either value or block must be given, but not both'
  end

  self.file_path_or_data = file_path_or_data || yield
  self.definition        = load_definition(definition_file_or_symbol)
  self.break_on_failure  = false
  self.errors            = []
  self.notifier          = Notifier.new

  dynamic_lib_loading 'Parser'
  dynamic_lib_loading 'Validator'

  @data_source = data_source

  init_plugins
end

Public Instance Methods

csv_with_errors() click to toggle source
# File lib/csv2hash.rb, line 133
def csv_with_errors
  @csv_with_errors ||= begin
    CsvArray.new.tap do |rows|
      errors.each do |error|
        rows << error.merge({ value: (data_source[error[:y]][error[:x]] rescue nil) })
      end
    end #.to_csv
  end
end
data_source() click to toggle source

protected

# File lib/csv2hash.rb, line 145
def data_source
  @data_source ||= begin
    self.file_path_or_data = Pathname(self.file_path_or_data) if self.file_path_or_data.is_a?(String)
    adapter_name = self.file_path_or_data.respond_to?(:to_path) ? :csv : :memory
    adapter = Adapter::Base.create(adapter_name, self.file_path_or_data)
    adapter.source
  end
end
Also aliased as: load_data_source
init_plugins() click to toggle source
# File lib/csv2hash.rb, line 96
def init_plugins
  begin
    @plugins = []
    ::Csv2hash::Plugins.constants.each do |name|
      @plugins << ::Csv2hash::Plugins.const_get(name).new(self)
    end
  rescue; end
end
load_data_source()
Alias for: data_source
parse() click to toggle source
# File lib/csv2hash.rb, line 112
def parse
  load_data_source

  definition.validate!
  definition.default!
  validate_structure!
  validate_data!

  DataWrapper.new.tap do |response|
    if valid?
      fill!
      TypeCoercer.new(data[:data]).deserialize! if Csv2hash.configuration.convert
      response.data = data[:data]
    else
      response.valid = false
      response.errors = csv_with_errors
      notifier.notify response
    end
  end
end
parse!() click to toggle source
# File lib/csv2hash.rb, line 105
def parse!
  self.break_on_failure = true
  parse
ensure
  self.break_on_failure = false
end

Private Instance Methods

dynamic_lib_loading(type) click to toggle source
# File lib/csv2hash.rb, line 157
def dynamic_lib_loading type
  case definition.type
  when Csv2hash::Definition::MAPPING
    self.extend Module.module_eval("Csv2hash::#{type}::Mapping")
  when Csv2hash::Definition::COLLECTION
    self.extend Module.module_eval("Csv2hash::#{type}::Collection")
  end
end
load_definition(definition_file_or_symbol) click to toggle source
# File lib/csv2hash.rb, line 166
def load_definition definition_file_or_symbol
  case definition_file_or_symbol
  when String
    config_file = definition_file_or_symbol
    config_file = Pathname(definition_file_or_symbol) unless config_file.respond_to?(:to_path)
    loader      = YamlLoader.new(config_file).tap &:load!
    loader.definition
  when Symbol
    Main[definition_file_or_symbol]
  when Definition
    definition_file_or_symbol
  else
    raise 'unsupported definition'
  end
end