Class: G5Updatable::LocationsUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/g5_updatable/locations_updater.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(foundation_client) ⇒ LocationsUpdater

Returns a new instance of LocationsUpdater.



5
6
7
8
# File 'lib/g5_updatable/locations_updater.rb', line 5

def initialize(foundation_client)
  @client_uid   = foundation_client.uid
  @g5_locations = foundation_client.locations
end

Class Method Details

.on_update(&block) ⇒ Object



10
11
12
13
# File 'lib/g5_updatable/locations_updater.rb', line 10

def self.on_update(&block)
  self.on_update_callbacks ||= []
  self.on_update_callbacks += [block]
end

.on_update_callbacksObject



15
16
17
# File 'lib/g5_updatable/locations_updater.rb', line 15

def self.on_update_callbacks
  @@on_update_callbacks || []
end

Instance Method Details

#updateObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/g5_updatable/locations_updater.rb', line 19

def update
  @g5_locations.each do |g5_location|
    attributes = g5_location.location_hash.dup
    location = G5Updatable::Location.
      find_or_initialize_by(uid: attributes[:uid])

    location.update_attributes!(
      urn:        attributes[:urn],
      name:       attributes[:name],
      client_uid: attributes[:client_uid],
      properties: attributes,
      updated_at: DateTime.now
    )
    self.class.on_update_callbacks.each { |cb| cb.call(location) }
  end
  destroy_orphaned_locations!
end