class Noder::Events::EventStack

Attributes

items[R]

Public Class Methods

new(options={}) click to toggle source
# File lib/noder/events/event_stack.rb, line 10
def initialize(options={})
  @items = []
  @node_class = options[:node_class] || EMEventNode
end

Public Instance Methods

call(env=nil) click to toggle source
# File lib/noder/events/event_stack.rb, line 44
def call(env=nil)
  empty_node = @node_class.new({ callback: proc { |env| env } })
  nodes = @items.map { |item| @node_class.new(item) }
  first_node = nodes.reverse.inject(empty_node) do |next_node, current_node|
    current_node.next_node = next_node
    current_node
  end
  first_node.call(env)
end
index_of_callback(callback) click to toggle source
# File lib/noder/events/event_stack.rb, line 40
def index_of_callback(callback)
  @items.index { |item| item[:callback] == callback }
end
insert_before(target_callback, item) click to toggle source
# File lib/noder/events/event_stack.rb, line 19
def insert_before(target_callback, item)
  index = index_of_callback(target_callback)
  raise "Item not found for callback: #{target_callback}" if index.nil?
  @items.insert(index, item)
end
push(options={}) click to toggle source
# File lib/noder/events/event_stack.rb, line 15
def push(options={})
  @items << options
end
remove(target_callback) click to toggle source
# File lib/noder/events/event_stack.rb, line 31
def remove(target_callback)
  index = index_of_callback(target_callback)
  @items.delete_at(index) if index
end
remove_all() click to toggle source
# File lib/noder/events/event_stack.rb, line 36
def remove_all
  @items = []
end
replace(target_callback, item) click to toggle source
# File lib/noder/events/event_stack.rb, line 25
def replace(target_callback, item)
  index = index_of_callback(target_callback)
  raise "Item not found for callback: #{target_callback}" if index.nil?
  @items[index] = item
end

Protected Instance Methods

does_item_match?(item, env) click to toggle source
# File lib/noder/events/event_stack.rb, line 56
def does_item_match?(item, env)
  callback = item[:callback]
  return true unless callback.respond_to?(:matches_env?)
  callback.matches_env?(env)
end