Sha256: 8d3c7159e6012cb9c1eb8b174fa0a99ed3ea315aaf846ebd6e2745599dcac441

Contents?: true

Size: 1.62 KB

Versions: 12

Compression:

Stored size: 1.62 KB

Contents

module PatronusFati
  module DataModels
    module CommonState
      module KlassMethods
        def [](key)
          instances[key] ||= new(key)
        end

        def exists?(mac)
          instances.key?(mac)
        end

        def instances
          @instances ||= {}
        end
      end

      def self.included(klass)
        klass.extend(KlassMethods)
        klass.class_eval do
          attr_accessor :presence, :sync_status
        end
      end

      def active?
        presence.visible_since?(self.class.current_expiration_threshold)
      end

      def data_dirty?
        sync_flag?(:dirtyAttributes) || sync_flag?(:dirtyChildren)
      end

      def diagnostic_data
        {
          sync_status: sync_status,
          presence_bit_offset: presence.current_bit_offset,
          current_presence: presence.current_presence.bits,
          last_presence: presence.last_presence.bits
        }
      end

      def dirty?
        new? || data_dirty? || status_dirty?
      end

      def initialize(*_args)
        self.presence = Presence.new
        self.sync_status = 0
      end

      def mark_synced
        flag = active? ? :syncedOnline : :syncedOffline
        self.sync_status = SYNC_FLAGS[flag]
      end

      def new?
        !(sync_flag?(:syncedOnline) || sync_flag?(:syncedOffline))
      end

      def set_sync_flag(flag)
        self.sync_status |= SYNC_FLAGS[flag]
      end

      def status_dirty?
        sync_flag?(:syncedOnline) && !active? ||
          sync_flag?(:syncedOffline) && active?
      end

      def sync_flag?(flag)
        (sync_status & SYNC_FLAGS[flag]) > 0
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
patronus_fati-1.3.8 lib/patronus_fati/data_models/common_state.rb
patronus_fati-1.3.7 lib/patronus_fati/data_models/common_state.rb
patronus_fati-1.3.6 lib/patronus_fati/data_models/common_state.rb
patronus_fati-1.3.5 lib/patronus_fati/data_models/common_state.rb
patronus_fati-1.3.4 lib/patronus_fati/data_models/common_state.rb
patronus_fati-1.3.3 lib/patronus_fati/data_models/common_state.rb
patronus_fati-1.3.2 lib/patronus_fati/data_models/common_state.rb
patronus_fati-1.3.1 lib/patronus_fati/data_models/common_state.rb
patronus_fati-1.3.0 lib/patronus_fati/data_models/common_state.rb
patronus_fati-1.2.2 lib/patronus_fati/data_models/common_state.rb
patronus_fati-1.2.1 lib/patronus_fati/data_models/common_state.rb
patronus_fati-1.2.0 lib/patronus_fati/data_models/common_state.rb