Class: Synchronizer::Base
- Inherits:
-
Object
- Object
- Synchronizer::Base
- Defined in:
- lib/synchronizer/base.rb
Instance Attribute Summary collapse
-
#external_attrs ⇒ Object
readonly
Returns the value of attribute external_attrs.
Class Method Summary collapse
- .active_record_class ⇒ Object
- .delete_missing_import_records(old_ids, current_ids) ⇒ Object
- .import_class ⇒ Object
- .import_records ⇒ Object
- .local_type ⇒ Object
- .sync(external_items, options = {}) ⇒ Object
Instance Method Summary collapse
- #external_id ⇒ Object
- #import_record ⇒ Object
-
#initialize(external_attrs) ⇒ Base
constructor
A new instance of Base.
- #local_attrs ⇒ Object
- #local_record ⇒ Object
- #mapping ⇒ Object
- #sync ⇒ Object
Constructor Details
#initialize(external_attrs) ⇒ Base
Returns a new instance of Base.
6 7 8 |
# File 'lib/synchronizer/base.rb', line 6 def initialize(external_attrs) @external_attrs = external_attrs.with_indifferent_access end |
Instance Attribute Details
#external_attrs ⇒ Object (readonly)
Returns the value of attribute external_attrs.
4 5 6 |
# File 'lib/synchronizer/base.rb', line 4 def external_attrs @external_attrs end |
Class Method Details
.active_record_class ⇒ Object
145 146 147 |
# File 'lib/synchronizer/base.rb', line 145 def active_record_class name.demodulize.sub(/Synchronizer$/, '').constantize end |
.delete_missing_import_records(old_ids, current_ids) ⇒ Object
118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/synchronizer/base.rb', line 118 def delete_missing_import_records(old_ids, current_ids) deleted_ids = old_ids - current_ids import_class.find(deleted_ids).each do |import_record| unless import_record.destroy = import_record.errors..join('.') = import_record.errors..join('.') = "Error import_error_message = #{} " \ "local_error_message = #{} " \ "when deleting #{import_record}" @@errors << end end end |
.import_class ⇒ Object
137 138 139 |
# File 'lib/synchronizer/base.rb', line 137 def import_class Synchronizer.config.import_class_name.constantize end |
.import_records ⇒ Object
133 134 135 |
# File 'lib/synchronizer/base.rb', line 133 def import_records import_class.with_type(local_type) end |
.local_type ⇒ Object
141 142 143 |
# File 'lib/synchronizer/base.rb', line 141 def local_type "#{active_record_class.name}" end |
.sync(external_items, options = {}) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/synchronizer/base.rb', line 95 def sync(external_items, = {}) @@errors = [] destroy_missed = [:destroy_missed] == false ? false : true model_sync_name = name.demodulize old_import_ids = [:scope_import_record_ids] || import_records.pluck(:id) current_import_ids = [] external_items.each do |item_attrs| sync_object = new(item_attrs) if sync_object.sync current_import_ids << sync_object.import_record.id end end if destroy_missed delete_missing_import_records(old_import_ids, current_import_ids) end return @@errors end |
Instance Method Details
#external_id ⇒ Object
35 36 37 |
# File 'lib/synchronizer/base.rb', line 35 def external_id external_attrs[Synchronizer.config.external_id || :id] end |
#import_record ⇒ Object
31 32 33 |
# File 'lib/synchronizer/base.rb', line 31 def import_record @import_record ||= self.class.import_class.find_by_external_id_and_local_type(external_id, local_type) end |
#local_attrs ⇒ Object
27 28 29 |
# File 'lib/synchronizer/base.rb', line 27 def local_attrs @local_attrs ||= mapping end |
#local_record ⇒ Object
23 24 25 |
# File 'lib/synchronizer/base.rb', line 23 def local_record @local_record ||= import_record.try(:local_record) end |
#mapping ⇒ Object
39 40 41 |
# File 'lib/synchronizer/base.rb', line 39 def mapping raise "Owerwrite this method for mapping external_attrs to local!" end |
#sync ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/synchronizer/base.rb', line 10 def sync error_handler do if import_record.present? update_local_record! else create_local_record! create_import_record! end return true end end |