Class: RubySync::Connectors::CsvFileConnector
- Inherits:
-
FileConnector
- Object
- BaseConnector
- FileConnector
- RubySync::Connectors::CsvFileConnector
- Defined in:
- lib/ruby_sync/connectors/csv_file_connector.rb
Overview
Reads files containing Comma Separated Values from the in_path directory and treats each line as an incoming event.
This Connector can’t act as an identity vault.
Instance Attribute Summary
Attributes inherited from BaseConnector
Class Method Summary collapse
Instance Method Summary collapse
-
#[](path) ⇒ Object
A file based system probably can’t look data up so always return nil for lookup attempts.
- #delete(path) ⇒ Object
-
#each_file_change(filename) ⇒ Object
Called for each filename matching in_glob in in_path Yields a modify event for each row found in the file.
-
#path_for(data) ⇒ Object
Return the value to be used as the source_path for the event given the supplied row data.
- #write_record(file, path, operations) ⇒ Object
Methods inherited from FileConnector
#add, #each_change, #output_file_name, #started
Methods inherited from BaseConnector
#associate, #association_context, #association_for, #association_key_for, #association_to_path_dbm_filename, #associations_for, #can_act_as_vault?, class_for, class_name_for, #clean, #create_operations_for, #dbm_path, #digest, #each_change, #each_entry, #entry_for_own_association_key, #find_associated, #has_entry_for_key?, #initialize, #is_delete_echo?, #is_echo?, #is_vault?, #mirror_dbm_filename, #own_association_key_for, #path_for_association, #path_for_own_association_key, #path_to_association_dbm_filename, #perform_operations, #remove_association, #remove_associations, #remove_mirror, #start, #started, #stop, #stopped, #test_add, #test_delete, #test_modify
Methods included from Utilities
#base_path, #call_if_exists, #connector_called, #ensure_dir_exists, #find_base_path, #get_preference, #get_preference_file_path, #include_in_search_path, #log_progress, #pipeline_called, #set_preference, #something_called, #with_rescue
Constructor Details
This class inherits a constructor from RubySync::Connectors::BaseConnector
Class Method Details
.fields ⇒ Object
57 58 59 60 61 62 |
# File 'lib/ruby_sync/connectors/csv_file_connector.rb', line 57 def self.fields c = self.new c.field_names and !c.field_names.empty? or log.warn "Please set the field names in the connector config." c.field_names || [] end |
.sample_config ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/ruby_sync/connectors/csv_file_connector.rb', line 45 def self.sample_config return <<END field_names ['names', 'of', 'the', 'columns'] path_field 'name_of_field_to_use_as_the_id' in_path '/directory/to/read/files/from' out_path '/directory/to/write/files/to' in_glob '*.csv' out_extension '.csv' END end |
Instance Method Details
#[](path) ⇒ Object
A file based system probably can’t look data up so always return nil for lookup attempts
83 84 85 |
# File 'lib/ruby_sync/connectors/csv_file_connector.rb', line 83 def [](path) nil end |
#delete(path) ⇒ Object
87 88 89 |
# File 'lib/ruby_sync/connectors/csv_file_connector.rb', line 87 def delete(path) log.warn "Delete on CSV driver ignored for #{path}" end |
#each_file_change(filename) ⇒ Object
Called for each filename matching in_glob in in_path Yields a modify event for each row found in the file.
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/ruby_sync/connectors/csv_file_connector.rb', line 29 def each_file_change(filename) CSV.open(filename, 'r') do |row| if defined? field_name &&row.length > field_names.length log.warn "#{name}: Row in file #{filename} exceeds defined field_names" end data = {} row.each_index do |i| field_name = (i < field_names.length)? field_names[i] : "field_#{i}" data[field_name] = row[i].data end association_key = source_path = path_for(data) yield RubySync::Event.modify(self, source_path, association_key, create_operations_for(data)) end end |
#path_for(data) ⇒ Object
Return the value to be used as the source_path for the event given the supplied row data.
74 75 76 77 78 79 |
# File 'lib/ruby_sync/connectors/csv_file_connector.rb', line 74 def path_for(data) if defined? path_field return data[path_field] end return nil end |
#write_record(file, path, operations) ⇒ Object
64 65 66 67 68 |
# File 'lib/ruby_sync/connectors/csv_file_connector.rb', line 64 def write_record file, path, operations record = perform_operations operations line = CSV.generate_line(field_names.map {|f| record[f]}) file.puts line end |