Class: Gitlab::QuickActions::CommandDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/quick_actions/command_definition.rb

Direct Known Subclasses

SubstitutionDefinition

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, attributes = {}) ⇒ CommandDefinition

Returns a new instance of CommandDefinition.


9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/gitlab/quick_actions/command_definition.rb', line 9

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_blockObject

Returns the value of attribute action_block


6
7
8
# File 'lib/gitlab/quick_actions/command_definition.rb', line 6

def action_block
  @action_block
end

#aliasesObject

Returns the value of attribute aliases


6
7
8
# File 'lib/gitlab/quick_actions/command_definition.rb', line 6

def aliases
  @aliases
end

#condition_blockObject

Returns the value of attribute condition_block


6
7
8
# File 'lib/gitlab/quick_actions/command_definition.rb', line 6

def condition_block
  @condition_block
end

#descriptionObject

Returns the value of attribute description


6
7
8
# File 'lib/gitlab/quick_actions/command_definition.rb', line 6

def description
  @description
end

#execution_messageObject

Returns the value of attribute execution_message


6
7
8
# File 'lib/gitlab/quick_actions/command_definition.rb', line 6

def execution_message
  @execution_message
end

#explanationObject

Returns the value of attribute explanation


6
7
8
# File 'lib/gitlab/quick_actions/command_definition.rb', line 6

def explanation
  @explanation
end

#iconObject

Returns the value of attribute icon


6
7
8
# File 'lib/gitlab/quick_actions/command_definition.rb', line 6

def icon
  @icon
end

#nameObject

Returns the value of attribute name


6
7
8
# File 'lib/gitlab/quick_actions/command_definition.rb', line 6

def name
  @name
end

#paramsObject

Returns the value of attribute params


6
7
8
# File 'lib/gitlab/quick_actions/command_definition.rb', line 6

def params
  @params
end

#parse_params_blockObject

Returns the value of attribute parse_params_block


6
7
8
# File 'lib/gitlab/quick_actions/command_definition.rb', line 6

def parse_params_block
  @parse_params_block
end

#typesObject

Returns the value of attribute types


6
7
8
# File 'lib/gitlab/quick_actions/command_definition.rb', line 6

def types
  @types
end

#warningObject

Returns the value of attribute warning


6
7
8
# File 'lib/gitlab/quick_actions/command_definition.rb', line 6

def warning
  @warning
end

Instance Method Details

#all_namesObject


25
26
27
# File 'lib/gitlab/quick_actions/command_definition.rb', line 25

def all_names
  [name, *aliases]
end

#available?(context) ⇒ Boolean

Returns:

  • (Boolean)

33
34
35
36
37
38
# File 'lib/gitlab/quick_actions/command_definition.rb', line 33

def available?(context)
  return false unless valid_type?(context)
  return true unless condition_block

  context.instance_exec(&condition_block)
end

#execute(context, arg) ⇒ Object


58
59
60
61
62
63
64
# File 'lib/gitlab/quick_actions/command_definition.rb', line 58

def execute(context, arg)
  return unless executable?(context)

  count_commands_executed_in(context)

  execute_block(action_block, context, arg)
end

#execute_message(context, arg) ⇒ Object


66
67
68
69
70
71
72
73
74
# File 'lib/gitlab/quick_actions/command_definition.rb', line 66

def execute_message(context, arg)
  return unless executable?(context)

  if execution_message.respond_to?(:call)
    execute_block(execution_message, context, arg)
  else
    execution_message
  end
end

#explain(context, arg) ⇒ Object


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/gitlab/quick_actions/command_definition.rb', line 40

def explain(context, arg)
  return unless available?(context)

  message = if explanation.respond_to?(:call)
              execute_block(explanation, context, arg)
            else
              explanation
            end

  warning_text = if warning.respond_to?(:call)
                   execute_block(warning, context, arg)
                 else
                   warning
                 end

  warning.empty? ? message : "#{message} (#{warning_text})"
end

#noop?Boolean

Returns:

  • (Boolean)

29
30
31
# File 'lib/gitlab/quick_actions/command_definition.rb', line 29

def noop?
  action_block.nil?
end

#to_h(context) ⇒ Object


76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/gitlab/quick_actions/command_definition.rb', line 76

def to_h(context)
  desc = description
  if desc.respond_to?(:call)
    desc = context.instance_exec(&desc) rescue ''
  end

  warn = warning
  if warn.respond_to?(:call)
    warn = context.instance_exec(&warn) rescue ''
  end

  prms = params
  if prms.respond_to?(:call)
    prms = Array(context.instance_exec(&prms)) rescue params
  end

  {
    name: name,
    aliases: aliases,
    description: desc,
    warning: warn,
    icon: icon,
    params: prms
  }
end