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 @options = 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
141 142 143 144 |
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 141 def reset @worksheet = nil self end |
#sync_row(record) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 117 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 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 72 def sync_with_rows reset records_to_save = {} all_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 |
#worksheet ⇒ Object
134 135 136 137 138 139 |
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 134 def worksheet @worksheet ||= default_class_for(REL_NAME_SPREADSHEET) .find(spreadsheet_id) .worksheets .find_by!(title: worksheet_title) end |