Class: Csv2hash

Inherits:
Object
  • Object
show all
Includes:
StructureValidator
Defined in:
lib/csv2hash.rb,
lib/csv2hash/parser.rb,
lib/csv2hash/notifier.rb,
lib/csv2hash/validator.rb,
lib/csv2hash/definition.rb,
lib/csv2hash/data_wrapper.rb,
lib/csv2hash/extra_validator.rb

Defined Under Namespace

Modules: Parser, StructureValidator, Validator Classes: DataWrapper, Definition, ExtraValidator, Notifier

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from StructureValidator

#rule_instance, #validate_structure!

Constructor Details

#initialize(definition, file_path, exception_mode = true, data_source = nil, ignore_blank_line = false) ⇒ Csv2hash

Returns a new instance of Csv2hash.



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/csv2hash.rb', line 25

def initialize definition, file_path, exception_mode=true, data_source=nil, ignore_blank_line=false
  @data_source = data_source
  self.definition, self.file_path = definition, file_path
  dynamic_lib_loading 'Parser'
  self.exception_mode, self.errors = exception_mode, []
  dynamic_lib_loading 'Validator'
  self.notifier = Notifier.new
  self.ignore_blank_line = ignore_blank_line
  init_plugins

end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



23
24
25
# File 'lib/csv2hash.rb', line 23

def data
  @data
end

#definitionObject

Returns the value of attribute definition.



23
24
25
# File 'lib/csv2hash.rb', line 23

def definition
  @definition
end

#errorsObject

Returns the value of attribute errors.



23
24
25
# File 'lib/csv2hash.rb', line 23

def errors
  @errors
end

#exception_modeObject

Returns the value of attribute exception_mode.



23
24
25
# File 'lib/csv2hash.rb', line 23

def exception_mode
  @exception_mode
end

#file_pathObject

Returns the value of attribute file_path.



23
24
25
# File 'lib/csv2hash.rb', line 23

def file_path
  @file_path
end

#ignore_blank_lineObject

Returns the value of attribute ignore_blank_line.



23
24
25
# File 'lib/csv2hash.rb', line 23

def ignore_blank_line
  @ignore_blank_line
end

#notifierObject

Returns the value of attribute notifier.



23
24
25
# File 'lib/csv2hash.rb', line 23

def notifier
  @notifier
end

Instance Method Details

#csv_with_errorsObject



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

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



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

def data_source
  @data_source ||= CSV.read self.file_path
end

#init_pluginsObject



37
38
39
40
41
42
43
44
# File 'lib/csv2hash.rb', line 37

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

#parseObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/csv2hash.rb', line 46

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