Class: Csv2hash::Main
- Inherits:
-
Object
- Object
- Csv2hash::Main
- Includes:
- StructureValidator
- Defined in:
- lib/csv2hash.rb
Constant Summary collapse
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
-
#break_on_failure ⇒ Object
Returns the value of attribute break_on_failure.
-
#data ⇒ Object
Returns the value of attribute data.
-
#definition ⇒ Object
Returns the value of attribute definition.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#file_path_or_data ⇒ Object
Returns the value of attribute file_path_or_data.
-
#notifier ⇒ Object
Returns the value of attribute notifier.
-
#options ⇒ Object
Returns the value of attribute options.
Class Method Summary collapse
- .[](definition_name) ⇒ Object
- .[]=(definition_name, role) ⇒ Object
- .generate_definition(name, &block) ⇒ Object
Instance Method Summary collapse
- #csv_with_errors ⇒ Object
-
#data_source ⇒ Object
(also: #load_data_source)
protected.
- #init_plugins ⇒ Object
-
#initialize(*args) ⇒ Main
constructor
A new instance of Main.
- #parse ⇒ Object
- #parse! ⇒ Object
Methods included from StructureValidator
#rule_instance, #validate_structure!
Methods included from StructureValidator::Deprecation
Constructor Details
#initialize(*args) ⇒ Main
Returns a new instance of Main.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/csv2hash.rb', line 73 def initialize *args self. = args. 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_failure ⇒ Object
Returns the value of attribute break_on_failure.
71 72 73 |
# File 'lib/csv2hash.rb', line 71 def break_on_failure @break_on_failure end |
#data ⇒ Object
Returns the value of attribute data.
71 72 73 |
# File 'lib/csv2hash.rb', line 71 def data @data end |
#definition ⇒ Object
Returns the value of attribute definition.
71 72 73 |
# File 'lib/csv2hash.rb', line 71 def definition @definition end |
#errors ⇒ Object
Returns the value of attribute errors.
71 72 73 |
# File 'lib/csv2hash.rb', line 71 def errors @errors end |
#file_path_or_data ⇒ Object
Returns the value of attribute file_path_or_data.
71 72 73 |
# File 'lib/csv2hash.rb', line 71 def file_path_or_data @file_path_or_data end |
#notifier ⇒ Object
Returns the value of attribute notifier.
71 72 73 |
# File 'lib/csv2hash.rb', line 71 def notifier @notifier end |
#options ⇒ Object
Returns the value of attribute options.
71 72 73 |
# File 'lib/csv2hash.rb', line 71 def @options end |
Class Method Details
.[](definition_name) ⇒ Object
60 61 62 |
# File 'lib/csv2hash.rb', line 60 def [] definition_name @@registry[definition_name.to_sym] end |
.[]=(definition_name, role) ⇒ Object
64 65 66 |
# File 'lib/csv2hash.rb', line 64 def []= definition_name, role @@registry[definition_name.to_sym] = role end |
.generate_definition(name, &block) ⇒ Object
55 56 57 58 |
# File 'lib/csv2hash.rb', line 55 def generate_definition name, &block definition = Definition.new name, &block Main[name] = definition end |
Instance Method Details
#csv_with_errors ⇒ Object
132 133 134 135 136 137 138 139 140 |
# File 'lib/csv2hash.rb', line 132 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
144 145 146 147 148 149 150 151 |
# File 'lib/csv2hash.rb', line 144 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_plugins ⇒ Object
95 96 97 98 99 100 101 102 |
# File 'lib/csv2hash.rb', line 95 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
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/csv2hash.rb', line 111 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
104 105 106 107 108 109 |
# File 'lib/csv2hash.rb', line 104 def parse! self.break_on_failure = true parse ensure self.break_on_failure = false end |