Class: Csv2hash::Main

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

Constant Summary

@@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

- (Main) initialize(definition, file_path_or_data, *args)

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

- (Object) break_on_failure

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

- (Object) data

Returns the value of attribute data



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

def data
  @data
end

- (Object) definition

Returns the value of attribute definition



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

def definition
  @definition
end

- (Object) errors

Returns the value of attribute errors



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

def errors
  @errors
end

- (Object) file_path_or_data

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

- (Object) notifier

Returns the value of attribute notifier



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

def notifier
  @notifier
end

- (Object) options

Returns the value of attribute options



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

def options
  @options
end

Class Method Details

+ (Object) [](definition_name)



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

def [] definition_name
  @@registry[definition_name]
end

+ (Object) []=(definition_name, role)



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

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

+ (Object) generate_definition(name, &block)



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

- (Object) csv_with_errors



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

- (Object) data_source 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

- (Object) init_plugins



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

- (Object) parse



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

- (Object) parse!



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