Class: GoogleSpreadsheets::Enhanced::Syncing::Synchronizer
- Inherits:
-
Object
- Object
- GoogleSpreadsheets::Enhanced::Syncing::Synchronizer
- Defined in:
- lib/google_spreadsheets/enhanced/syncing.rb
Instance Attribute Summary collapse
-
#record_class ⇒ Object
readonly
Returns the value of attribute record_class.
-
#row_class ⇒ Object
readonly
Returns the value of attribute row_class.
-
#spreadsheet_id ⇒ Object
readonly
Returns the value of attribute spreadsheet_id.
-
#worksheet_title ⇒ Object
readonly
Returns the value of attribute worksheet_title.
Instance Method Summary collapse
- #all_rows ⇒ Object
-
#initialize(record_class, row_class, spreadsheet_id, worksheet_title, options = {}) ⇒ Synchronizer
constructor
A new instance of Synchronizer.
- #reset ⇒ Object
- #sync_row(record) ⇒ Object
- #sync_with_rows ⇒ Object
- #worksheet ⇒ Object
Constructor Details
#initialize(record_class, row_class, spreadsheet_id, worksheet_title, options = {}) ⇒ Synchronizer
Returns a new instance of Synchronizer.
53 54 55 56 57 58 59 |
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 53 def initialize(record_class, row_class, spreadsheet_id, worksheet_title, = {}) @record_class = record_class @row_class = row_class || default_class_for(REL_NAME_ROW) @spreadsheet_id = spreadsheet_id @worksheet_title = worksheet_title = end |
Instance Attribute Details
#record_class ⇒ Object (readonly)
Returns the value of attribute record_class.
51 52 53 |
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 51 def record_class @record_class end |
#row_class ⇒ Object (readonly)
Returns the value of attribute row_class.
51 52 53 |
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 51 def row_class @row_class end |
#spreadsheet_id ⇒ Object (readonly)
Returns the value of attribute spreadsheet_id.
51 52 53 |
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 51 def spreadsheet_id @spreadsheet_id end |
#worksheet_title ⇒ Object (readonly)
Returns the value of attribute worksheet_title.
51 52 53 |
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 51 def worksheet_title @worksheet_title end |
Instance Method Details
#all_rows ⇒ Object
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 61 def all_rows reflections = worksheet.class.reflections.values.find_all{|ref| ref.is_a?(LinkRelations::LinkRelationReflection) } if reflection = reflections.find{|ref| ref.klass == row_class } worksheet.send(reflection.name) elsif reflection = reflections.find{|ref| ref.[:rel] == REL_NAME_ROW } worksheet.send(reflection.name, as: row_class.to_s) else raise "Reflection for #{row_class.to_s} not found." end end |
#reset ⇒ Object
130 131 132 133 |
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 130 def reset @worksheet = nil self end |
#sync_row(record) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 106 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 ⇒ Object
72 73 74 75 76 77 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 |
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 72 def sync_with_rows reset records_to_save = {} all_rows.each do |row| record_id = row.id.to_i record = records_to_save[record_id] || record_class.find_or_initialize_by(id: record_id) if row.all_values_empty? # Due to destroy if exists record.instance_variable_set(:@due_to_destroy, true) records_to_save[row.id.to_i] = record next end row_attributes = Hash[row.aliased_attributes.map{|attr| [attr, row.send(attr)] }] row_attributes.reject!{|_, v| v.blank? } unless [:include_blank] if [:assigner] record.send([:assigner], row_attributes) else assign_row_attributes(record, row_attributes) end records_to_save[row.id.to_i] = 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.instance_variable_get(:@due_to_destroy) record.destroy else record.save end end end end end |
#worksheet ⇒ Object
123 124 125 126 127 128 |
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 123 def worksheet @worksheet ||= default_class_for(REL_NAME_SPREADSHEET) .find(spreadsheet_id) .worksheets .find_by!(title: worksheet_title) end |