Class: CfScript::Command::Registry

Inherits:
Object
  • Object
show all
Defined in:
lib/cf_script/command/registry.rb

Constant Summary collapse

DuplicateCommand =
Class.new(StandardError)
UnimplementedCommand =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegistry

Returns a new instance of Registry.



8
9
10
# File 'lib/cf_script/command/registry.rb', line 8

def initialize
  @commands = {}
end

Instance Attribute Details

#commandsObject (readonly)

Returns the value of attribute commands.



3
4
5
# File 'lib/cf_script/command/registry.rb', line 3

def commands
  @commands
end

Instance Method Details

#[](command_name) ⇒ Object



46
47
48
# File 'lib/cf_script/command/registry.rb', line 46

def [](command_name)
  commands[command_name]
end

#add!(command_class) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/cf_script/command/registry.rb', line 22

def add!(command_class)
  instance = command_class.instance

  if known?(instance.name)
    raise DuplicateCommand.new(
      "The '#{instance.name}' command is already registered"
    )
  end

  commands[instance.name] = instance
end

#catalogObject



12
13
14
15
16
17
18
19
20
# File 'lib/cf_script/command/registry.rb', line 12

def catalog
  catalog = Hash.new { |types, type| types[type] = [] }

  commands.each do |name, command|
    catalog[command.type] << name
  end

  catalog
end

#check!(command_name) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/cf_script/command/registry.rb', line 38

def check!(command_name)
  unless known?(command_name)
    raise UnimplementedCommand.new(
      "The #{command_name} command is not implemented, yet"
    )
  end
end

#known?(command_name) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/cf_script/command/registry.rb', line 34

def known?(command_name)
  commands.key?(command_name)
end