Class: Webcommand::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/webcommand/command.rb

Constant Summary collapse

DEFAULT_PARAM_SCHEMA_REGEX =
/.*/
PARAMS_DEFAULT_SCHEMA =
{}.tap do |hash|
  hash.default_proc = proc { DEFAULT_PARAM_SCHEMA_REGEX }
end
WrongParamsSize =
Class.new(StandardError)
InvalidParams =
Class.new(StandardError)

Instance Method Summary collapse

Constructor Details

#initialize(template_command, params_schema: nil, executor: TTY::Command) ⇒ Command

Returns a new instance of Command.



11
12
13
14
15
# File 'lib/webcommand/command.rb', line 11

def initialize(template_command, params_schema: nil, executor: TTY::Command)
  @template_command = template_command
  @params_schema = params_schema || PARAMS_DEFAULT_SCHEMA
  @executor = executor
end

Instance Method Details

#call(**params) ⇒ Object

Raises:



17
18
19
20
21
22
23
24
25
# File 'lib/webcommand/command.rb', line 17

def call(**params)
  raise WrongParamsSize, params if wrong_params_size?(params)
  raise InvalidParams, params unless valid_params?(params)

  template_render = make_template_renderer(params)
  command = template_render.render(@template_command)
  #TODO: Extract executor to a new class returning a customized execution result
  @executor.new.run!(command)
end