Class: Synchronisable::Source
Overview
Synchronization source.
Constant Summary collapse
- CHILD_ASSOCIATION_KEYS =
%i(has_one has_many)
- PARENT_ASSOCIATION_KEYS =
%i(belongs_to)
Instance Attribute Summary collapse
-
#child_associations ⇒ Object
readonly
Returns the value of attribute child_associations.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#import_ids ⇒ Object
readonly
Returns the value of attribute import_ids.
-
#import_record ⇒ Object
Returns the value of attribute import_record.
-
#includes ⇒ Object
readonly
Returns the value of attribute includes.
-
#local_attrs ⇒ Object
readonly
Returns the value of attribute local_attrs.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#parent_associations ⇒ Object
readonly
Returns the value of attribute parent_associations.
-
#remote_attrs ⇒ Object
readonly
Returns the value of attribute remote_attrs.
-
#remote_id ⇒ Object
readonly
Returns the value of attribute remote_id.
-
#unique_id ⇒ Object
readonly
Returns the value of attribute unique_id.
Instance Method Summary collapse
- #dump_message ⇒ Object
- #find_import ⇒ Object
- #find_import_by_remote_id ⇒ Object
- #find_import_by_unique_id ⇒ Object
-
#initialize(model, parent, includes) ⇒ Source
constructor
A new instance of Source.
- #local_record ⇒ Object
-
#prepare(data, remote_attrs) ⇒ Object
private
Prepares synchronization source: ‘remote_id`, `local_attributes`, `import_record` and `associations`.
- #updatable? ⇒ Boolean
Constructor Details
#initialize(model, parent, includes) ⇒ Source
Returns a new instance of Source.
14 15 16 17 18 |
# File 'lib/synchronisable/source.rb', line 14 def initialize(model, parent, includes) @model, @parent, @synchronizer = model, parent, model.synchronizer @model_name = @model.to_s.demodulize.underscore.to_sym @includes = includes end |
Instance Attribute Details
#child_associations ⇒ Object (readonly)
Returns the value of attribute child_associations.
10 11 12 |
# File 'lib/synchronisable/source.rb', line 10 def child_associations @child_associations end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
11 12 13 |
# File 'lib/synchronisable/source.rb', line 11 def data @data end |
#import_ids ⇒ Object (readonly)
Returns the value of attribute import_ids.
11 12 13 |
# File 'lib/synchronisable/source.rb', line 11 def import_ids @import_ids end |
#import_record ⇒ Object
Returns the value of attribute import_record.
9 10 11 |
# File 'lib/synchronisable/source.rb', line 9 def import_record @import_record end |
#includes ⇒ Object (readonly)
Returns the value of attribute includes.
11 12 13 |
# File 'lib/synchronisable/source.rb', line 11 def includes @includes end |
#local_attrs ⇒ Object (readonly)
Returns the value of attribute local_attrs.
11 12 13 |
# File 'lib/synchronisable/source.rb', line 11 def local_attrs @local_attrs end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
11 12 13 |
# File 'lib/synchronisable/source.rb', line 11 def model @model end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
11 12 13 |
# File 'lib/synchronisable/source.rb', line 11 def parent @parent end |
#parent_associations ⇒ Object (readonly)
Returns the value of attribute parent_associations.
10 11 12 |
# File 'lib/synchronisable/source.rb', line 10 def parent_associations @parent_associations end |
#remote_attrs ⇒ Object (readonly)
Returns the value of attribute remote_attrs.
11 12 13 |
# File 'lib/synchronisable/source.rb', line 11 def remote_attrs @remote_attrs end |
#remote_id ⇒ Object (readonly)
Returns the value of attribute remote_id.
11 12 13 |
# File 'lib/synchronisable/source.rb', line 11 def remote_id @remote_id end |
#unique_id ⇒ Object (readonly)
Returns the value of attribute unique_id.
11 12 13 |
# File 'lib/synchronisable/source.rb', line 11 def unique_id @unique_id end |
Instance Method Details
#dump_message ⇒ Object
72 73 74 75 76 77 78 79 |
# File 'lib/synchronisable/source.rb', line 72 def %Q( remote id: '#{remote_id}', unique_id: '#{unique_id}', remote attributes: #{remote_attrs}, local attributes: #{local_attrs} ) end |
#find_import ⇒ Object
44 45 46 47 |
# File 'lib/synchronisable/source.rb', line 44 def find_import (@unique_id.present? && find_import_by_unique_id) || find_import_by_remote_id end |
#find_import_by_remote_id ⇒ Object
56 57 58 59 60 61 |
# File 'lib/synchronisable/source.rb', line 56 def find_import_by_remote_id Import.where( remote_id: @remote_id.to_s, synchronisable_type: @model.to_s ).first end |
#find_import_by_unique_id ⇒ Object
49 50 51 52 53 54 |
# File 'lib/synchronisable/source.rb', line 49 def find_import_by_unique_id Import.where( unique_id: @unique_id.to_s, synchronisable_type: @model.to_s ).first end |
#local_record ⇒ Object
68 69 70 |
# File 'lib/synchronisable/source.rb', line 68 def local_record @import_record.try(:synchronisable) end |
#prepare(data, remote_attrs) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Prepares synchronization source: ‘remote_id`, `local_attributes`, `import_record` and `associations`.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/synchronisable/source.rb', line 24 def prepare(data, remote_attrs) @data = @parent .try(:source).try(:data) .try(:merge, data) || data @remote_attrs = remote_attrs.with_indifferent_access @remote_id = @synchronizer.extract_remote_id(@remote_attrs) @local_attrs = @synchronizer.map_attributes(@remote_attrs) @unique_id = @synchronizer.uid(@local_attrs) @associations = @synchronizer.associations_for(@local_attrs) @parent_associations = filter_associations(PARENT_ASSOCIATION_KEYS) @child_associations = filter_associations(CHILD_ASSOCIATION_KEYS) @import_record = find_import remove_association_keys_from_local_attrs end |
#updatable? ⇒ Boolean
63 64 65 66 |
# File 'lib/synchronisable/source.rb', line 63 def updatable? import_record.present? && local_record.present? end |