Class: GoogleSpreadsheets::Enhanced::Syncing::Synchronizer

Inherits:
Object
  • Object
show all
Defined in:
lib/google_spreadsheets/enhanced/syncing.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record_class, row_class, spreadsheet_id, worksheet_title, options = {}) ⇒ Synchronizer

Returns a new instance of Synchronizer.



54
55
56
57
58
59
60
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 54

def initialize(record_class, row_class, spreadsheet_id, worksheet_title, options = {})
  @record_class = record_class
  @row_class = row_class || default_class_for(REL_NAME_ROW)
  @spreadsheet_id = spreadsheet_id
  @worksheet_title = worksheet_title
  @options = options
end

Instance Attribute Details

#record_classObject (readonly)

Returns the value of attribute record_class.



52
53
54
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 52

def record_class
  @record_class
end

#row_classObject (readonly)

Returns the value of attribute row_class.



52
53
54
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 52

def row_class
  @row_class
end

#spreadsheet_idObject (readonly)

Returns the value of attribute spreadsheet_id.



52
53
54
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 52

def spreadsheet_id
  @spreadsheet_id
end

#worksheet_titleObject (readonly)

Returns the value of attribute worksheet_title.



52
53
54
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 52

def worksheet_title
  @worksheet_title
end

Instance Method Details

#all_rowsObject



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 62

def all_rows
  reflections = worksheet.class.reflections.values.find_all{|ref| ref.is_a?(LinkRelations::LinkRelationReflection) }
  collection =
    if reflection = reflections.find{|ref| ref.klass == row_class }
      worksheet.send(reflection.name)
    elsif reflection = reflections.find{|ref| ref.options[:rel] == REL_NAME_ROW }
      worksheet.send(reflection.name, as: row_class.to_s)
    else
      raise "Reflection for #{row_class.to_s} not found."
    end
  if @options[:scope]
    collection = collection.instance_exec(&@options[:scope])
  end
  collection
end

#resetObject



149
150
151
152
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 149

def reset
  @worksheet = nil
  self
end

#sync_row(record) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 125

def sync_row(record)
  return if record.instance_variable_defined?(:@_skip_outbound_sync) &&
            record.instance_variable_get(:@_skip_outbound_sync)
  row = all_rows.find(record.id)
  if record.destroyed?
    row.destroy
  else
    # TODO: separate by AttributeAssignment
    row.aliased_attributes.each do |attr|
      value = (record.respond_to?(:attributes_before_type_cast) && record.attributes_before_type_cast[attr]) ||
              record.send(attr)
      row.send("#{attr}=", value)
    end
    row.save
  end
end

#sync_with_rows(scope = nil) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 78

def sync_with_rows(scope = nil)
  reset
  records_to_save = {}
  rows = all_rows
  rows = rows.instance_exec(&scope) if scope
  rows.each do |row|
    if row.id.present?
      record_id = row.id.to_i
      record = records_to_save[record_id] || record_class.find_or_initialize_by(id: record_id)
    elsif !@options[:ignore_blank_id]
      record_id = SecureRandom.hex(8) # dummy id
      record = record_class.new
    else
      next
    end
    if row.all_values_empty?
      # Due to destroy if exists
      record.mark_for_destruction
      records_to_save[record_id] = record
      next
    end
    row_attributes = Hash[row.aliased_attributes.map{|attr| [attr, row.send(attr)] }]
    row_attributes.reject!{|_, v| v.blank? } unless @options[:include_blank]
    if assigner = @options[:assigner]
      if assigner.is_a?(Proc)
        record.instance_exec(row_attributes, &assigner)
      else
        record.send(assigner, row_attributes)
      end
    else
      assign_row_attributes(record, row_attributes)
    end
    records_to_save[record_id] = record
  end
  skipping_outbound_sync_of(records_to_save.values) do |records_with_skipped_outbound|
    transaction_if_possible(record_class) do
      records_with_skipped_outbound.each do |record|
        if record.marked_for_destruction?
          record.destroy
        else
          record.save
        end
      end
    end
  end
end

#worksheetObject



142
143
144
145
146
147
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 142

def worksheet
  @worksheet ||= default_class_for(REL_NAME_SPREADSHEET)
                   .find(spreadsheet_id)
                   .worksheets
                   .find_by!(title: worksheet_title)
end