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

Returns a new instance of Main.



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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

Instance Attribute Details

#break_on_failureObject

Returns the value of attribute break_on_failure.



72
73
74
# File 'lib/csv2hash.rb', line 72

def break_on_failure
  @break_on_failure
end

#dataObject

Returns the value of attribute data.



72
73
74
# File 'lib/csv2hash.rb', line 72

def data
  @data
end

#definitionObject

Returns the value of attribute definition.



72
73
74
# File 'lib/csv2hash.rb', line 72

def definition
  @definition
end

#errorsObject

Returns the value of attribute errors.



72
73
74
# File 'lib/csv2hash.rb', line 72

def errors
  @errors
end

#file_path_or_dataObject

Returns the value of attribute file_path_or_data.



72
73
74
# File 'lib/csv2hash.rb', line 72

def file_path_or_data
  @file_path_or_data
end

#notifierObject

Returns the value of attribute notifier.



72
73
74
# File 'lib/csv2hash.rb', line 72

def notifier
  @notifier
end

#optionsObject

Returns the value of attribute options.



72
73
74
# File 'lib/csv2hash.rb', line 72

def options
  @options
end

Class Method Details

.[](definition_name) ⇒ Object



61
62
63
# File 'lib/csv2hash.rb', line 61

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

.[]=(definition_name, role) ⇒ Object



65
66
67
# File 'lib/csv2hash.rb', line 65

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

.generate_definition(name, &block) ⇒ Object



56
57
58
59
# File 'lib/csv2hash.rb', line 56

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

Instance Method Details

#csv_with_errorsObject



133
134
135
136
137
138
139
140
141
# 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_sourceObject Also known as: load_data_source

protected



145
146
147
148
149
150
151
152
# 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

#init_pluginsObject



96
97
98
99
100
101
102
103
# 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

#parseObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# 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!Object



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

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