Class: Processable

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

Overview

Examples:

Fetch repos from Github and print to console

class PrintGithubRepos < Processable
  step :get_repos do
    github_client.where('rails', per_page: 100)
  end

  step :get_only_essential_fields do |repos_json|
    repos_json.except(:title, :description)
  end

  step :print_to_console do |repos_json|
    print repos_json
  end
end

Defined Under Namespace

Classes: Error

Constant Summary collapse

@@registered_steps =

Collections of steps like

[]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.step(name) {|block| ... } ⇒ Object

Adding new step to the process

Parameters:

  • name (String)

    Name of the step to have a reference for further interactions

Yield Parameters:

  • block (block)

    Block which will be executed when method ‘process’ called on the instance of the class



28
29
30
# File 'lib/processable.rb', line 28

def step(name, &block)
  @@registered_steps = @@registered_steps << { name: name, block: block }
end

Instance Method Details

#processObject

Run steps in order as they were defined



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/processable.rb', line 34

def process
  @@registered_steps.each_with_index do |step, index|
    previous_step = index.zero? ? {} : @@registered_steps[index - 1]

    result = instance_exec(previous_step[:result],
                           step,
                           @@registered_steps,
                           &step[:block])

    step[:result] = result
  end

  @@registered_steps.last[:result]
end