Class: DevSystem::SimpleCommand

Inherits:
BaseCommand show all
Defined in:
lib/dev_system/subsystems/command/commands/simple_command.rb

Instance Attribute Summary

Attributes inherited from Liza::Controller

#menv

Instance Method Summary collapse

Methods inherited from BaseCommand

#action_method_name, #action_name, #around, #call, call, #env, get_command_signatures, #not_found, typo, #typo

Methods inherited from Command

get_command_signatures

Methods inherited from Liza::Controller

#`, `, attr_accessor, attr_reader, attr_writer, #attrs, box, #box, call, color, division, division!, division?, inherited, menv_accessor, menv_reader, menv_writer, on_connected, panel, #panel, plural, require, requirements, sh, #sh, singular, subsystem, subsystem!, subsystem?, subsystem_token, token

Methods inherited from Liza::Unit

_erbs_for, #add, add, cl, #cl, class_methods_defined, const_added, const_missing, constants_defined, define_error, descendants_select, division, erbs_available, erbs_defined, erbs_for, errors, #fetch, fetch, get, #get, instance_methods_defined, log, #log, log?, #log?, #log_array, log_array, log_hash, #log_hash, #log_level, log_level, #log_level?, log_level?, log_levels, #log_levels, #log_render_convert, #log_render_format, #log_render_in, #log_render_out, method_added, methods_defined, namespace, part, raise_error, #raise_error, reload!, #reload!, #render, #render!, #render_stack, renderable_formats_for, renderable_names, section, sections, #set, set, #settings, settings, singleton_method_added, sleep, #sleep, stick, #stick, sticks, #sticks, subclasses_select, subunits, system, #system, system?, test_class, time_diff, #time_diff

Instance Method Details

#afterObject



11
12
13
14
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 11

def after
  super
  after_simple
end

#ask?Boolean

Returns:

  • (Boolean)


43
44
45
46
47
48
49
50
51
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 43

def ask?
  return @ask if defined? @ask

  ask = default_booleans[:ask]
  ask_arg = given_booleans[:ask]
  ask = ask_arg unless ask_arg.nil?

  @ask = ask
end

#beforeObject



5
6
7
8
9
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 5

def before
  super
  before_given
  before_simple
end

#default_argsObject



53
54
55
56
57
58
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 53

def default_args
  h = get :default_args
  return h if h

  set :default_args, []
end

#default_booleansObject



82
83
84
85
86
87
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 82

def default_booleans
  h = get :default_booleans
  return h if h

  set :default_booleans, {}
end

#default_stringsObject



65
66
67
68
69
70
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 65

def default_strings
  h = get :default_strings
  return h if h

  set :default_strings, {}
end

#given_argsObject



18
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 18

def given_args = env[:given_args]

#given_booleansObject



22
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 22

def given_booleans = env[:given_booleans]

#given_stringsObject



20
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 20

def given_strings = env[:given_strings]

#input_argsObject



96
97
98
99
100
101
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 96

def input_args
  h = get :input_args
  return h if h

  set :input_args, []
end

#input_booleansObject



110
111
112
113
114
115
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 110

def input_booleans
  h = get :input_booleans
  return h if h

  set :input_booleans, {}
end

#input_stringsObject



103
104
105
106
107
108
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 103

def input_strings
  h = get :input_strings
  return h if h

  set :input_strings, {}
end

#set_arg(index, name, default, title) ⇒ Object

Sets a default and input method for an argument.

Examples:

set_arg(0, :file_name, "default.txt", "Enter the file name:")

Parameters:

  • index (Integer)

    The index of the argument.

  • name (Symbol)

    The name of the argument.

  • default (String)

    The default value for the argument.

  • title (String)

    The prompt title for the argument.



398
399
400
401
402
403
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 398

def set_arg(index, name, default, title)
  set_default_arg index, name, title
  set_input_arg index, name do |default|
    InputShell.ask title, default: default
  end
end

#set_boolean(name, default, title) ⇒ Object

Sets a default and input method for a boolean.

Examples:

set_boolean(:overwrite, false, "Do you want to overwrite the file?")

Parameters:

  • name (Symbol)

    The name of the boolean parameter.

  • default (Boolean)

    The default value for the boolean parameter.

  • title (String)

    The prompt title for the boolean parameter.



412
413
414
415
416
417
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 412

