Class: Excavator::Namespace

Inherits:
Object
  • Object
show all
Defined in:
lib/excavator/namespace.rb

Overview

Internal: Namespace is a container for other Namespaces and Commands.

Examples

ns = Namespace.new

# Add a command
ns << Command.new(:log)

# Add a namespace
ns << Namespace.new(:database)

# Lookup a command
ns.command(:log)

# Lookup a namespace
ns.namespace(:namespace)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = nil, options = {}) ⇒ Namespace

Returns a new instance of Namespace.



32
33
34
35
36
# File 'lib/excavator/namespace.rb', line 32

def initialize(name = nil, options = {})
  @name = name.to_sym if name
  @namespaces = {}
  @commands = {}
end

Instance Attribute Details

#nameObject (readonly)

Public: A String/Symbol name for the namespace. This is used to build a Command’s full name used on the command line.



27
28
29
# File 'lib/excavator/namespace.rb', line 27

def name
  @name
end

#parentObject

Internal: A pointer to the parent Namespace (if any).



30
31
32
# File 'lib/excavator/namespace.rb', line 30

def parent
  @parent
end

Instance Method Details

#<<(obj) ⇒ Object

Public: Add a Namespace or Command instance to this namespace.

When adding namespaces, it will automatically set #parent attribute on the new object.

obj - A Command or Namespace to add to the namespace.

Examples

ns = Namespace.new(:test)

# Add a namespace. The added namespace will have #parent set to 'ns'.
recent = Namespace.new(:recent)
ns << recent
recent.parent
# => ns

# Add a command.
ns << Command.new(:name)

Returns itself (Namespace).



59
60
61
62
63
64
65
66
67
68
# File 'lib/excavator/namespace.rb', line 59

def <<(obj)
  if self.class === obj
    obj.parent = self
    @namespaces[obj.name] = obj
  else
    @commands[obj.name] = obj
  end

  self
end

#command(name) ⇒ Object

Public: Look up a command within this namespace. It does not look into child namespaces.

name - A String or Symbol name of the command to look up.

Examples

ns = Namespace.new(:test)
ns << Command.new(:run)
ns.command("run")
# => <Command :run>

Returns a Command if the command exists in the namespace. Returns nil if the command does not exist.



84
85
86
# File 'lib/excavator/namespace.rb', line 84

def command(name)
  @commands[name.to_sym]
end

#commands_and_descriptionsObject

Public: An array of full command names and their description. This will include all commands in child namespaces as well.

Examples

# Setup
ns_a = Namespace.new(:a)
ns_b = Namespace.new(:b)
ns_c = Namespace.new(:c)

# A > B > C
ns_a << ns_b
ns_b << ns_c

ns_b << Command.new(:foo, :desc => "Foo Desc")
ns_c << Command.new(:bar, :desc => "Bar Desc")

ns_a.commands_and_descriptions
# => [
#   ["a:b:foo", "Foo Desc"],
#   ["a:b:c:bar", "Bar Desc"
# ]

Returns an Array of two item Arrays.



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/excavator/namespace.rb', line 169

def commands_and_descriptions
  items = []
  @commands.each do |cmd_name, cmd|
    items << [
      cmd.full_name,
      cmd.desc
    ]
  end

  @namespaces.each do |ns_name, ns|
    items.push(*ns.commands_and_descriptions)
  end

  items.sort
end

#full_name(command_name = nil) ⇒ Object

Public: The full name of the namespace. This returns a string of the namespace name and it’s ancestor’s name joined by “:”. A namespace with no parent will return an emptry String.

command_name - An optional String or Symbol name of a command to append

to the end of the namespace's full name.

Examples

ns_a = Namespace.new(:a)
ns_b = Namespace.new(:b)
ns_c = Namespace.new(:c)

ns_a << ns_b
ns_b << ns_c

ns_a.full_name
# => ""

ns_b.full_name
# => "a:b"

ns_c.full_name
# => "a:b:c"

ns_c.full_name("zebra")
# => "a:b:c:zebra"

Returns a String.



134
135
136
137
138
139
140
141
142
143
# File 'lib/excavator/namespace.rb', line 134

def full_name(command_name = nil)
  return "#{command_name}" if parent.nil?

  parts = []
  parts << parent.full_name if parent.full_name != ""
  parts << name.to_s
  parts << command_name.to_s if command_name

  parts.compact.join(":")
end

#namespace(name) ⇒ Object

Public: Look up a child namespace within this namespace.

name - A String or Symbol name of the namespace to look up.

Examples

ns = Namespace.new(:test)
ns << Namespace.new(:recent)
ns.namespace(:recent)
# => <Namespace :recent>

Returns a Namespace if it exists in the namespace. Returns nil if the namespace does not exist.



101
102
103
# File 'lib/excavator/namespace.rb', line 101

def namespace(name)
  @namespaces[name.to_sym]
end