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
-
#fetch_identifier(name) ⇒ Object
Fetch identifier and deserialize if neccessary.
-
#identifier ⇒ Object
Return a single connection identifier that combines the value of all the registered identifiers into a single id.
- #identifiers ⇒ Object
-
#identifiers_hash ⇒ Object
Generate identifiers info as hash.
- #identifiers_json ⇒ Object
- #initialize(socket, identifiers: nil, **hargs) ⇒ Object
Class Method Details
.prepended(base) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/lite_cable/connection/identification.rb', line 24 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
78 79 80 81 82 83 84 85 |
# File 'lib/lite_cable/connection/identification.rb', line 78 def fetch_identifier(name) val = @encoded_ids[name] val = LiteCable.config.identifier_coder.decode(val) unless val.nil? instance_variable_set( :"@#{name}", val ) end |
#identifier ⇒ Object
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.
55 56 57 58 59 60 61 62 |
# File 'lib/lite_cable/connection/identification.rb', line 55 def identifier unless defined? @identifier values = identifiers_hash.values.compact @identifier = values.empty? ? nil : values.map(&:to_s).sort.join(":") end @identifier end |
#identifiers ⇒ Object
43 44 45 |
# File 'lib/lite_cable/connection/identification.rb', line 43 def identifiers self.class.identifiers end |
#identifiers_hash ⇒ Object
Generate identifiers info as hash.
65 66 67 68 69 70 71 |
# File 'lib/lite_cable/connection/identification.rb', line 65 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_json ⇒ Object
73 74 75 |
# File 'lib/lite_cable/connection/identification.rb', line 73 def identifiers_json identifiers_hash.to_json end |
#initialize(socket, identifiers: nil, **hargs) ⇒ Object
38 39 40 41 |
# File 'lib/lite_cable/connection/identification.rb', line 38 def initialize(socket, identifiers: nil, **hargs) @encoded_ids = identifiers ? JSON.parse(identifiers) : {} super socket, **hargs end |