def set_boolean(name, default, title)
  set_default_boolean name, default
  set_input_boolean name do |default|
    InputShell.yes? title, default: default
  end
end

#set_default_arg(index, value) ⇒ Object



60
61
62
63
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 60

def set_default_arg(index, value)
  log :highest, "set_default_arg #{index.inspect} #{value.inspect}"
  default_args[index] = value
end

#set_default_array(key, value) ⇒ Object



77
78
79
80
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 77

def set_default_array(key, value)
  log :highest, "set_default_array #{key.inspect} #{value.inspect}"
  set_default_string key, value.join(",")
end

#set_default_boolean(key, value) ⇒ Object



89
90
91
92
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 89

def set_default_boolean(key, value)
  log :highest, "set_default_boolean #{key.inspect} #{value.inspect}"
  default_booleans[key] = value
end

#set_default_string(key, value) ⇒ Object



72
73
74
75
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 72

def set_default_string(key, value)
  log :highest, "set_default_string #{key.inspect} #{value.inspect}"
  default_strings[key] = value
end

#set_input_arg(index, &block) ⇒ Object



117
118
119
120
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 117

def set_input_arg(index, &block)
  log :highest, "set_input_arg #{index.inspect} #{block.source_location}"
  input_args[index] = block
end

#set_input_array(key, &block) ⇒ Object



127
128
129
130
131
132
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 127

def set_input_array(key, &block)
  log :highest, "set_input_array #{key.inspect}"
  set_input_string key do |default|
    block.call(default.to_s.split ",").join ","
  end
end

#set_input_boolean(key, &block) ⇒ Object



134
135
136
137
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 134

def set_input_boolean(key, &block)
  log :highest, "set_input_boolean #{key.inspect}"
  input_booleans[key] = block
end

#set_input_string(key, &block) ⇒ Object



122
123
124
125
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 122

def set_input_string(key, &block)
  log :highest, "set_input_string #{key.inspect}"
  input_strings[key] = block
end

#set_string(name, default, title) ⇒ Object

Sets a default and input method for a string parameter.

Examples:

set_string(:role, "guest", "Enter your role:")

Parameters:

  • name (Symbol)

    The name of the string parameter.

  • default (String)

    The default value for the string parameter.

  • title (String)

    The prompt title for the string parameter.



426
427
428
429
430
431
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 426

def set_string(name, default, title)
  set_default_string name, default
  set_input_string name do |default|
    InputShell.ask title, default: default
  end
end

#simple_arg(index) ⇒ String

Retrieves a simple argument by index if given, then default, then input.

Parameters:

  • index (Integer)

    The index of the argument to retrieve.

Returns:

  • (String)

    The argument value determined



145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 145

def simple_arg(index)
  arg_given = given_args[index]
  arg_default = default_args[index]
  arg_input = input_args[index]

  arg = arg_given || arg_default
  arg = _arg_input_call(index) if arg_input and (ask? or arg.nil?)

  env[:simple_args][index] = arg

  arg
end

#simple_arg_ask(index, title) ⇒ String

Prompts the user to input a value for a specific argument by index.

Parameters:

  • index (Integer)

    The index of the argument to retrieve.

  • title (String)

    The prompt title for user input.

Returns:

  • (String)

    The user-provided value.



351
352
353
354
355
356
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 351

def simple_arg_ask(index, title)
  set_input_arg index do |default|
    InputShell.ask(title, default: default)
  end
  simple_arg index
end

#simple_arg_ask_snakecase(index, title, regexp: /^[a-zA-Z_]*$/) ⇒ String

May prompt the user to input a value for a specific argument by index, enforcing snake_case validation.

Parameters:

  • index (Integer)

    The index of the argument to retrieve.

  • title (String)

    The prompt title for user input.

  • regexp (Regexp) (defaults to: /^[a-zA-Z_]*$/)

    The regular expression for validating input (defaults to /^[a-zA-Z_]*$/).

Returns:

  • (String)

    The user-provided value in snake_case format.



364
365
366
367
368
369
370
371
372
373
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 364

def simple_arg_ask_snakecase(index, title, regexp: /^[a-zA-Z_]*$/)
  set_input_arg index do |default|
    InputShell.ask(title, default: default) do |q|
      q.required true
      q.validate regexp
    end.snakecase
  end
  
  simple_arg index
end

#simple_argsArray<String>

