Class: Loco::Hub

Inherits:
Object
  • Object
show all
Defined in:
lib/loco/hub.rb

Constant Summary collapse

PREFIX =
'hub:'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, members = []) ⇒ Hub

Returns a new instance of Hub.



23
24
25
26
# File 'lib/loco/hub.rb', line 23

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

Class Method Details

.full_name(val) ⇒ Object



18
19
20
# File 'lib/loco/hub.rb', line 18

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

.get(name) ⇒ Object



12
13
14
15
16
# File 'lib/loco/hub.rb', line 12

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

  new(name)
end

.set(name, members) ⇒ Object



8
9
10
# File 'lib/loco/hub.rb', line 8

def set(name, members)
  new(name, members)
end

Instance Method Details

#add_member(member) ⇒ Object



36
37
38
# File 'lib/loco/hub.rb', line 36

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

#del_member(member) ⇒ Object



40
41
42
# File 'lib/loco/hub.rb', line 40

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

#destroyObject



44
45
46
47
48
# File 'lib/loco/hub.rb', line 44

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

#full_nameObject



32
33
34
# File 'lib/loco/hub.rb', line 32

def full_name
  @name
end

#include?(resource) ⇒ Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/loco/hub.rb', line 50

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

#membersObject



58
59
60
61
62
63
# File 'lib/loco/hub.rb', line 58

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

#nameObject



28
29
30
# File 'lib/loco/hub.rb', line 28

def name
  full_name.split(PREFIX).last
end

#raw_membersObject



54
55
56
# File 'lib/loco/hub.rb', line 54

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