Class: Csv2hash::Main

Inherits:
Object
  • Object
show all
Includes:
StructureValidator
Defined in:
lib/csv2hash.rb

Constant Summary collapse

@@registry =
Registry.new

Constants included from StructureValidator

StructureValidator::MAX_COLUMN, StructureValidator::MIN_COLUMN, StructureValidator::RULES_NAME

Constants included from StructureValidator::Deprecation

StructureValidator::Deprecation::NEW_SYNTAX, StructureValidator::Deprecation::OLD_MAX_COLUMN, StructureValidator::Deprecation::OLD_MIN_COLUMN, StructureValidator::Deprecation::OLD_RULES_NAME

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from StructureValidator

#rule_instance, #validate_structure!

Methods included from StructureValidator::Deprecation

#check_params

Constructor Details

#initialize(definition_file_or_symbol, file_path_or_data, *args) ⇒ Main

Returns a new instance of Main.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/csv2hash.rb', line 52

def initialize definition_file_or_symbol, file_path_or_data, *args
  self.options           = args.extract_options!
  self.definition        = load_definition(definition_file_or_symbol)
  self.file_path_or_data = file_path_or_data
  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

Instance Attribute Details

#break_on_failureObject

Returns the value of attribute break_on_failure.



50
51
52
# File 'lib/csv2hash.rb', line 50

def break_on_failure
  @break_on_failure
end

#dataObject

Returns the value of attribute data.



50
51
52
# File 'lib/csv2hash.rb', line 50

def data
  @data
end

#definitionObject

Returns the value of attribute definition.



50
51
52
# File 'lib/csv2hash.rb', line 50

def definition
  @definition
end

#errorsObject

Returns the value of attribute errors.



50
51
52
# File 'lib/csv2hash.rb', line 50

def errors
  @errors
end

#file_path_or_dataObject

Returns the value of attribute file_path_or_data.



50
51
52
# File 'lib/csv2hash.rb', line 50

def file_path_or_data
  @file_path_or_data
end

#notifierObject

Returns the value of attribute notifier.



50
51
52
# File 'lib/csv2hash.rb', line 50

def notifier
  @notifier
end

#optionsObject

Returns the value of attribute options.



50
51
52
# File 'lib/csv2hash.rb', line 50

def options
  @options
end

Class Method Details

.[](definition_name) ⇒ Object



39
40
41
# File 'lib/csv2hash.rb', line 39

def [] definition_name
  @@registry[definition_name.to_sym]
end

.[]=(definition_name, role) ⇒ Object



43
44
45
# File 'lib/csv2hash.rb', line 43

def []= definition_name, role
  @@registry[definition_name.to_sym] = role
end

.generate_definition(name, &block) ⇒ Object



34
35
36
37
# File 'lib/csv2hash.rb', line 34

def generate_definition name, &block
  definition = Definition.new name, &block
  Main[name] = definition
end

Instance Method Details

#csv_with_errorsObject



104
105
106
107
108
109
110
111
112
# File 'lib/csv2hash.rb', line 104

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_sourceObject Also known as: load_data_source

protected



116
117
118
119
120
121
122
# File 'lib/csv2hash.rb', line 116

def data_source
  @data_source ||= begin
    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

#init_pluginsObject



68
69
70
71
72
73
74
75
# File 'lib/csv2hash.rb', line 68

def init_plugins
  begin
    @plugins = []
    ::Csv2hash::Plugins.constants.each do |name|
      @plugins << ::Csv2hash::Plugins.const_get(name).new(self)
    end
  rescue; end
end

#parseObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/csv2hash.rb', line 84

def parse
  load_data_source

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

  Csv2hash::DataWrapper.new.tap do |response|
    if valid?
      fill!
      response.data = data[:data]
    else
      response.valid = false
      response.errors = csv_with_errors
      notifier.notify response
    end
  end
end

#parse!Object



77
78
79
80
81
82
# File 'lib/csv2hash.rb', line 77

def parse!
  self.break_on_failure = true
  parse
ensure
  self.break_on_failure = false
end