Class: Tardvig::Command Abstract

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

Overview

This class is abstract.

The class represents abstract algorithm. It is something like Proc, but your code is stored in the class instead of object so you can use instance variables and divide your code into methods.

See the ‘spec` directory for example.

Direct Known Subclasses

Act

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Command

Returns a new instance of Command.

Parameters:

  • params (Hash) (defaults to: {})

    these key => value pairs will be instance variables with reader methods



11
12
13
# File 'lib/tardvig/command.rb', line 11

def initialize(params = {})
  handle_params(params)
end

Class Method Details

.call(params = {}) ⇒ Object

Creates a new instance and executes it.

Parameters:

  • params (Hash) (defaults to: {})

    these key => value pairs will be instance variables with reader methods

Returns:

  • self



27
28
29
# File 'lib/tardvig/command.rb', line 27

def self.call(params = {})
  new.call(params)
end

Instance Method Details

#call(params = {}) ⇒ Command

Executes your process (you should redefine the ‘process` method)

Parameters:

  • params (Hash) (defaults to: {})

    these key => value pairs will be instance variables with reader methods

Returns:



18
19
20
21
22
# File 'lib/tardvig/command.rb', line 18

def call(params = {})
  handle_params(params)
  execute
  self
end

#executeObject

The method is used for be redefined in the situation when you need to change the execution order. By default it executes ‘process` only. See the specs for example.



37
38
39
# File 'lib/tardvig/command.rb', line 37

def execute
  process
end

#processObject

This method is abstract.

Redefine this method and it will do whatever you want whilst execution

Raises:

  • (NoMethodError)


44
45
46
# File 'lib/tardvig/command.rb', line 44

def process
  raise NoMethodError, 'You should define #process for your Command'
end