Class: Synchronisable::Source
- Inherits:
-
Object
- Object
- Synchronisable::Source
- Defined in:
- lib/synchronisable/source.rb
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.
16 17 18 19 20 |
# File 'lib/synchronisable/source.rb', line 16 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.
12 13 14 |
# File 'lib/synchronisable/source.rb', line 12 def child_associations @child_associations end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
13 14 15 |
# File 'lib/synchronisable/source.rb', line 13 def data @data end |
#import_ids ⇒ Object (readonly)
Returns the value of attribute import_ids.
13 14 15 |
# File 'lib/synchronisable/source.rb', line 13 def import_ids @import_ids end |
#import_record ⇒ Object
Returns the value of attribute import_record.
11 12 13 |
# File 'lib/synchronisable/source.rb', line 11 def import_record @import_record end |
#includes ⇒ Object (readonly)
Returns the value of attribute includes.
13 14 15 |
# File 'lib/synchronisable/source.rb', line 13 def includes @includes end |
#local_attrs ⇒ Object (readonly)
Returns the value of attribute local_attrs.
13 14 15 |
# File 'lib/synchronisable/source.rb', line 13 def local_attrs @local_attrs end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
13 14 15 |
# File 'lib/synchronisable/source.rb', line 13 def model @model end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
13 14 15 |
# File 'lib/synchronisable/source.rb', line 13 def parent @parent end |
#parent_associations ⇒ Object (readonly)
Returns the value of attribute parent_associations.
12 13 14 |
# File 'lib/synchronisable/source.rb', line 12 def parent_associations @parent_associations end |
#remote_attrs ⇒ Object (readonly)
Returns the value of attribute remote_attrs.
13 14 15 |
# File 'lib/synchronisable/source.rb', line 13 def remote_attrs @remote_attrs end |
#remote_id ⇒ Object (readonly)
Returns the value of attribute remote_id.
13 14 15 |
# File 'lib/synchronisable/source.rb', line 13 def remote_id @remote_id end |
#unique_id ⇒ Object (readonly)
Returns the value of attribute unique_id.
13 14 15 |
# File 'lib/synchronisable/source.rb', line 13 def unique_id @unique_id end |
Instance Method Details
#dump_message ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/synchronisable/source.rb', line 74 def %Q( remote id: '#{remote_id}', unique_id: '#{unique_id}', remote attributes: #{remote_attrs}, local attributes: #{local_attrs} ) end |
#find_import ⇒ Object
46 47 48 49 |
# File 'lib/synchronisable/source.rb', line 46 def find_import (@unique_id.present? && find_import_by_unique_id) || find_import_by_remote_id end |
#find_import_by_remote_id ⇒ Object
58 59 60 61 62 63 |
# File 'lib/synchronisable/source.rb', line 58 def find_import_by_remote_id Import.find_by( remote_id: @remote_id.to_s, synchronisable_type: @model ) end |
#find_import_by_unique_id ⇒ Object
51 52 53 54 55 56 |
# File 'lib/synchronisable/source.rb', line 51 def find_import_by_unique_id Import.find_by( unique_id: @unique_id.to_s, synchronisable_type: @model ) end |
#local_record ⇒ Object
70 71 72 |
# File 'lib/synchronisable/source.rb', line 70 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`.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/synchronisable/source.rb', line 26 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
65 66 67 68 |
# File 'lib/synchronisable/source.rb', line 65 def updatable? import_record.present? && local_record.present? end |