Class: Creeper::Middleware::Chain

Inherits:
Object
  • Object
show all
Defined in:
lib/creeper/middleware/chain.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize {|_self| ... } ⇒ Chain

Returns a new instance of Chain.

Yields:

  • (_self)

Yield Parameters:



50
51
52
53
# File 'lib/creeper/middleware/chain.rb', line 50

def initialize
  @entries = []
  yield self if block_given?
end

Instance Attribute Details

#entriesObject (readonly)

Returns the value of attribute entries.



48
49
50
# File 'lib/creeper/middleware/chain.rb', line 48

def entries
  @entries
end

Instance Method Details

#add(klass, *args) ⇒ Object



59
60
61
# File 'lib/creeper/middleware/chain.rb', line 59

def add(klass, *args)
  entries << Entry.new(klass, *args) unless exists?(klass)
end

#clearObject



71
72
73
# File 'lib/creeper/middleware/chain.rb', line 71

def clear
  entries.clear
end

#exists?(klass) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/creeper/middleware/chain.rb', line 63

def exists?(klass)
  entries.any? { |entry| entry.klass == klass }
end

#invoke(*args, &final_action) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/creeper/middleware/chain.rb', line 75

def invoke(*args, &final_action)
  chain = retrieve.dup
  traverse_chain = lambda do
    if chain.empty?
      final_action.call
    else
      chain.shift.call(*args, &traverse_chain)
    end
  end
  traverse_chain.call
end

#remove(klass) ⇒ Object



55
56
57
# File 'lib/creeper/middleware/chain.rb', line 55

def remove(klass)
  entries.delete_if { |entry| entry.klass == klass }
end

#retrieveObject



67
68
69
# File 'lib/creeper/middleware/chain.rb', line 67

def retrieve
  entries.map(&:make_new)
end