Class: HammerCLI::CommandExtensions

Inherits:
Object
  • Object
show all
Defined in:
lib/hammer_cli/command_extensions.rb

Constant Summary collapse

ALLOWED_EXTENSIONS =
%i[
  option command_options before_print data output help request
  request_headers headers request_options options request_params params
  option_sources predefined_options use_option
].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CommandExtensions

Returns a new instance of CommandExtensions.



21
22
23
24
25
26
27
# File 'lib/hammer_cli/command_extensions.rb', line 21

def initialize(options = {})
  @only = options[:only] || ALLOWED_EXTENSIONS
  @only = [@only] unless @only.is_a?(Array)
  @except = options[:except] || []
  @except = [@except] unless @except.is_a?(Array)
  @inheritable = options[:inheritable]
end

Class Attribute Details

.delegateeObject

Returns the value of attribute delegatee.



4
5
6
# File 'lib/hammer_cli/command_extensions.rb', line 4

def delegatee
  @delegatee
end

Class Method Details

.before_print(&block) ⇒ Object



61
62
63
# File 'lib/hammer_cli/command_extensions.rb', line 61

def self.before_print(&block)
  @before_print_block = block
end

.extend_before_print(data) ⇒ Object



189
190
191
192
193
194
# File 'lib/hammer_cli/command_extensions.rb', line 189

def self.extend_before_print(data)
  return if @before_print_block.nil?

  @before_print_block.call(data)
  logger.debug("Called block for #{@delegatee} data:\n\t#{@before_print_block}")
end

.extend_help(command_class) ⇒ Object



203
204
205
206
207
208
# File 'lib/hammer_cli/command_extensions.rb', line 203

def self.extend_help(command_class)
  return if @help_extension_block.nil?

  command_class.help_extension_blocks << @help_extension_block
  logger.debug("Saved block for #{@delegatee} help definition:\n\t#{@help_extension_block}")
end

.extend_option_sources(sources, command = nil) ⇒ Object



231
232
233
234
235
236
# File 'lib/hammer_cli/command_extensions.rb', line 231

def self.extend_option_sources(sources, command = nil)
  return if @option_sources_block.nil?

  @option_sources_block.call(sources, command)
  logger.debug("Called block for #{@delegatee} option sources:\n\t#{@option_sources_block}")
end

.extend_options(command_class) ⇒ Object

Class



170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/hammer_cli/command_extensions.rb', line 170

def self.extend_options(command_class)
  return if @options.nil?

  @options.each do |option|
    command_class.send(:option,
                       option[:switches],
                       option[:type],
                       option[:description],
                       option[:opts],
                       &option[:block])
    logger.debug("Added option for #{command_class}: #{option}")
  end
end

.extend_output(command_class) ⇒ Object



196
197
198
199
200
201
# File 'lib/hammer_cli/command_extensions.rb', line 196

def self.extend_output(command_class)
  return if @output_extension_block.nil?

  @output_extension_block.call(command_class.output_definition)
  logger.debug("Called block for #{@delegatee} output definition:\n\t#{@output_extension_block}")
end

.extend_predefined_options(command_class) ⇒ Object



184
185
186
187
# File 'lib/hammer_cli/command_extensions.rb', line 184

def self.extend_predefined_options(command_class)
  command_class.send(:use_option, *@predefined_option_names)
  logger.debug("Added predefined options for #{command_class}: #{@predefined_option_names}")
end

.extend_request_headers(headers) ⇒ Object



210
211
212
213
214
215
# File 'lib/hammer_cli/command_extensions.rb', line 210

def self.extend_request_headers(headers)
  return if @request_headers_block.nil?

  @request_headers_block.call(headers)
  logger.debug("Called block for #{@delegatee} request headers:\n\t#{@request_headers_block}")
end

.extend_request_options(options) ⇒ Object



217
218
219
220
221
222
# File 'lib/hammer_cli/command_extensions.rb', line 217

def self.extend_request_options(options)
  return if @request_options_block.nil?

  @request_options_block.call(options)
  logger.debug("Called block for #{@delegatee} request options:\n\t#{@request_options_block}")
end

.extend_request_params(params) ⇒ Object



224
225
226
227
228
229
# File 'lib/hammer_cli/command_extensions.rb', line 224

def self.extend_request_params(params)
  return if @request_params_block.nil?

  @request_params_block.call(params)
  logger.debug("Called block for #{@delegatee} request params:\n\t#{@request_params_block}")
end

.help(&block) ⇒ Object



69
70
71
# File 'lib/hammer_cli/command_extensions.rb', line 69

def self.help(&block)
  @help_extension_block = block
end

.inheritable(boolean) ⇒ Object

DSL



45
46
47
# File 'lib/hammer_cli/command_extensions.rb', line 45

def self.inheritable(boolean)
  @inheritable = boolean
end

.inheritable?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/hammer_cli/command_extensions.rb', line 10

def inheritable?
  @inheritable
end

.loggerObject



6
7
8
# File 'lib/hammer_cli/command_extensions.rb', line 6

def logger
  Logging.logger[to_s]
end

.method_missing(message, *args, &block) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/hammer_cli/command_extensions.rb', line 35

