Class: Command
- Inherits:
-
Gloo::Core::Obj
- Object
- Gloo::Core::Obj
- Command
- Defined in:
- lib/command.rb
Overview
- Author
-
Eric Crane (eric.crane@mac.com)
- Copyright
-
Copyright © 2026 Eric Crane. All rights reserved.
A CLI command.
Constant Summary collapse
- KEYWORD =
'command'.freeze
- KEYWORD_SHORT =
'command'.freeze
- NAME =
'name'.freeze
- DESCRIPTION =
'description'.freeze
- ACTION =
'action'.freeze
- DYNAMIC =
'dynamic'.freeze
- NODES =
'nodes'.freeze
- CONTEXT =
'context'.freeze
- OPTIONS =
'options'.freeze
- OPTIONS_KEY =
'options_key'.freeze
- ON_ERROR =
'on_error'.freeze
Class Method Summary collapse
-
.messages ⇒ Object
Get a list of message names that this object receives.
-
.short_typename ⇒ Object
The short name of the object type.
-
.typename ⇒ Object
The name of the object type.
Instance Method Summary collapse
-
#add_children_on_create? ⇒ Boolean
Does this object have children to add when an object is created in interactive mode? This does not apply during obj load, etc.
-
#add_default_children ⇒ Object
Add children to this object.
- #context ⇒ Object
-
#description ⇒ Object
Get the description of the command.
-
#dynamic? ⇒ Boolean
Is this a dynamic command? It is dynamic if there is a dynamic child.
-
#dynamic_key ⇒ Object
Get the dynamic key for this command.
-
#get_child_nodes ⇒ Object
Get the child nodes for this command.
-
#get_command_data ⇒ Object
Get the command data for this command.
-
#msg_register ⇒ Object
Register the command with the shell.
-
#nodes ⇒ Object
Get the child nodes for this command.
-
#options ⇒ Object
Get the options for this command.
-
#options_key ⇒ Object
Get the options key for this command.
-
#run_action ⇒ Object
Run the action script.
-
#run_action_with_context(context) ⇒ Object
Run the action script with context.
-
#run_on_error ⇒ Object
Run the on_error script if one exists.
Class Method Details
.messages ⇒ Object
Get a list of message names that this object receives.
169 170 171 |
# File 'lib/command.rb', line 169 def self. return super + [ 'register' ] end |
.short_typename ⇒ Object
The short name of the object type.
32 33 34 |
# File 'lib/command.rb', line 32 def self.short_typename return KEYWORD_SHORT end |
.typename ⇒ Object
The name of the object type.
25 26 27 |
# File 'lib/command.rb', line 25 def self.typename return KEYWORD end |
Instance Method Details
#add_children_on_create? ⇒ Boolean
Does this object have children to add when an object is created in interactive mode? This does not apply during obj load, etc.
145 146 147 |
# File 'lib/command.rb', line 145 def add_children_on_create? return true end |
#add_default_children ⇒ Object
Add children to this object. This is used by containers to add children needed for default configurations.
154 155 156 157 158 159 |
# File 'lib/command.rb', line 154 def add_default_children fac = @engine.factory fac.create_string NAME, '', self fac.create_string DESCRIPTION, '', self fac.create_script ACTION, '', self end |
#context ⇒ Object
65 66 67 68 |
# File 'lib/command.rb', line 65 def context o = find_child CONTEXT return o ? o : nil end |
#description ⇒ Object
Get the description of the command.
39 40 41 42 43 44 |
# File 'lib/command.rb', line 39 def description o = find_child DESCRIPTION return '' unless o return o.value end |
#dynamic? ⇒ Boolean
Is this a dynamic command? It is dynamic if there is a dynamic child. If so, it has contextual data.
51 52 53 54 55 |
# File 'lib/command.rb', line 51 def dynamic? o = find_child DYNAMIC return true if o return false end |
#dynamic_key ⇒ Object
Get the dynamic key for this command.
60 61 62 63 |
# File 'lib/command.rb', line 60 def dynamic_key o = find_child DYNAMIC return o ? o.value : nil end |
#get_child_nodes ⇒ Object
Get the child nodes for this command.
197 198 199 200 201 202 203 |
# File 'lib/command.rb', line 197 def get_child_nodes data = nodes.children.map do |child| child.get_command_data end return data end |
#get_command_data ⇒ Object
Get the command data for this command.
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 |
# File 'lib/command.rb', line 208 def get_command_data if dynamic? return { name: name, description: description, dynamic: true, obj: pn, method: "cmd_obj_action_with_context", source: dynamic_key } elsif nodes return { name: name, description: description, children: get_child_nodes } else return { name: name, description: description, method: "cmd_obj_action", obj: pn } end end |
#msg_register ⇒ Object
Register the command with the shell.
176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/command.rb', line 176 def msg_register if @params&.token_count&.positive? pn = Gloo::Core::Pn.new( @engine, @params.first ) shell = pn.resolve shell.add_command( self, get_command_data ) # Are there options to add? if shell.set_context( , ) end end end |
#nodes ⇒ Object
Get the child nodes for this command.
95 96 97 98 |
# File 'lib/command.rb', line 95 def nodes o = find_child NODES return o ? o : nil end |
#options ⇒ Object
Get the options for this command.
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/command.rb', line 81 def arr = [] o = find_child OPTIONS if o arr = o.children.map { |child| child.value } end return arr end |
#options_key ⇒ Object
Get the options key for this command.
73 74 75 76 |
# File 'lib/command.rb', line 73 def o = find_child OPTIONS_KEY return o ? o.value : nil end |
#run_action ⇒ Object
Run the action script.
103 104 105 106 107 108 |
# File 'lib/command.rb', line 103 def run_action o = find_child ACTION return unless o Gloo::Exec::Dispatch.( @engine, 'run', o ) end |
#run_action_with_context(context) ⇒ Object
Run the action script with context.
113 114 115 116 117 118 119 120 121 |
# File 'lib/command.rb', line 113 def run_action_with_context( context ) o = find_child ACTION return unless o ctx = find_child CONTEXT ctx.set_value( context ) if ctx Gloo::Exec::Dispatch.( @engine, 'run', o ) end |
#run_on_error ⇒ Object
Run the on_error script if one exists. Returns true if the script was found and run, false otherwise.
127 128 129 130 131 132 133 |
# File 'lib/command.rb', line 127 def run_on_error o = find_child ON_ERROR return false unless o Gloo::Exec::Dispatch.( @engine, 'run', o ) return true end |