Class: Csv2hash
- Inherits:
-
Object
- Object
- Csv2hash
- 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
-
#data ⇒ Object
Returns the value of attribute data.
-
#definition ⇒ Object
Returns the value of attribute definition.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#exception_mode ⇒ Object
Returns the value of attribute exception_mode.
-
#file_path ⇒ Object
Returns the value of attribute file_path.
-
#ignore_blank_line ⇒ Object
Returns the value of attribute ignore_blank_line.
-
#notifier ⇒ Object
Returns the value of attribute notifier.
Instance Method Summary collapse
- #csv_with_errors ⇒ Object
-
#data_source ⇒ Object
(also: #load_data_source)
protected.
- #init_plugins ⇒ Object
-
#initialize(definition, file_path, exception_mode = true, data_source = nil, ignore_blank_line = false) ⇒ Csv2hash
constructor
A new instance of Csv2hash.
- #parse ⇒ Object
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
#data ⇒ Object
Returns the value of attribute data.
23 24 25 |
# File 'lib/csv2hash.rb', line 23 def data @data end |
#definition ⇒ Object
Returns the value of attribute definition.
23 24 25 |
# File 'lib/csv2hash.rb', line 23 def definition @definition end |
#errors ⇒ Object
Returns the value of attribute errors.
23 24 25 |
# File 'lib/csv2hash.rb', line 23 def errors @errors end |
#exception_mode ⇒ Object
Returns the value of attribute exception_mode.
23 24 25 |
# File 'lib/csv2hash.rb', line 23 def exception_mode @exception_mode end |
#file_path ⇒ Object
Returns the value of attribute file_path.
23 24 25 |
# File 'lib/csv2hash.rb', line 23 def file_path @file_path end |
#ignore_blank_line ⇒ Object
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 |
#notifier ⇒ Object
Returns the value of attribute notifier.
23 24 25 |
# File 'lib/csv2hash.rb', line 23 def notifier @notifier end |
Instance Method Details
#csv_with_errors ⇒ Object
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_source ⇒ Object 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_plugins ⇒ Object
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 |
#parse ⇒ Object
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 |