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) ⇒ 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) ⇒ Synchronizer
Returns a new instance of Synchronizer.
51 52 53 54 55 56 |
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 51 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.
49 50 51 |
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 49 def record_class @record_class end |
#row_class ⇒ Object (readonly)
Returns the value of attribute row_class.
49 50 51 |
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 49 def row_class @row_class end |
#spreadsheet_id ⇒ Object (readonly)
Returns the value of attribute spreadsheet_id.
49 50 51 |
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 49 def spreadsheet_id @spreadsheet_id end |
#worksheet_title ⇒ Object (readonly)
Returns the value of attribute worksheet_title.
49 50 51 |
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 49 def worksheet_title @worksheet_title end |
Instance Method Details
#all_rows ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 58 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
109 110 111 112 |
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 109 def reset @worksheet = nil self end |
#sync_row(record) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 85 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
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 69 def sync_with_rows reset records = all_rows.map do |row| record_class.find_or_initialize_by(id: row.id).tap do |record| row.aliased_attributes.each do |attr| record.send("#{attr}=", row.send(attr)) end end end skipping_outbound_sync_of(records) do |records_with_skipped_outbound| transaction_if_possible(record_class) do records_with_skipped_outbound.each(&:save) end end end |
#worksheet ⇒ Object
102 103 104 105 106 107 |
# File 'lib/google_spreadsheets/enhanced/syncing.rb', line 102 def worksheet @worksheet ||= default_class_for(REL_NAME_SPREADSHEET) .find(spreadsheet_id) .worksheets .find_by!(title: worksheet_title) end |