Class: Creeper::Middleware::Chain
- Inherits:
-
Object
- Object
- Creeper::Middleware::Chain
- Defined in:
- lib/creeper/middleware/chain.rb
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
Returns the value of attribute entries.
Instance Method Summary collapse
- #add(klass, *args) ⇒ Object
- #clear ⇒ Object
- #exists?(klass) ⇒ Boolean
-
#initialize {|_self| ... } ⇒ Chain
constructor
A new instance of Chain.
- #invoke(*args, &final_action) ⇒ Object
- #remove(klass) ⇒ Object
- #retrieve ⇒ Object
Constructor Details
#initialize {|_self| ... } ⇒ Chain
Returns a new instance of Chain.
50 51 52 53 |
# File 'lib/creeper/middleware/chain.rb', line 50 def initialize @entries = [] yield self if block_given? end |
Instance Attribute Details
#entries ⇒ Object (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 |
#clear ⇒ Object
71 72 73 |
# File 'lib/creeper/middleware/chain.rb', line 71 def clear entries.clear end |
#exists?(klass) ⇒ 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 |
#retrieve ⇒ Object
67 68 69 |
# File 'lib/creeper/middleware/chain.rb', line 67 def retrieve entries.map(&:make_new) end |