Class: Gitlab::QuickActions::CommandDefinition
- Inherits:
-
Object
- Object
- Gitlab::QuickActions::CommandDefinition
- Defined in:
- lib/gitlab/quick_actions/command_definition.rb
Direct Known Subclasses
Constant Summary collapse
- ParseError =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#action_block ⇒ Object
Returns the value of attribute action_block.
-
#aliases ⇒ Object
Returns the value of attribute aliases.
-
#condition_block ⇒ Object
Returns the value of attribute condition_block.
-
#description ⇒ Object
Returns the value of attribute description.
-
#execution_message ⇒ Object
Returns the value of attribute execution_message.
-
#explanation ⇒ Object
Returns the value of attribute explanation.
-
#icon ⇒ Object
Returns the value of attribute icon.
-
#name ⇒ Object
Returns the value of attribute name.
-
#params ⇒ Object
Returns the value of attribute params.
-
#parse_params_block ⇒ Object
Returns the value of attribute parse_params_block.
-
#types ⇒ Object
Returns the value of attribute types.
-
#warning ⇒ Object
Returns the value of attribute warning.
Instance Method Summary collapse
- #all_names ⇒ Object
- #available?(context) ⇒ Boolean
- #execute(context, arg) ⇒ Object
- #execute_message(context, arg) ⇒ Object
- #explain(context, arg) ⇒ Object
-
#initialize(name, attributes = {}) ⇒ CommandDefinition
constructor
A new instance of CommandDefinition.
- #noop? ⇒ Boolean
- #to_h(context) ⇒ Object
Constructor Details
#initialize(name, attributes = {}) ⇒ CommandDefinition
Returns a new instance of CommandDefinition.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/gitlab/quick_actions/command_definition.rb', line 11 def initialize(name, attributes = {}) @name = name @aliases = attributes[:aliases] || [] @description = attributes[:description] || '' @warning = attributes[:warning] || '' @icon = attributes[:icon] || '' @explanation = attributes[:explanation] || '' @execution_message = attributes[:execution_message] || '' @params = attributes[:params] || [] @condition_block = attributes[:condition_block] @parse_params_block = attributes[:parse_params_block] @action_block = attributes[:action_block] @types = attributes[:types] || [] end |
Instance Attribute Details
#action_block ⇒ Object
Returns the value of attribute action_block.
8 9 10 |
# File 'lib/gitlab/quick_actions/command_definition.rb', line 8 def action_block @action_block end |
#aliases ⇒ Object
Returns the value of attribute aliases.
8 9 10 |
# File 'lib/gitlab/quick_actions/command_definition.rb', line 8 def aliases @aliases end |
#condition_block ⇒ Object
Returns the value of attribute condition_block.
8 9 10 |
# File 'lib/gitlab/quick_actions/command_definition.rb', line 8 def condition_block @condition_block end |
#description ⇒ Object
Returns the value of attribute description.
8 9 10 |
# File 'lib/gitlab/quick_actions/command_definition.rb', line 8 def description @description end |
#execution_message ⇒ Object
Returns the value of attribute execution_message.
8 9 10 |
# File 'lib/gitlab/quick_actions/command_definition.rb', line 8 def @execution_message end |
#explanation ⇒ Object
Returns the value of attribute explanation.
8 9 10 |
# File 'lib/gitlab/quick_actions/command_definition.rb', line 8 def explanation @explanation end |
#icon ⇒ Object
Returns the value of attribute icon.
8 9 10 |
# File 'lib/gitlab/quick_actions/command_definition.rb', line 8 def icon @icon end |
#name ⇒ Object
Returns the value of attribute name.
8 9 10 |
# File 'lib/gitlab/quick_actions/command_definition.rb', line 8 def name @name end |
#params ⇒ Object
Returns the value of attribute params.
8 9 10 |
# File 'lib/gitlab/quick_actions/command_definition.rb', line 8 def params @params end |
#parse_params_block ⇒ Object
Returns the value of attribute parse_params_block.
8 9 10 |
# File 'lib/gitlab/quick_actions/command_definition.rb', line 8 def parse_params_block @parse_params_block end |
#types ⇒ Object
Returns the value of attribute types.
8 9 10 |
# File 'lib/gitlab/quick_actions/command_definition.rb', line 8 def types @types end |
#warning ⇒ Object
Returns the value of attribute warning.
8 9 10 |
# File 'lib/gitlab/quick_actions/command_definition.rb', line 8 def warning @warning end |
Instance Method Details
#all_names ⇒ Object
27 28 29 |
# File 'lib/gitlab/quick_actions/command_definition.rb', line 27 def all_names [name, *aliases] end |
#available?(context) ⇒ Boolean
35 36 37 38 39 40 |
# File 'lib/gitlab/quick_actions/command_definition.rb', line 35 def available?(context) return false unless valid_type?(context) return true unless condition_block context.instance_exec(&condition_block) end |
#execute(context, arg) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/gitlab/quick_actions/command_definition.rb', line 64 def execute(context, arg) return if noop? count_commands_executed_in(context) return unless available?(context) execute_block(action_block, context, arg) rescue ParseError # message propagation is handled in `execution_message`. end |
#execute_message(context, arg) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/gitlab/quick_actions/command_definition.rb', line 76 def (context, arg) return if noop? return _('Could not apply %{name} command.') % { name: name } unless available?(context) if .respond_to?(:call) execute_block(, context, arg) else end rescue ParseError => e format _('Could not apply %{name} command. %{message}.'), name: name, message: e. end |
#explain(context, arg) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/gitlab/quick_actions/command_definition.rb', line 42 def explain(context, arg) return unless available?(context) = if explanation.respond_to?(:call) begin execute_block(explanation, context, arg) rescue ParseError => e format(_('Problem with %{name} command: %{message}.'), name: name, message: e.) end else explanation end warning_text = if warning.respond_to?(:call) execute_block(warning, context, arg) else warning end warning.empty? ? : "#{} (#{warning_text})" end |
#noop? ⇒ Boolean
31 32 33 |
# File 'lib/gitlab/quick_actions/command_definition.rb', line 31 def noop? action_block.nil? end |
#to_h(context) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/gitlab/quick_actions/command_definition.rb', line 89 def to_h(context) desc = description if desc.respond_to?(:call) desc = begin context.instance_exec(&desc) rescue StandardError '' end end warn = warning if warn.respond_to?(:call) warn = begin context.instance_exec(&warn) rescue StandardError '' end end prms = params if prms.respond_to?(:call) prms = begin Array(context.instance_exec(&prms)) rescue StandardError params end end { name: name, aliases: aliases, description: desc, warning: warn, icon: icon, params: prms } end |