class WebsocketRails::EventMap::Namespace

Stores route map for nested namespaces

Attributes

actions[R]
controllers[R]
name[R]
namespaces[R]
parent[R]

Public Class Methods

new(name,dispatcher,parent=nil) click to toggle source
# File lib/websocket_rails/event_map.rb, line 82
def initialize(name,dispatcher,parent=nil)
  @name        = name
  @parent      = parent
  @dispatcher  = dispatcher
  @actions     = Hash.new {|h,k| h[k] = Array.new}
  @controllers = Hash.new
  @namespaces  = Hash.new
end

Public Instance Methods

find_or_create(namespace) click to toggle source
# File lib/websocket_rails/event_map.rb, line 91
def find_or_create(namespace)
  unless child = namespaces[namespace]
    child = Namespace.new namespace, @dispatcher, self
    namespaces[namespace] = child
  end
  child
end
routes_for(event, event_namespace=nil, &block) click to toggle source

Iterates through the namespace tree and yields all controller/action pairs stored for the target event.

# File lib/websocket_rails/event_map.rb, line 108
def routes_for(event, event_namespace=nil, &block)

  # Grab the first level namespace from the namespace array
  # and remove it from the copy.
  event_namespace = copy_event_namespace( event, event_namespace ) || return
  namespace       = event_namespace.shift

  # If the namespace matches the current namespace and we are
  # at the last namespace level, yield any controller/action
  # pairs for this event.
  #
  # If the namespace does not match, search the list of child
  # namespaces stored at this level for a match and delegate
  # to it's #routes_for method, passing along the current
  # copy of the event's namespace array.
  if namespace == @name and event_namespace.empty?
    actions[event.name].each do |klass,action|
      block.call klass, action
    end
  else
    child_namespace = event_namespace.first
    child = namespaces[child_namespace]
    child.routes_for event, event_namespace, &block unless child.nil?
  end
end
store(event_name,options) click to toggle source

Stores controller/action pairs for events subscribed under this namespace.

# File lib/websocket_rails/event_map.rb, line 101
def store(event_name,options)
  klass, action = TargetValidator.validate_target options
  actions[event_name] << [klass,action]
end

Private Instance Methods

copy_event_namespace(event, namespace=nil) click to toggle source
# File lib/websocket_rails/event_map.rb, line 136
def copy_event_namespace(event, namespace=nil)
  namespace = event.namespace.dup if namespace.nil?
  namespace
end