Module: GoogleSpreadsheets::Enhanced::Syncing::ClassMethods

Defined in:
lib/google_spreadsheets/enhanced/syncing.rb

Instance Method Summary collapse

Instance Method Details

#sync_with(rows_name, options) ⇒ Object

Example

class User < ActiveRecord::Base include GoogleSpreadsheets::Enhanced::Syncing sync_with :user_rows, spreadsheet_id: 'xxxx', worksheet_title: 'users' after_commit :sync_user_row



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 21

def sync_with(rows_name, options)
  options.assert_valid_keys(:spreadsheet_id, :worksheet_title, :class_name)
  options[:worksheet_title] ||= rows_name.to_s
  options[:class_name] ||= rows_name.to_s.classify
  synchronizer = Synchronizer.new(self, options[:class_name].safe_constantize, options[:spreadsheet_id], options[:worksheet_title])
  self.synchronizers.merge!(rows_name => synchronizer)

  # rows accessor
  define_singleton_method(rows_name) do
    synchronizer = self.synchronizers[rows_name]
    synchronizer.all_rows
  end

  # inbound sync all (import)
  define_singleton_method("sync_with_#{rows_name}") do
    synchronizer = self.synchronizers[rows_name]
    synchronizer.sync_with_rows
  end

  # outbound sync one (export)
  define_method("sync_#{rows_name.to_s.singularize}") do
    synchronizer = self.class.synchronizers[rows_name]
    synchronizer.sync_row(self)
  end
end