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
#is_vault, #name, #once_only, #pipeline
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
#add, #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, #remove_association, #remove_associations, #remove_mirror, #start, #started, #stop, #stopped, #sync_started, #sync_stopped, #test_add, #test_delete, #test_modify
Methods included from Utilities
#as_array, #call_if_exists, #connector_called, #effective_operations, #ensure_dir_exists, #get_preference, #get_preference_file_path, #include_in_search_path, #log_progress, #perform_operations, #pipeline_called, #set_preference, #something_called, #with_rescue
Constructor Details
This class inherits a constructor from RubySync::Connectors::BaseConnector
Class Method Details
.fields ⇒ Object
69 70 71 72 73 74 |
# File 'lib/ruby_sync/connectors/csv_file_connector.rb', line 69 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
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/ruby_sync/connectors/csv_file_connector.rb', line 53 def self.sample_config return <<END # True if the first line of each file is a header # and should be ignored header_line true 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
95 96 97 |
# File 'lib/ruby_sync/connectors/csv_file_connector.rb', line 95 def [](path) nil end |
#delete(path) ⇒ Object
99 100 101 |
# File 'lib/ruby_sync/connectors/csv_file_connector.rb', line 99 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.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/ruby_sync/connectors/csv_file_connector.rb', line 31 def each_file_change(filename) header = header_line CSV.open(filename, 'r') do |row| if header # should we ignore the first line header = false next end 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}" row[i] and 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.
86 87 88 89 90 91 |
# File 'lib/ruby_sync/connectors/csv_file_connector.rb', line 86 def path_for(data) if defined? path_field return data[path_field] end return nil end |
#write_record(file, path, operations) ⇒ Object
76 77 78 79 80 |
# File 'lib/ruby_sync/connectors/csv_file_connector.rb', line 76 def write_record file, path, operations record = perform_operations operations line = CSV.generate_line(field_names.map {|f| record[f]}) file.puts line end |