def self.method_missing(message, *args, &block)
  if @delegatee
    @delegatee.send(message, *args, &block)
  else
    super
  end
end

.option(switches, type, description, opts = {}, &block) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/hammer_cli/command_extensions.rb', line 49

def self.option(switches, type, description, opts = {}, &block)
  @options ||= []
  @options << { switches: switches,
                type: type,
                description: description,
                opts: opts, block: block }
end

.option_sources(&block) ⇒ Object



85
86
87
# File 'lib/hammer_cli/command_extensions.rb', line 85

def self.option_sources(&block)
  @option_sources_block = block
end

.output(&block) ⇒ Object



65
66
67
# File 'lib/hammer_cli/command_extensions.rb', line 65

def self.output(&block)
  @output_extension_block = block
end

.request_headers(&block) ⇒ Object



73
74
75
# File 'lib/hammer_cli/command_extensions.rb', line 73

def self.request_headers(&block)
  @request_headers_block = block
end

.request_options(&block) ⇒ Object



77
78
79
# File 'lib/hammer_cli/command_extensions.rb', line 77

def self.request_options(&block)
  @request_options_block = block
end

.request_params(&block) ⇒ Object



81
82
83
# File 'lib/hammer_cli/command_extensions.rb', line 81

def self.request_params(&block)
  @request_params_block = block
end

.use_option(*names) ⇒ Object



57
58
59
# File 'lib/hammer_cli/command_extensions.rb', line 57

def self.use_option(*names)
  @predefined_option_names = names
end

Instance Method Details

#delegatee(command_class) ⇒ Object



154
155
156
# File 'lib/hammer_cli/command_extensions.rb', line 154

def delegatee(command_class)
  self.class.delegatee = command_class
end

#detailsObject



158
159
160
161
162
163
164
165
166
# File 'lib/hammer_cli/command_extensions.rb', line 158

def details
  except = @except.empty? ? '*nothing*' : @except
  details = if @only == ALLOWED_EXTENSIONS
              "*all* except #{except}"
            else
              "#{@only} only"
            end
  "#{self.class} for #{details}"
end

#extend_before_print(data) ⇒ Object



105
106
107
108
109
110
# File 'lib/hammer_cli/command_extensions.rb', line 105

def extend_before_print(data)
  allowed = @only & %i[before_print data]
  return if allowed.empty? || (allowed & @except).any?

  self.class.extend_before_print(data)
end

#extend_help(command_class) ⇒ Object



119
120
121
122
123
124
# File 'lib/hammer_cli/command_extensions.rb', line 119

def extend_help(command_class)
  allowed = @only & %i[help]
  return if allowed.empty? || (allowed & @except).any?

  self.class.extend_help(command_class)
end

#extend_option_sources(sources, command = nil) ⇒ Object



147
148
149
150
151
152
# File 'lib/hammer_cli/command_extensions.rb', line 147

def extend_option_sources(sources, command = nil)
  allowed = @only & %i[option_sources]
  return if allowed.empty? || (allowed & @except).any?

  self.class.extend_option_sources(sources, command)
end

#extend_options(command_class) ⇒ Object

Object



91
92
93
94
95
96
# File 'lib/hammer_cli/command_extensions.rb', line 91

def extend_options(command_class)
  allowed = @only & %i[command_options option]
  return if allowed.empty? || (allowed & @except).any?

  self.class.extend_options(command_class)
end

#extend_output(command_class) ⇒ Object



112
113
114
115
116
117
# File 'lib/hammer_cli/command_extensions.rb', line 112

def extend_output(command_class)
  allowed = @only & %i[output]
  return if allowed.empty? || (allowed & @except).any?

  self.class.extend_output(command_class)
end

#extend_predefined_options(command_class) ⇒ Object



98
99
100
101
102
103
# File 'lib/hammer_cli/command_extensions.rb', line 98

def extend_predefined_options(command_class)
  allowed = @only & %i[predefined_options use_option]
  return if allowed.empty? || (allowed & @except).any?

  self.class.extend_predefined_options(command_class)
end

#extend_request_headers(headers) ⇒ Object



126
127
128
129
130
131
# File 'lib/hammer_cli/command_extensions.rb', line 126

def extend_request_headers(headers)
  allowed = @only & %i[request_headers headers request]
  return if allowed.empty? || (allowed & @except).any?

  self.class.extend_request_headers(headers)
end

#extend_request_options(options) ⇒ Object



133
134
135
136
137
138
# File 'lib/hammer_cli/command_extensions.rb', line 133

def extend_request_options(options)
  allowed = @only & %i[request_options options request]
  return if allowed.empty? || (allowed & @except).any?

  self.class.extend_request_options(options)
end

#extend_request_params(params) ⇒ Object



140
141
142
143
144
145
# File 'lib/hammer_cli/command_extensions.rb', line 140

def extend_request_params(params)
  allowed = @only & %i[request_params params request]
  return if allowed.empty? || (allowed & @except).any?

  self.class.extend_request_params(params)
end

#inheritable?Boolean

Returns:

  • (Boolean)


29
30
31
32
33
# File 'lib/hammer_cli/command_extensions.rb', line 29

def inheritable?
  return @inheritable unless @inheritable.nil?

  self.class.inheritable? || false
end