Class: DataShift::LoaderBase
- Inherits:
-
Object
- Object
- DataShift::LoaderBase
- Extended by:
- Forwardable
- Defined in:
- lib/loaders/loader_base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#binder ⇒ Object
Returns the value of attribute binder.
-
#configuration ⇒ Object
readonly
Returns the value of attribute configuration.
-
#doc_context ⇒ Object
Returns the value of attribute doc_context.
-
#file_name ⇒ Object
Returns the value of attribute file_name.
Attributes included from Delimiters
#attribute_list_end, #attribute_list_start, #csv_delimiter, #key_value_sep, #text_delim
Instance Method Summary collapse
- #abort_on_failure? ⇒ Boolean
-
#bind_headers(headers) ⇒ Object
(also: #bind_fields)
Core API.
-
#configure_from(yaml_file) ⇒ Object
Any Config under key ‘LoaderBase’ is merged over existing options - taking precedence.
-
#initialize ⇒ LoaderBase
constructor
A new instance of LoaderBase.
- #load_object_class ⇒ Object
- #report ⇒ Object
-
#reset(object = nil) ⇒ Object
Reset the loader, including database object to be populated, and load counts.
- #run(file_name, object_class) ⇒ Object
- #set_headers(headings) ⇒ Object
- #setup_load_class(load_class) ⇒ Object
Methods included from Querying
#find_or_new, #get_record_by, #get_record_by!, #search_for_record, where_field_and_values
Methods included from Logging
#logdir, #logdir=, #logger, #verbose
Methods included from Delimiters
#column_delim, #column_delim=, #eol, #multi_assoc_delim, #multi_assoc_delim=, #multi_facet_delim, #multi_value_delim, #multi_value_delim=, #name_value_delim, #name_value_delim=, #setmulti_facet_delim
Constructor Details
#initialize ⇒ LoaderBase
Returns a new instance of LoaderBase.
38 39 40 41 42 43 44 45 |
# File 'lib/loaders/loader_base.rb', line 38 def initialize @file_name = '' @doc_context = DocContext.new(Object) @binder = Binder.new @configuration = DataShift::Loaders::Configuration.call end |
Instance Attribute Details
#binder ⇒ Object
Returns the value of attribute binder.
24 25 26 |
# File 'lib/loaders/loader_base.rb', line 24 def binder @binder end |
#configuration ⇒ Object (readonly)
Returns the value of attribute configuration.
36 37 38 |
# File 'lib/loaders/loader_base.rb', line 36 def configuration @configuration end |
#doc_context ⇒ Object
Returns the value of attribute doc_context.
26 27 28 |
# File 'lib/loaders/loader_base.rb', line 26 def doc_context @doc_context end |
#file_name ⇒ Object
Returns the value of attribute file_name.
23 24 25 |
# File 'lib/loaders/loader_base.rb', line 23 def file_name @file_name end |
Instance Method Details
#abort_on_failure? ⇒ Boolean
68 69 70 |
# File 'lib/loaders/loader_base.rb', line 68 def abort_on_failure? !! configuration.abort_on_failure end |
#bind_headers(headers) ⇒ Object Also known as: bind_fields
Core API
Returns an instance of DataShift::Binder
Given a list of free text column names from inbound headers, map all headers to a domain model containing details on operator, look ups etc.
See configuration options
[:ignore] : List of column headers to ignore when building operator ma
[:include_all] : Include all headers in processing - takes precedence of :force_inclusion
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/loaders/loader_base.rb', line 97 def bind_headers( headers ) logger.info("Binding #{headers.size} inbound headers to #{load_object_class.name}") @binder ||= DataShift::Binder.new begin binder.map_inbound_headers(load_object_class, headers) rescue => e logger.error("Failed to map header row to set of database operators : #{e.inspect}") logger.error( e.backtrace ) raise MappingDefinitionError, 'Failed to map header row to set of database operators' end unless binder.missing_bindings.empty? logger.warn("Following headings couldn't be mapped to #{load_object_class}:") binder.missing_bindings.each { |m| logger.warn("Heading [#{m.inbound_name}] - Index (#{m.inbound_index})") } raise MappingDefinitionError, "Missing mappings for columns : #{binder.missing_bindings.join(',')}" if configuration.strict end mandatory = DataShift::Mandatory.new(configuration.mandatory) unless mandatory.contains_all?(binder) mandatory.missing_columns.each do |er| logger.error "Mandatory column missing - expected column '#{er}'" end raise MissingMandatoryError, 'Mandatory columns missing - see logs - please fix and retry.' end binder end |
#configure_from(yaml_file) ⇒ Object
Any Config under key ‘LoaderBase’ is merged over existing options - taking precedence.
Any Config under a key equal to the full name of the Loader class (e.g DataShift::SpreeEcom::ImageLoader) is merged over existing options - taking precedence.
Format :
LoaderClass:
option: value
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/loaders/loader_base.rb', line 144 def configure_from(yaml_file) logger.info("Reading Datashift loader config from: #{yaml_file.inspect}") data = YAML.load( ERB.new( IO.read(yaml_file) ).result ) logger.info("Read Datashift config: #{data.inspect}") @config.merge!(data['LoaderBase']) if data['LoaderBase'] @config.merge!(data[self.class.name]) if data[self.class.name] DataShift::Transformation.factory { |f| f.configure_from(load_object_class, yaml_file) } ContextFactory.configure(load_object_class, yaml_file) logger.info("Loader Options : #{@config.inspect}") end |
#load_object_class ⇒ Object
72 73 74 |
# File 'lib/loaders/loader_base.rb', line 72 def load_object_class doc_context.klass end |
#report ⇒ Object
81 82 83 |
# File 'lib/loaders/loader_base.rb', line 81 def report reporters.each(&:report) end |
#reset(object = nil) ⇒ Object
Reset the loader, including database object to be populated, and load counts
64 65 66 |
# File 'lib/loaders/loader_base.rb', line 64 def reset(object = nil) doc_context.reset(object) end |
#run(file_name, object_class) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/loaders/loader_base.rb', line 51 def run(file_name, object_class) @file_name = file_name setup_load_class(object_class) logger.info("Loading objects of type #{load_object_class}") # no implementation - derived classes must implement perform_load end |
#set_headers(headings) ⇒ Object
76 77 78 79 |
# File 'lib/loaders/loader_base.rb', line 76 def set_headers(headings) logger.info("Setting parsed headers to [#{headings.inspect}]") doc_context.headers = headings end |
#setup_load_class(load_class) ⇒ Object
47 48 49 |
# File 'lib/loaders/loader_base.rb', line 47 def setup_load_class(load_class) @doc_context = DocContext.new( MapperUtils.ensure_class(load_class) ) end |