Module: LiteCable::Connection::Identification

Included in:
Base
Defined in:
lib/lite_cable/connection/identification.rb

Overview

:nodoc:

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/lite_cable/connection/identification.rb', line 26

def self.prepended(base)
  base.class_eval do
    class << self
      attr_writer :identifiers

      def identifiers
        @identifiers ||= Set.new
      end

      include ClassMethods
    end
  end
end

Instance Method Details

#fetch_identifier(name) ⇒ Object

Fetch identifier and deserialize if neccessary



81
82
83
84
85
86
87
88
# File 'lib/lite_cable/connection/identification.rb', line 81

def fetch_identifier(name)
  val = @encoded_ids[name]
  val = LiteCable.config.identifier_coder.decode(val) unless val.nil?
  instance_variable_set(
    :"@#{name}",
    val
  )
end

#identifierObject

Return a single connection identifier that combines the value of all the registered identifiers into a single id.

You can specify a custom identifier_coder in config to implement specific logic of encoding/decoding custom classes to identifiers.

By default uses Raw coder.



57
58
59
60
61
62
63
64
# File 'lib/lite_cable/connection/identification.rb', line 57

def identifier
  unless defined? @identifier
    values = identifiers_hash.values.compact
    @identifier = values.empty? ? nil : values.map(&:to_s).sort.join(":")
  end

  @identifier
end

#identifiersObject



45
46
47
# File 'lib/lite_cable/connection/identification.rb', line 45

def identifiers
  self.class.identifiers
end

#identifiers_hashObject

Generate identifiers info as hash.



67
68
69
70
71
72
73
74
# File 'lib/lite_cable/connection/identification.rb', line 67

def identifiers_hash
  identifiers.each_with_object({}) do |id, acc|
    obj = instance_variable_get("@#{id}")
    next unless obj

    acc[id.to_s] = LiteCable.config.identifier_coder.encode(obj)
  end
end

#identifiers_jsonObject



76
77
78
# File 'lib/lite_cable/connection/identification.rb', line 76

def identifiers_json
  identifiers_hash.to_json
end

#initialize(socket, identifiers: nil, **hargs) ⇒ Object



40
41
42
43
# File 'lib/lite_cable/connection/identification.rb', line 40

def initialize(socket, identifiers: nil, **hargs)
  @encoded_ids = identifiers ? JSON.parse(identifiers) : {}
  super socket, **hargs
end