Class: Tengine::Resource::Provider::Synchronizer

Inherits:
Object
  • Object
show all
Defined in:
lib/tengine/resource/provider.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider, target_name) ⇒ Synchronizer

Returns a new instance of Synchronizer.



132
133
134
135
# File 'lib/tengine/resource/provider.rb', line 132

def initialize(provider, target_name)
  @provider, @target_name = provider, target_name
  @log_prefix = "#{self.class.name} for #{provider.name}"
end

Instance Attribute Details

#log_prefixObject (readonly)

Returns the value of attribute log_prefix.



131
132
133
# File 'lib/tengine/resource/provider.rb', line 131

def log_prefix
  @log_prefix
end

#providerObject (readonly)

Returns the value of attribute provider.



131
132
133
# File 'lib/tengine/resource/provider.rb', line 131

def provider
  @provider
end

#target_nameObject (readonly)

Returns the value of attribute target_name.



131
132
133
# File 'lib/tengine/resource/provider.rb', line 131

def target_name
  @target_name
end

Class Method Details

.fetch_known_target_method(method_name = nil) ⇒ Object



107
108
109
110
111
# File 'lib/tengine/resource/provider.rb', line 107

def fetch_known_target_method(method_name = nil)
  @fetch_known_target_methods ||= {}
  @fetch_known_target_methods[self] = method_name if method_name
  @fetch_known_target_methods[self]
end

.map(attr_name, prop_name = nil, &block) ⇒ Object



113
114
115
# File 'lib/tengine/resource/provider.rb', line 113

def map(attr_name, prop_name = nil, &block)
  property_map[attr_name] = prop_name || block
end

.property_mapObject



117
118
119
120
# File 'lib/tengine/resource/provider.rb', line 117

def property_map
  @property_map ||= {}
  @property_map[self] ||= {}
end

Instance Method Details

#executeObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/tengine/resource/provider.rb', line 137

def execute
  actual_target_hashs = fetch_actual_target_hashs

  provider.reload
  known_targets = fetch_known_targets

  id_key = property_map[:provided_id].to_s
  updated_target_hashs = []
  destroyed_targets = []

  known_targets.each do |known_target|
    actual_target_hash = actual_target_hashs.detect do |t|
      (t[id_key] || t[id_key.to_sym]) == known_target.provided_id
    end
    if actual_target_hash
      updated_target_hashs << actual_target_hash
    else
      destroyed_targets << known_target
    end
  end
  created_target_hashs = actual_target_hashs - updated_target_hashs

  differential_update(updated_target_hashs)
  create_by_hashs(created_target_hashs)
  destroy_targets(destroyed_targets)
end

#fetch_known_target_methodObject



123
124
125
# File 'lib/tengine/resource/provider.rb', line 123

def fetch_known_target_method
  self.class.fetch_known_target_method
end

#property_mapObject



127
128
129
# File 'lib/tengine/resource/provider.rb', line 127

def property_map
  @property_map ||= self.class.property_map
end