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 option_family
].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, command_object, command_class) ⇒ Object



212
213
214
215
216
217
# File 'lib/hammer_cli/command_extensions.rb', line 212

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

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

.extend_help(command_class) ⇒ Object



226
227
228
229
230
231
# File 'lib/hammer_cli/command_extensions.rb', line 226

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_family(command_class) ⇒ Object



261
262
263
264
265
266
267
268
269
# File 'lib/hammer_cli/command_extensions.rb', line 261

def self.extend_option_family(command_class)
  return if @option_family_extensions.nil?

  @option_family_extensions.each do |extension|
    extension[:options][:creator] = command_class
    command_class.send(:option_family, extension[:options], &extension[:block])
    logger.debug("Called option family block for #{command_class}:\n\t#{extension[:block]}")
  end
end

.extend_option_sources(sources, command_object, command_class) ⇒ Object



254
255
256
257
258
259
# File 'lib/hammer_cli/command_extensions.rb', line 254

def self.extend_option_sources(sources, command_object, command_class)
  return if @option_sources_block.nil?

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

.extend_options(command_class) ⇒ Object

Class



193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/hammer_cli/command_extensions.rb', line 193

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, command_object) ⇒ Object



219
220
221
222
223
224
# File 'lib/hammer_cli/command_extensions.rb', line 219

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

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

.extend_predefined_options(command_class) ⇒ Object



207
208
209
210
# File 'lib/hammer_cli/command_extensions.rb', line 207

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, command_object, command_class) ⇒ Object



233
234
235
236
237
238
# File 'lib/hammer_cli/command_extensions.rb', line 233

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

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

.extend_request_options(options, command_object, command_class) ⇒ Object



240
241
242
243
244
245
# File 'lib/hammer_cli/command_extensions.rb', line 240

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

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

.extend_request_params(params, command_object, command_class) ⇒ Object



247
248
249
250
251
252
# File 'lib/hammer_cli/command_extensions.rb', line 247

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

  @request_params_block.call(params, command_object, command_class)
  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_family(options = {}, &block) ⇒ Object



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

def self.option_family(options = {}, &block)
  @option_family_extensions ||= []
  @option_family_extensions << {
    options: options,
    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

#command_class(command_class) ⇒ Object



173
174
175
# File 'lib/hammer_cli/command_extensions.rb', line 173

def command_class(command_class)
  @command_class = command_class
end

#command_object(command_object) ⇒ Object



177
178
179
# File 'lib/hammer_cli/command_extensions.rb', line 177

def command_object(command_object)
  @command_object = command_object
end

#delegatee(command_class) ⇒ Object



169
170
171
# File 'lib/hammer_cli/command_extensions.rb', line 169

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

#detailsObject



181
182
183
184
185
186
187
188
189
# File 'lib/hammer_cli/command_extensions.rb', line 181

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



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

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

  self.class.extend_before_print(data, @command_object, @command_class)
end

#extend_helpObject



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

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

  self.class.extend_help(@command_class)
end

#extend_option_familyObject



162
163
164
165
166
167
# File 'lib/hammer_cli/command_extensions.rb', line 162

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

  self.class.extend_option_family(@command_class)
end

#extend_option_sources(sources) ⇒ Object



155
156
157
158
159
160
# File 'lib/hammer_cli/command_extensions.rb', line 155

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

  self.class.extend_option_sources(sources, @command_object, @command_class)
end

#extend_optionsObject

Object



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

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

  self.class.extend_options(@command_class)
end

#extend_outputObject



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

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

  self.class.extend_output(@command_class, @command_object)
end

#extend_predefined_optionsObject



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

def extend_predefined_options
  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



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

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, @command_object, @command_class)
end

#extend_request_options(options) ⇒ Object



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

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, @command_object, @command_class)
end

#extend_request_params(params) ⇒ Object



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

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, @command_object, @command_class)
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