Sha256: ac952f9dee8a4aba405fed360d15a5ae4de22932d629ed9897037e3a392da085

Contents?: true

Size: 1.28 KB

Versions: 31

Compression:

Stored size: 1.28 KB

Contents

module PatronusFati::DataObservers
  class ClientObserver
    include DataMapper::Observer

    observe PatronusFati::DataModels::Client

    before :save do
      break unless self.valid?

      self.reported_online = active?

      @change_type = self.new? ? :new : :changed
      if @change_type == :changed
        dirty = self.dirty_attributes.map { |a| a.first.name }.map(&:to_s)
        dirty.select! { |k, _| full_state.keys.include?(k) || k == 'reported_online' }

        # If there weren't any meaningful changes, don't print out anything
        # after we save.
        if dirty.empty?
          @change_type = nil
          next
        end

        changes = dirty.map do |attr|
          clean = original_attributes[PatronusFati::DataModels::Client.properties[attr]]
          dirty = dirty_attributes[PatronusFati::DataModels::Client.properties[attr]]

          [attr, [clean, dirty]]
        end

        @change_list = Hash[changes]
        @change_list.delete('reported_online')
      end
    end

    after :save do
      next unless @change_type

      mac.update_cached_counts!

      PatronusFati.event_handler.event(
        :client,
        @change_type,
        self.full_state,
        @change_list || {}
      )

      @change_type = nil
      @change_list = nil
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
patronus_fati-0.9.11 lib/patronus_fati/data_observers/client_observer.rb
patronus_fati-0.9.10 lib/patronus_fati/data_observers/client_observer.rb
patronus_fati-0.9.9 lib/patronus_fati/data_observers/client_observer.rb
patronus_fati-0.9.8 lib/patronus_fati/data_observers/client_observer.rb
patronus_fati-0.9.7 lib/patronus_fati/data_observers/client_observer.rb
patronus_fati-0.9.6 lib/patronus_fati/data_observers/client_observer.rb
patronus_fati-0.9.5 lib/patronus_fati/data_observers/client_observer.rb
patronus_fati-0.9.4 lib/patronus_fati/data_observers/client_observer.rb
patronus_fati-0.9.3 lib/patronus_fati/data_observers/client_observer.rb
patronus_fati-0.9.2 lib/patronus_fati/data_observers/client_observer.rb
patronus_fati-0.9.1 lib/patronus_fati/data_observers/client_observer.rb
patronus_fati-0.9.0 lib/patronus_fati/data_observers/client_observer.rb
patronus_fati-0.8.18 lib/patronus_fati/data_observers/client_observer.rb
patronus_fati-0.8.17 lib/patronus_fati/data_observers/client_observer.rb
patronus_fati-0.8.16 lib/patronus_fati/data_observers/client_observer.rb
patronus_fati-0.8.15 lib/patronus_fati/data_observers/client_observer.rb
patronus_fati-0.8.14 lib/patronus_fati/data_observers/client_observer.rb
patronus_fati-0.8.13 lib/patronus_fati/data_observers/client_observer.rb
patronus_fati-0.8.12 lib/patronus_fati/data_observers/client_observer.rb
patronus_fati-0.8.11 lib/patronus_fati/data_observers/client_observer.rb