class Tardvig::Command

@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.

Public Class Methods

call(params = {}) click to toggle source

Creates a new instance and executes it. @param (see call) @return self

# File lib/tardvig/command.rb, line 27
def self.call(params = {})
  new.call(params)
end
new(params = {}) click to toggle source

@param params [Hash] these key => value pairs will be instance variables

with reader methods
# File lib/tardvig/command.rb, line 11
def initialize(params = {})
  handle_params(params)
end

Public Instance Methods

call(params = {}) click to toggle source

Executes your process (you should redefine the `process` method) @param (see initialize) @return [Command] self

# File lib/tardvig/command.rb, line 18
def call(params = {})
  handle_params(params)
  execute
  self
end

Private Instance Methods

execute() click to toggle source

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. @!visibility public

# File lib/tardvig/command.rb, line 37
def execute
  process
end
handle_params(params) click to toggle source
# File lib/tardvig/command.rb, line 48
def handle_params(params)
  params.each_pair do |name, value|
    var_name = :"@#{name}"
    instance_variable_set var_name, value
    define_singleton_method name do
      instance_variable_get var_name
    end
  end
end
process() click to toggle source

@abstract Redefine this method and it will do whatever you want whilst

execution

@!visibility public

# File lib/tardvig/command.rb, line 44
def process
  raise NoMethodError, 'You should define #process for your Command'
end