class Evnt::Command

Commands are used to run single tasks on the system. It's like a controller on an MVC architecture without the communication with the client.

Attributes

params[R]

Attribute containings the list of command parameters.

Public Class Methods

new(params = {}) click to toggle source

The constructor is used to run a new command.

Attributes

  • params - The list of parameters for the command.

  • _options - The list of options for the command.

Options

  • exceptions - Boolean value used to activate the throw of excetions.

  • nullify_empty_params - Transform empty params to nil values so the validator

should consider them as nil values.

# File lib/evnt/command.rb, line 31
def initialize(params = {})
  _init_command_data(params)
  _run_command_steps
end

Private Class Methods

default_options(options) click to toggle source

This function sets the default options that should be used by the command.

# File lib/evnt/command.rb, line 210
def default_options(options)
  @options ||= {}
  @options.merge!(options)
  command_options = @options

  define_method('_default_options', -> { return command_options })
end
to_initialize_events(&block) click to toggle source

This function sets the intitialize events function for the command.

# File lib/evnt/command.rb, line 243
def to_initialize_events(&block)
  define_method('_initialize_events', &block)
end
to_normalize_params(&block) click to toggle source

This function sets the normalize params function for the command.

# File lib/evnt/command.rb, line 228
def to_normalize_params(&block)
  define_method('_normalize_params', &block)
end
to_validate_logic(&block) click to toggle source

This function sets the validate logic function for the command.

# File lib/evnt/command.rb, line 238
def to_validate_logic(&block)
  define_method('_validate_logic', &block)
end
to_validate_params(&block) click to toggle source

This function sets the validate params function for the command.

# File lib/evnt/command.rb, line 233
def to_validate_params(&block)
  define_method('_validate_params', &block)
end
validates(param, options) click to toggle source

This function sets the single validation request for a command parameter.

# File lib/evnt/command.rb, line 219
def validates(param, options)
  @validations ||= []
  @validations.push(param: param, options: options)
  command_validations = @validations

  define_method('_validations', -> { return command_validations })
end

Public Instance Methods

completed?() click to toggle source

This function tells if the command is completed or not. The returned object should be a boolean value.

# File lib/evnt/command.rb, line 70
def completed?
  @state[:result]
end
error_codes() click to toggle source

This function returns the list of error codes of the command. The returned object should be an array of integers.

# File lib/evnt/command.rb, line 62
def error_codes
  @state[:errors].map { |e| e[:code] }
end
error_messages() click to toggle source

This function returns the list of error messages of the command. The returned object should be an array of strings.

# File lib/evnt/command.rb, line 54
def error_messages
  @state[:errors].map { |e| e[:message] }
end
errors() click to toggle source

This function returns the list of errors of the command. The returned object should be an array of hashes with a message and a code value. The code value of hashes should be nil if code is not defined using the stop() function.

# File lib/evnt/command.rb, line 46
def errors
  @state[:errors]
end

Protected Instance Methods

err(message, code: nil) click to toggle source

This function can be used to stop the command execution and add a new error. Using err inside a callback should not stop the execution but should avoid the call of the next callback. Every time you call this function, a new error should be added to the errors list. If the exceptions option is active, it should raise a new error.

Attributes

  • message - The message string of the error.

  • code - The error code.

# File lib/evnt/command.rb, line 93
def err(message, code: nil)
  @state[:result] = false
  @state[:errors].push(
    message: message,
    code: code
  )

  # raise error if command needs exceptions
  raise message if @options[:exceptions]
end

Private Instance Methods

_init_command_data(params) click to toggle source

This function initializes the command required data.

# File lib/evnt/command.rb, line 110
def _init_command_data(params)
  # set state
  @state = {
    result: true,
    errors: []
  }

  # set options
  initial_options = {
    exceptions: false,
    nullify_empty_params: false
  }
  default_options = _safe_default_options || {}
  params_options = params[:_options] || {}
  @options = initial_options.merge(default_options)
                            .merge(params_options)

  # set other data
  @params = params
end
_run_command_steps() click to toggle source

This function calls requested steps (callback) for the command.

# File lib/evnt/command.rb, line 132
def _run_command_steps
  _validate_single_params
  _safe_normalize_params if @state[:result]
  _safe_validate_params if @state[:result]
  _safe_validate_logic if @state[:result]
  _safe_initialize_events if @state[:result]
end
_safe_default_options() click to toggle source

Safe defined functions:

# File lib/evnt/command.rb, line 173
def _safe_default_options
  return _default_options if defined?(_default_options)
  {}
end
_safe_initialize_events() click to toggle source
# File lib/evnt/command.rb, line 198
def _safe_initialize_events
  return _initialize_events if defined?(_initialize_events)
  nil
end
_safe_normalize_params() click to toggle source
# File lib/evnt/command.rb, line 183
def _safe_normalize_params
  return _normalize_params if defined?(_normalize_params)
  nil
end
_safe_validate_logic() click to toggle source
# File lib/evnt/command.rb, line 193
def _safe_validate_logic
  return _validate_logic if defined?(_validate_logic)
  nil
end
_safe_validate_params() click to toggle source
# File lib/evnt/command.rb, line 188
def _safe_validate_params
  return _validate_params if defined?(_validate_params)
  nil
end
_safe_validations() click to toggle source
# File lib/evnt/command.rb, line 178
def _safe_validations
  return _validations if defined?(_validations)
  []
end
_validate_single_params() click to toggle source

This function validates the single parameters sets with the “validates” method.

# File lib/evnt/command.rb, line 141
def _validate_single_params
  validations = _safe_validations
  validations.each do |val|
    value_to_validate = _value_to_validate(val)
    validator = Evnt::Validator.new(value_to_validate, val[:options])

    unless validator.passed?
      error = val[:options][:err] || "#{val[:param].capitalize} value not accepted"
      err_code = val[:options][:err_code] || val[:param].to_sym
      err(error, code: err_code)
      break
    end
    
    if val[:options][:do] && !send(val[:options][:do])
      break
    end

    @params[val[:param]] = validator.value
  end

  @params
end
_value_to_validate(val) click to toggle source
# File lib/evnt/command.rb, line 164
def _value_to_validate(val)
  return nil if @options[:nullify_empty_params] && params[val[:param]].empty?
  params[val[:param]]
rescue StandardError
  params[val[:param]]
end