Class: CfScript::Command::Registry
- Inherits:
-
Object
- Object
- CfScript::Command::Registry
- Defined in:
- lib/cf_script/command/registry.rb
Constant Summary collapse
- DuplicateCommand =
Class.new(StandardError)
- UnimplementedCommand =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#commands ⇒ Object
readonly
Returns the value of attribute commands.
Instance Method Summary collapse
- #[](command_name) ⇒ Object
- #add!(command_class) ⇒ Object
- #catalog ⇒ Object
- #check!(command_name) ⇒ Object
-
#initialize ⇒ Registry
constructor
A new instance of Registry.
- #known?(command_name) ⇒ Boolean
Constructor Details
#initialize ⇒ Registry
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
#commands ⇒ Object (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 |
#catalog ⇒ Object
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
34 35 36 |
# File 'lib/cf_script/command/registry.rb', line 34 def known?(command_name) commands.key?(command_name) end |