Module: Capistrano::Configuration::Namespaces

Included in:
Capistrano::Configuration, Namespace
Defined in:
lib/capistrano/configuration/namespaces.rb

Defined Under Namespace

Classes: Namespace

Constant Summary collapse

DEFAULT_TASK =
:default

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#nameObject (readonly)

The name of this namespace. Defaults to nil for the top-level namespace.



15
16
17
# File 'lib/capistrano/configuration/namespaces.rb', line 15

def name
  @name
end

#namespacesObject (readonly)

The hash of namespaces defined for this namespace.



25
26
27
# File 'lib/capistrano/configuration/namespaces.rb', line 25

def namespaces
  @namespaces
end

#parentObject (readonly)

The parent namespace of this namespace. Returns nil for the top-level namespace.



19
20
21
# File 'lib/capistrano/configuration/namespaces.rb', line 19

def parent
  @parent
end

#tasksObject (readonly)

The hash of tasks defined for this namespace.



22
23
24
# File 'lib/capistrano/configuration/namespaces.rb', line 22

def tasks
  @tasks
end

Class Method Details

.included(base) ⇒ Object

:nodoc:



8
9
10
11
# File 'lib/capistrano/configuration/namespaces.rb', line 8

def self.included(base) #:nodoc:
  base.send :alias_method, :initialize_without_namespaces, :initialize
  base.send :alias_method, :initialize, :initialize_with_namespaces
end

Instance Method Details

#default_taskObject

Returns the default task for this namespace. This will be nil if the namespace is at the top-level, and will otherwise return the task named “default”. If no such task exists, nil will be returned.



143
144
145
146
# File 'lib/capistrano/configuration/namespaces.rb', line 143

def default_task
  return nil if parent.nil?
  return tasks[DEFAULT_TASK]
end

#desc(text) ⇒ Object

Describe the next task to be defined. The given text will be attached to the next task that is defined and used as its description.



44
45
46
# File 'lib/capistrano/configuration/namespaces.rb', line 44

def desc(text)
  @next_description = text
end

#find_task(name) ⇒ Object

Find the task with the given name, where name is the fully-qualified name of the task. This will search into the namespaces and return the referenced task, or nil if no such task can be found. If the name refers to a namespace, the task in that namespace named “default” will be returned instead, if one exists.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/capistrano/configuration/namespaces.rb', line 107

def find_task(name)
  parts = name.to_s.split(/:/)
  tail = parts.pop.to_sym

  ns = self
  until parts.empty?
    ns = ns.namespaces[parts.shift.to_sym]
    return nil if ns.nil?
  end

  if ns.namespaces.key?(tail)
    ns = ns.namespaces[tail]
    tail = DEFAULT_TASK
  end

  ns.tasks[tail]
end

#fully_qualified_nameObject

Returns the fully-qualified name of this namespace, or nil if the namespace is at the top-level.



37
38
39
40
# File 'lib/capistrano/configuration/namespaces.rb', line 37

def fully_qualified_name
  return nil if name.nil?
  [parent.fully_qualified_name, name].compact.join(":")
end

#namespace(name, &block) ⇒ Object

Open a namespace in which to define new tasks. If the namespace was defined previously, it will be reopened, otherwise a new namespace will be created for the given name.

Raises:

  • (ArgumentError)


59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/capistrano/configuration/namespaces.rb', line 59

def namespace(name, &block)
  name = name.to_sym
  raise ArgumentError, "expected a block" unless block_given?

  namespace_already_defined = namespaces.key?(name)
  if all_methods.include?(name.to_s) && !namespace_already_defined
    thing = tasks.key?(name) ? "task" : "method"
    raise ArgumentError, "defining a namespace named `#{name}' would shadow an existing #{thing} with that name"
  end

  namespaces[name] ||= Namespace.new(name, self)
  namespaces[name].instance_eval(&block)

  # make sure any open description gets terminated
  namespaces[name].desc(nil)

  if !namespace_already_defined
    metaclass = class << self; self; end
    metaclass.send(:define_method, name) { namespaces[name] }
  end
end

#next_description(reset = false) ⇒ Object

Returns the value set by the last, pending “desc” call. If reset is not false, the value will be reset immediately afterwards.



50
51
52
53
54
# File 'lib/capistrano/configuration/namespaces.rb', line 50

def next_description(reset=false)
  @next_description
ensure
  @next_description = nil if reset
end

#search_task(name) ⇒ Object

Given a task name, this will search the current namespace, and all parent namespaces, looking for a task that matches the name, exactly. It returns the task, if found, or nil, if not.



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/capistrano/configuration/namespaces.rb', line 128

def search_task(name)
  name = name.to_sym
  ns = self

  until ns.nil?
    return ns.tasks[name] if ns.tasks.key?(name)
    ns = ns.parent
  end

  return nil
end

#task(name, options = {}, &block) ⇒ Object

Describe a new task. If a description is active (see #desc), it is added to the options under the :desc key. The new task is added to the namespace.

Raises:

  • (ArgumentError)


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/capistrano/configuration/namespaces.rb', line 84

def task(name, options={}, &block)
  name = name.to_sym
  raise ArgumentError, "expected a block" unless block_given?

  task_already_defined = tasks.key?(name)
  if all_methods.include?(name.to_s) && !task_already_defined
    thing = namespaces.key?(name) ? "namespace" : "method"
    raise ArgumentError, "defining a task named `#{name}' would shadow an existing #{thing} with that name"
  end

  tasks[name] = TaskDefinition.new(name, self, {:desc => next_description(:reset)}.merge(options), &block)

  if !task_already_defined
    metaclass = class << self; self; end
    metaclass.send(:define_method, name) { execute_task(tasks[name]) }
  end
end

#task_list(all = false) ⇒ Object

Returns the tasks in this namespace as an array of TaskDefinition objects. If a non-false parameter is given, all tasks in all namespaces under this namespace will be returned as well.



151
152
153
154
155
# File 'lib/capistrano/configuration/namespaces.rb', line 151

def task_list(all=false)
  list = tasks.values
  namespaces.each { |name,space| list.concat(space.task_list(:all)) } if all
  list
end