Class: Synchronisable::Source
- Inherits:
-
Object
- Object
- Synchronisable::Source
- Defined in:
- lib/synchronisable/source.rb
Overview
Synchronization source.
Instance Attribute Summary collapse
-
#associations ⇒ Object
readonly
Returns the value of attribute associations.
-
#import_ids ⇒ Object
readonly
Returns the value of attribute import_ids.
-
#import_record ⇒ Object
Returns the value of attribute import_record.
-
#local_attrs ⇒ Object
readonly
Returns the value of attribute local_attrs.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#remote_attrs ⇒ Object
readonly
Returns the value of attribute remote_attrs.
-
#remote_id ⇒ Object
readonly
Returns the value of attribute remote_id.
Instance Method Summary collapse
- #create_record_pair ⇒ Object
- #dump_message ⇒ Object
-
#initialize(model, parent, remote_attrs) ⇒ Source
constructor
A new instance of Source.
- #local_record ⇒ Object
-
#prepare ⇒ Object
private
Prepares synchronization source:
remote_id,local_attributes,import_recordandassociations. - #updatable? ⇒ Boolean
- #update_record ⇒ Object
Constructor Details
#initialize(model, parent, remote_attrs) ⇒ Source
9 10 11 12 |
# File 'lib/synchronisable/source.rb', line 9 def initialize(model, parent, remote_attrs) @model, @parent, @synchronizer = model, parent, model.synchronizer @remote_attrs = remote_attrs.with_indifferent_access end |
Instance Attribute Details
#associations ⇒ Object (readonly)
Returns the value of attribute associations.
5 6 7 |
# File 'lib/synchronisable/source.rb', line 5 def associations @associations end |
#import_ids ⇒ Object (readonly)
Returns the value of attribute import_ids.
5 6 7 |
# File 'lib/synchronisable/source.rb', line 5 def import_ids @import_ids end |
#import_record ⇒ Object
Returns the value of attribute import_record.
4 5 6 |
# File 'lib/synchronisable/source.rb', line 4 def import_record @import_record end |
#local_attrs ⇒ Object (readonly)
Returns the value of attribute local_attrs.
5 6 7 |
# File 'lib/synchronisable/source.rb', line 5 def local_attrs @local_attrs end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
5 6 7 |
# File 'lib/synchronisable/source.rb', line 5 def model @model end |
#remote_attrs ⇒ Object (readonly)
Returns the value of attribute remote_attrs.
5 6 7 |
# File 'lib/synchronisable/source.rb', line 5 def remote_attrs @remote_attrs end |
#remote_id ⇒ Object (readonly)
Returns the value of attribute remote_id.
5 6 7 |
# File 'lib/synchronisable/source.rb', line 5 def remote_id @remote_id end |
Instance Method Details
#create_record_pair ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/synchronisable/source.rb', line 49 def create_record_pair record = @model.create!(@local_attrs) @import_record = Import.create!( :synchronisable_id => record.id, :synchronisable_type => @model.to_s, :remote_id => @remote_id, :attrs => @local_attrs ) end |
#dump_message ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/synchronisable/source.rb', line 63 def %Q( remote id: '#{remote_id}', remote attributes: #{remote_attrs}, local attributes: #{local_attrs} ) end |
#local_record ⇒ Object
59 60 61 |
# File 'lib/synchronisable/source.rb', line 59 def local_record @import_record.try(:synchronisable) end |
#prepare ⇒ 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. Sets foreign key if current model is specified as has_one or has_many association of parent model.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/synchronisable/source.rb', line 20 def prepare @remote_id = @synchronizer.extract_remote_id(@remote_attrs) @local_attrs = @synchronizer.map_attributes(@remote_attrs) @associations = @synchronizer.associations_for(@local_attrs) # TODO: implement destroy_missed, somehow pass @import_ids, # get all import records if nil # remove associations keys from local attributes @local_attrs.delete_if do |key, _| @associations.keys.any? { |a| a.key == key } end @import_record = Import.find_by( :remote_id => @remote_id, :synchronisable_type => @model ) set_foreign_key end |
#updatable? ⇒ Boolean
41 42 43 |
# File 'lib/synchronisable/source.rb', line 41 def updatable? @import_record.present? && local_record.present? end |
#update_record ⇒ Object
45 46 47 |
# File 'lib/synchronisable/source.rb', line 45 def update_record local_record.update_attributes!(@local_attrs) end |