Sha256: a53ec91ffbc8bd31ce71f994166ad772127e9e6876091d08ec1dd1ef8473fa1d

Contents?: true

Size: 1.37 KB

Versions: 6

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

module Loco
  class WsConnectedResourcesManager
    def initialize resources
      @resources = resources
      @connected_resources = nil
    end

    class << self
      def identifiers
        val = WsConnectionStorage.current.get key
        return [] if val.blank?
        JSON.parse val
      end

      def add identifier
        ids = identifiers
        return if ids.include? identifier
        ids << identifier
        WsConnectionStorage.current.set key, ids.to_json
      end

      def del identifier
        ids = identifiers
        return unless ids.include? identifier
        ids.delete identifier
        WsConnectionStorage.current.set key, ids.to_json
      end

      def key
        'loco:conn_ids'
      end
    end

    def connected_resources
      return @connected_resources if @connected_resources
      @resources.each do |resource|
        next if WsConnectionManager.new(resource).connected_uuids.empty?
        add resource
      end
      @connected_resources || []
    end

    def connected? resource
      connected_resources.map do |res|
        WsConnectionManager.new(res).identifier
      end.include? WsConnectionManager.new(resource).identifier
    end

    private

      def add resource
        @connected_resources ||= []
        @connected_resources << resource
        @connected_resources.uniq!
      end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
loco-rails-3.0.6 lib/loco/ws_connected_resources_manager.rb
loco-rails-3.0.5 lib/loco/ws_connected_resources_manager.rb
loco-rails-3.0.4 lib/loco/ws_connected_resources_manager.rb
loco-rails-3.0.3 lib/loco/ws_connected_resources_manager.rb
loco-rails-3.0.2 lib/loco/ws_connected_resources_manager.rb
loco-rails-3.0.1 lib/loco/ws_connected_resources_manager.rb