Retrieves all simple arguments.

Returns:

  • (Array<String>)

    An array of simple arguments.



203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 203

def simple_args
  array = []
  [
    default_args,
    given_args,
    env[:simple_args]
  ].each do |args|
    args.each_with_index do |arg, i|
      array[i] = arg unless arg.nil?
    end
  end
  array
end

#simple_args_from_2Array<String>

Retrieves all simple arguments from the second index onward.

Returns:

  • (Array<String>)

    An array of simple arguments offset by 2.



220
221
222
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 220

def simple_args_from_2
  Array simple_args[2..-1]
end

#simple_array(key) ⇒ Array<String>

Retrieves an array of strings if given, then default, then input.

Parameters:

  • key (Symbol)

    The key to retrieve the argument.

Returns:

  • (Array<String>)

    The array of string values determined.



196
197
198
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 196

def simple_array(key)
  simple_string(key).to_s.split ","
end

#simple_boolean(key) ⇒ Boolean

Retrieves a boolean if given, then default, then input.

Parameters:

  • key (Symbol)

    The key to retrieve the argument.

Returns:

  • (Boolean)

    The boolean value determined



162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 162

def simple_boolean(key)
  boolean_given = given_booleans[key]
  boolean_default = default_booleans[key]
  boolean_input = input_booleans[key]

  boolean = boolean_given.nil? ? boolean_default : boolean_given
  boolean = _boolean_input_call key if boolean_input and (ask? or boolean.nil?)

  env[:simple_booleans][key] = boolean

  boolean
end

#simple_boolean_yes(key, title) ⇒ Boolean

May prompt the user (default true) unless arg +key or -key is found.

Parameters:

  • key (Symbol)

    The key to retrieve the argument.

  • title (String)

    The prompt title for user input.

Returns:

  • (Boolean)

    The boolean value determined by user input.



380
381
382
383
384
385
386
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 380

def simple_boolean_yes(key, title)
  set_input_boolean key do |default|
    InputShell.yes? title, default: !!default
  end

  simple_boolean key
end

#simple_booleansHash<Symbol, Boolean>

Retrieves all simple boolean arguments.

Returns:

  • (Hash<Symbol, Boolean>)

    A hash where each key is a symbol representing a boolean argument.



227
228
229
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 227

def simple_booleans
  given_booleans.merge(default_booleans).merge(env[:simple_booleans])
end

#simple_color(key, string: "I LOVE RUBY") ⇒ Symbol

Retrieves the value of an arg key=value. If a block is given and the key is not present, a color picker will be called to select a color.

Parameters:

  • key (Symbol)

    The key to retrieve from the environment.

  • string (String) (defaults to: "I LOVE RUBY")

    A prompt string for color selection.

Returns:

  • (Symbol)

    The selected color as a symbol.



318
319
320
321
322
323
324
325
326
327
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 318

def simple_color(key, string: "I LOVE RUBY")
  set_input_string key do |_default|
    InputShell.pick_color string: string
  end
  
  value = simple_string key
  value.to_sym.tap do |color|
    log :low, color.inspect
  end
end

#simple_controller_placement(key, places) ⇒ Object

key=value



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 330

def simple_controller_placement(key, places)
  set_input_string key do |_default|
    options = places.map do |place, path|
      [
        "#{place.ljust 30} path: #{path}",
        place
      ]
    end.to_h
    InputShell.pick_one "Where should the controller be placed?", options
  end
  value = simple_string key
  value.tap do |place|
    log :low, place.inspect
  end
end

#simple_string(key) ⇒ String

Retrieves a string if given, then default, then input.

Parameters:

  • key (Symbol)

    The key to retrieve the argument.

Returns:

  • (String)

    The string value determined



179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 179

def simple_string(key)
  string_given = given_strings[key]
  string_default = default_strings[key]
  string_input = input_strings[key]

  string = string_given || string_default
  string = _string_input_call key if string_input and (ask? or string.nil?)

  env[:simple_strings][key] = string

  string
end

#simple_stringsHash<Symbol, String>

Retrieves all simple string arguments.

Returns:

  • (Hash<Symbol, String>)

    A hash where each key is a symbol representing a string argument.



234
235
236
# File 'lib/dev_system/subsystems/command/commands/simple_command.rb', line 234

def simple_strings
  given_strings.merge(default_strings).merge(env[:simple_strings])
end