module AnyCable::Rails

Rails handler for AnyCable

Constants

ADAPTER_ALIASES
VERSION

Public Class Methods

compatible_adapter?(adapter) click to toggle source
# File lib/anycable/rails.rb, line 23
def compatible_adapter?(adapter)
  ADAPTER_ALIASES.include?(adapter)
end
deserialize(str, json: false) click to toggle source

Deserialize previously serialized value from string to Ruby object. If the resulting object is a Hash, make it indifferent

# File lib/anycable/rails.rb, line 36
def deserialize(str, json: false)
  str.yield_self do |val|
    next val unless val.is_a?(String)

    gval = GlobalID::Locator.locate(val)
    return gval if gval

    next val unless json

    JSON.parse(val)
  end.yield_self do |val|
    next val.with_indifferent_access if val.is_a?(Hash)
    val
  end
end
enabled?() click to toggle source
# File lib/anycable/rails.rb, line 18
def enabled?
  adapter = ::ActionCable.server.config.cable&.fetch("adapter", nil)
  compatible_adapter?(adapter)
end
serialize(obj, json: false) click to toggle source

Serialize connection/channel state variable to string using GlobalID where possible or JSON (if json: true)

# File lib/anycable/rails.rb, line 29
def serialize(obj, json: false)
  obj.try(:to_gid_param) || (json ? obj.to_json : obj)
end