Class: RubySync::Connectors::CsvFileConnector

Inherits:
FileConnector show all
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

Methods inherited from FileConnector

#add, #each_change, #output_file_name, #started

Methods inherited from BaseConnector

#add, #association_context, #association_for, #can_act_as_vault?, #clean, #create_operations_for, #each_entry, #entry_for_own_association_key, event_method, #find_associated, #has_entry_for_key?, #initialize, #is_delete_echo?, #is_echo?, #is_vault?, #own_association_key_for, #path_for_own_association_key, #start, #started, #stop, #stopped, #sync_started, #sync_stopped, target_transform, #test_add, #test_delete, #test_modify, track_associations_with, track_changes_with

Methods included from ConnectorEventProcessing

#associated_path, #clean, #delete_from_mirror, #perform_add, #perform_delete, #perform_modify, #process, #update_mirror

Methods included from Utilities

#as_array, #call_if_exists, #class_called, #class_for_name, #class_name_for, #connector_called, #dump_after, #dump_before, #effective_operations, #ensure_dir_exists, #get_preference, #get_preference_file_path, #include_in_search_path, #log_progress, #perform_operations, #perform_transform, #pipeline_called, #set_preference, #something_called, #with_rescue

Constructor Details

This class inherits a constructor from RubySync::Connectors::BaseConnector

Class Method Details

.fieldsObject



68
69
70
71
72
73
# File 'lib/ruby_sync/connectors/csv_file_connector.rb', line 68

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_configObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ruby_sync/connectors/csv_file_connector.rb', line 52

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



94
95
96
# File 'lib/ruby_sync/connectors/csv_file_connector.rb', line 94

def [](path)
  nil
end

#delete(path) ⇒ Object



98
99
100
# File 'lib/ruby_sync/connectors/csv_file_connector.rb', line 98

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 an add event for each row found in the file.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ruby_sync/connectors/csv_file_connector.rb', line 30

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.add(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.



85
86
87
88
89
90
# File 'lib/ruby_sync/connectors/csv_file_connector.rb', line 85

def path_for(data)
  if defined? path_field
    return data[path_field]
  end
  return nil
end

#write_record(file, path, operations) ⇒ Object



75
76
77
78
79
# File 'lib/ruby_sync/connectors/csv_file_connector.rb', line 75

def write_record file, path, operations
  record = perform_operations operations
  line = CSV.generate_line(field_names.map {|f| record[f]})
  file.puts line
end