Class: Theatre::ActorNamespaceManager

Inherits:
Object
  • Object
show all
Defined in:
lib/theatre/namespace_manager.rb

Overview

Manages the hierarchial namespaces of a Theatre. This class is Thread-safe.

Defined Under Namespace

Classes: NamespaceNode, RootNamespaceNode

Constant Summary collapse

VALID_NAMESPACE =
%r{^(/[\w_]+)+$}

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeActorNamespaceManager

Returns a new instance of ActorNamespaceManager.



29
30
31
32
# File 'lib/theatre/namespace_manager.rb', line 29

def initialize
  @registry_lock = Mutex.new
  @root          = RootNamespaceNode.new
end

Class Method Details

.normalize_path_to_array(paths) ⇒ Object

Since there are a couple ways to represent namespaces, this is a helper method which will normalize them into the most practical: an Array of Symbols

Parameters:

  • paths (String, Array)

    The namespace to register. Can be in “/foo/bar” or *[foo,bar] format



19
20
21
22
23
24
25
# File 'lib/theatre/namespace_manager.rb', line 19

def normalize_path_to_array(paths)
  paths = paths.is_a?(Array) ? paths.flatten : Array(paths)
  paths.map! { |path_segment| path_segment.kind_of?(String) ? path_segment.split('/') : path_segment }
  paths.flatten!
  paths.reject! { |path| path.nil? || (path.kind_of?(String) && path.empty?) }
  paths.map { |path| path.to_sym }
end

.valid_namespace_path?(namespace_path) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/theatre/namespace_manager.rb', line 11

def valid_namespace_path?(namespace_path)
  namespace_path =~ VALID_NAMESPACE
end

Instance Method Details

#callbacks_for_namespaces(*paths) ⇒ Object

Returns a Proc found after searching with the namespace you provide

Raises:

  • NamespaceNotFound if a segment has not been registered yet



56
57
58
# File 'lib/theatre/namespace_manager.rb', line 56

def callbacks_for_namespaces(*paths)
  search_for_namespace(paths).callbacks
end

#register_callback_at_namespace(paths, callback) ⇒ Object

Registers the given callback at a namespace, assuming the namespace was already registered.

Parameters:

  • paths (Array)

    Must be an Array of segments

  • callback (Proc)

Raises:

  • NamespaceNotFound if a segment has not been registered yet



86
87
88
89
# File 'lib/theatre/namespace_manager.rb', line 86

def register_callback_at_namespace(paths, callback)
  raise ArgumentError, "callback must be a Proc" unless callback.kind_of? Proc
  search_for_namespace(paths).register_callback callback
end

#register_namespace_name(*paths) ⇒ NamespaceNode

Have this registry recognize a new path and prepare it for callback registrations. All path segements will be created in order. For example, when registering “/foo/bar/qaz” when no namespaces at all have been registered, this method will first register “foo”, then “bar”, then “qaz”. If the namespace was already registered, it will not be affected.

Parameters:

  • paths (String, Array)

    The namespace to register. Can be in “/foo/bar” or *[foo,bar] format

Returns:

  • (NamespaceNode)

    The NamespaceNode representing the path given.

Raises:

  • NamespaceNotFound if a segment has not been registered yet



43
44
45
46
47
48
49
# File 'lib/theatre/namespace_manager.rb', line 43

def register_namespace_name(*paths)
  paths = self.class.normalize_path_to_array paths

  paths.inject(@root) do |node, name|
    node.register_namespace_name name
  end
end

#search_for_namespace(paths) ⇒ Object

Find a namespace in the tree.

Parameters:

  • paths (Array, String)

    Must be an Array of segments or a name like “/foo/bar/qaz”

Raises:

  • NamespaceNotFound if a segment has not been registered yet



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/theatre/namespace_manager.rb', line 66

def search_for_namespace(paths)
  paths = self.class.normalize_path_to_array paths
  path_string = "/"

  found_namespace = paths.inject(@root) do |last_node,this_node_name|
    raise NamespaceNotFound.new(path_string) if last_node.nil?
    path_string << this_node_name.to_s
    last_node.child_named this_node_name
  end
  raise NamespaceNotFound.new("/#{paths.join('/')}") unless found_namespace
  found_namespace
end