Sha256: 720631717fc6416d89169cb969c1599e96014cf0b50c624435cd97536c893ec9

Contents?: true

Size: 1.33 KB

Versions: 4

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module Loco
  class Hub
    PREFIX = 'hub:'

    class << self
      def set(name, members)
        new(name, members)
      end

      def get(name)
        return nil if WsConnectionStorage.current.type("s:#{full_name(name)}") != 'set'

        new(name)
      end

      def full_name(val)
        "#{PREFIX}#{val}"
      end
    end

    def initialize(name, members = [])
      @name = self.class.full_name(name)
      members.map { |member| add_member(member) }
    end

    def name
      full_name.split(PREFIX).last
    end

    def full_name
      @name
    end

    def add_member(member)
      WsConnectionStorage.current.add(@name, WsConnectionIdentifier.(member))
    end

    def del_member(member)
      WsConnectionStorage.current.rem(@name, WsConnectionIdentifier.(member))
    end

    def destroy
      WsConnectionStorage.current.members(@name).each do |member|
        WsConnectionStorage.current.rem(@name, member)
      end
    end

    def include?(resource)
      WsConnectionStorage.current.member?(@name, WsConnectionIdentifier.(resource))
    end

    def raw_members
      WsConnectionStorage.current.members(@name)
    end

    def members
      raw_members.map do |serialized|
        klass, id = serialized.split(':')
        klass.classify.constantize.find_by(id: id)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
loco-rails-6.2.0 lib/loco/hub.rb
loco-rails-6.1.0 lib/loco/hub.rb
loco-rails-6.0.0 lib/loco/hub.rb
loco-rails-5.0.0 lib/loco/hub.rb