Sha256: b8efcad70f3f4ef66bef3c7dd3e46e3774e0e9786136b90263b86657d40b2ff5

Contents?: true

Size: 1.33 KB

Versions: 20

Compression:

Stored size: 1.33 KB

Contents

module Loco
  class Hub
    PREFIX = 'loco:hub:'

    attr_reader :raw_members

    def initialize name, members = []
      @name = "#{PREFIX}#{name}"
      @raw_members = members.map{ |m| serialize m }
    end

    class << self
      def get name
        hub = WsConnectionStorage.current.get "#{PREFIX}#{name}"
        return nil if hub.blank?
        new name, JSON.parse(hub)
      end
    end

    def name
      @name.split(PREFIX).last
    end

    def add_member member
      serialized = serialize member
      return raw_members if raw_members.include? serialized
      raw_members << serialized
      save
      raw_members
    end

    def del_member member
      serialized = serialize member
      return nil if not raw_members.include? serialized
      raw_members.delete serialized
      save
      serialized
    end

    def destroy
      WsConnectionStorage.current.del @name
      true
    end

    def save
      WsConnectionStorage.current.set @name, raw_members.to_json
      self
    end

    def include? resource
      raw_members.include? serialize(resource)
    end

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

    private

      def serialize member
        WsConnectionManager.new(member).identifier
      end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
loco-rails-2.5.3 lib/loco/hub.rb
loco-rails-2.5.2 lib/loco/hub.rb
loco-rails-2.5.1 lib/loco/hub.rb
loco-rails-2.5.0 lib/loco/hub.rb
loco-rails-2.4.0 lib/loco/hub.rb
loco-rails-2.3.0 lib/loco/hub.rb
loco-rails-3.0.0 lib/loco/hub.rb
loco-rails-2.2.2 lib/loco/hub.rb
loco-rails-2.2.1 lib/loco/hub.rb
loco-rails-2.2.0 lib/loco/hub.rb
loco-rails-2.1.0 lib/loco/hub.rb
loco-rails-2.0.0 lib/loco/hub.rb
loco-rails-1.5.2 lib/loco/hub.rb
loco-rails-1.5.1 lib/loco/hub.rb
loco-rails-1.5.0 lib/loco/hub.rb
loco-rails-1.4.0 lib/loco/hub.rb
loco-rails-1.3.3 lib/loco/hub.rb
loco-rails-1.3.2 lib/loco/hub.rb
loco-rails-1.3.1 lib/loco/hub.rb
loco-rails-1.3.0 lib/loco/hub.rb