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_path_or_data, *args) ⇒ Main

Returns a new instance of Main.



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

def initialize definition, file_path_or_data, *args
  self.options = args.extract_options!
  self.definition, self.file_path_or_data = definition, file_path_or_data
  self.break_on_failure, self.errors = false, []
  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.



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

def break_on_failure
  @break_on_failure
end

#dataObject

Returns the value of attribute data.



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

def data
  @data
end

#definitionObject

Returns the value of attribute definition.



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

def definition
  @definition
end

#errorsObject

Returns the value of attribute errors.



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

def errors
  @errors
end

#file_path_or_dataObject

Returns the value of attribute file_path_or_data.



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

def file_path_or_data
  @file_path_or_data
end

#notifierObject

Returns the value of attribute notifier.



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

def notifier
  @notifier
end

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.[](definition_name) ⇒ Object



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

def [] definition_name
  @@registry[definition_name]
end

.[]=(definition_name, role) ⇒ Object



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

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

.generate_definition(name, &block) ⇒ Object



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

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

Instance Method Details

#csv_with_errorsObject



101
102
103
104
105
106
107
108
109
# File 'lib/csv2hash.rb', line 101

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



113
114
115
116
117
118
119
# File 'lib/csv2hash.rb', line 113

def data_source
  @data_source ||= begin
    adapter_name = self.file_path_or_data.is_a?(String) ? :csv : :memory
    adapter = Adapter::Base.create(adapter_name, self.file_path_or_data)
    adapter.source
  end
end

#init_pluginsObject



65
66
67
68
69
70
71
72
# File 'lib/csv2hash.rb', line 65

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

#parseObject



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

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



74
75
76
77
78
79
# File 'lib/csv2hash.rb', line 74

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