Class: Evnt::Command

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

Overview

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.

Direct Known Subclasses

ApplicationCommand

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Command

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.



31
32
33
34
# File 'lib/evnt/command.rb', line 31

def initialize(params = {})
  _init_command_data(params)
  _run_command_steps
end

Instance Attribute Details

#paramsObject (readonly)

Attribute containings the list of command parameters.



15
16
17
# File 'lib/evnt/command.rb', line 15

def params
  @params
end

Class Method Details

.default_options(options) ⇒ Object

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



210
211
212
213
214
215
216
# 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) ⇒ Object

This function sets the intitialize events function for the command.



243
244
245
# File 'lib/evnt/command.rb', line 243

def to_initialize_events(&block)
  define_method('_initialize_events', &block)
end

.to_normalize_params(&block) ⇒ Object

This function sets the normalize params function for the command.



228
229
230
# File 'lib/evnt/command.rb', line 228

def to_normalize_params(&block)
  define_method('_normalize_params', &block)
end

.to_validate_logic(&block) ⇒ Object

This function sets the validate logic function for the command.



238
239
240
# File 'lib/evnt/command.rb', line 238

def to_validate_logic(&block)
  define_method('_validate_logic', &block)
end

.to_validate_params(&block) ⇒ Object

This function sets the validate params function for the command.



233
234
235
# File 'lib/evnt/command.rb', line 233

def to_validate_params(&block)
  define_method('_validate_params', &block)
end

.validates(param, options) ⇒ Object

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



219
220
221
222
223
224
225
# 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

Instance Method Details

#completed?Boolean

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

Returns:



70
71
72
# File 'lib/evnt/command.rb', line 70

def completed?
  @state[:result]
end

#error_codesObject

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



62
63
64
# File 'lib/evnt/command.rb', line 62

def error_codes
  @state[:errors].map { |e| e[:code] }
end

#error_messagesObject

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



54
55
56
# File 'lib/evnt/command.rb', line 54

def error_messages
  @state[:errors].map { |e| e[:message] }
end

#errorsObject

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.



46
47
48
# File 'lib/evnt/command.rb', line 46

def errors
  @state[:errors]
end