Class: ActiveHarmony::Synchronizer

Inherits:
Object
  • Object
show all
Defined in:
lib/active_harmony/synchronizer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSynchronizer

Initializes new Service Synchronizer to do some synchronizing magic.



10
11
12
13
# File 'lib/active_harmony/synchronizer.rb', line 10

def initialize
  @contexts = {}
  @configuration = SynchronizerConfiguration.new
end

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration.



5
6
7
# File 'lib/active_harmony/synchronizer.rb', line 5

def configuration
  @configuration
end

#factoryObject

Returns the value of attribute factory.



5
6
7
# File 'lib/active_harmony/synchronizer.rb', line 5

def factory
  @factory
end

#serviceObject

Returns the value of attribute service.



5
6
7
# File 'lib/active_harmony/synchronizer.rb', line 5

def service
  @service
end

Instance Method Details

#configure { ... } ⇒ Object

Takes block to configure synchronization

Yields:

  • Configuration block



33
34
35
# File 'lib/active_harmony/synchronizer.rb', line 33

def configure
  yield(@configuration)
end

#object_nameSymbol

Returns object name for currently used factory

Returns:

  • (Symbol)

    Object name



18
19
20
# File 'lib/active_harmony/synchronizer.rb', line 18

def object_name
  @factory.object_name.to_sym
end

#pull_collectionObject

Pulls whole remote collection. If it cannot find matching local object, it will create one. This method is slow, useful for initial import, not for regular updates. For regular updates, only changed remote objects should be updates using pull_object

See Also:



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/active_harmony/synchronizer.rb', line 107

def pull_collection
  @service.set_contexts(@contexts)
  collection = @service.list(object_name)
  @service.clear_contexts
  collection.each_with_index do |remote_object_hash, index|
    remote_id = remote_object_hash.delete("id")
    local_object = @factory.with_remote_id(remote_id)
    unless local_object
      local_object = @factory.new
      local_object.update_remote_id(remote_id)
    end
    local_object.before_pull(self) if local_object.respond_to?(:before_pull)
    local_object._collection_order = index
    fields = configuration.synchronizable_for_pull
    fields.each do |field|
      value = remote_object_hash[field.to_s]
      local_object.send("#{field}=", value)
    end
    local_object.after_pull(self) if local_object.respond_to?(:after_pull)
    local_object.save
  end
  collection.count
end

#pull_object(id) ⇒ Boolean

Pulls object from remote service

Parameters:

  • Remote (Integer)

    ID of object.

Returns:

  • (Boolean)

    Result of pulling



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/active_harmony/synchronizer.rb', line 41

def pull_object(id)
  local_object = @factory.with_remote_id(id)
  if local_object
    # FIXME What if there's no local object and we still want to set some
    # contexts?
    @service.set_contexts(local_object.contexts)
  else
    local_object = @factory.new
  end
  local_object.before_pull(self) if local_object.respond_to?(:before_pull)
  object_hash = @service.show(object_name, id)
  @service.clear_contexts
  local_object._remote_id = object_hash.delete('id')
  fields = configuration.synchronizable_for_pull
  fields.each do |key|
    value = object_hash[key.to_s]
    local_object.send("#{key}=", value)
  end
  local_object.after_pull(self) if local_object.respond_to?(:after_pull)
  local_object.save
end

#push_object(local_object) ⇒ Object

Pushes local object to remote services. Er, I mean, its attributes. Like not object itself. Just attributes.

Parameters:

  • Local (Object)

    object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/active_harmony/synchronizer.rb', line 68

def push_object(local_object)
  object_name = @factory.object_name.to_sym
  
  local_object.before_push(self) if local_object.respond_to?(:before_push)
  
  changes = {}
  fields = configuration.synchronizable_for_push
  fields.each do |atr|
    value = local_object.send(atr)
    changes[atr.to_s] = value
  end
  
  @service.set_contexts(local_object.contexts)
  
  if local_object._remote_id
    @service.update(object_name, local_object._remote_id, changes)
  else
    result = @service.create(object_name, changes)
    if result
      local_object._remote_id = result['id']
      fields = configuration.synchronizable_for_pull
      fields.each do |atr|
        local_object.write_attribute(atr, result[atr.to_s])
      end
      local_object.save
    end
  end
  
  local_object.after_push(self) if local_object.respond_to?(:after_push)
  @service.clear_contexts
end

#set_context(context_name, context_value) ⇒ Object

Sets context for this synchronizer

Parameters:

  • Name (Symbol)

    of context

  • Value (String, Integer)


26
27
28
# File 'lib/active_harmony/synchronizer.rb', line 26

def set_context(context_name, context_value)
  @contexts[context_name.to_sym] = context_value
end