Module: Pancake::BootLoaderMixin

Includes:
Enumerable
Included in:
Stack::BootLoader
Defined in:
lib/pancake/bootloaders.rb

Defined Under Namespace

Classes: Base

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



60
61
62
63
64
65
66
# File 'lib/pancake/bootloaders.rb', line 60

def self.extended(base)
  base.class_eval do
    class_inheritable_reader :_bootloaders, :_central_bootloaders, :_bootloader_map
    @_bootloaders, @_central_bootloaders = {}, []
    @_bootloader_map = Hash.new{|h,k| h[k] = {:before => [], :after => []}}
  end
end

Instance Method Details

#[](name) ⇒ Object

Provides access to an individual bootloader :api: public



70
71
72
# File 'lib/pancake/bootloaders.rb', line 70

def [](name)
  _bootloaders[name]
end

#add(name, opts = {}, &block) ⇒ Object

Add a bootloader. Inside the block we’re inside a class definition. Requirements: define a run! method

Example

FooStack::BootLoader.add(:foo) do
  def run!
    # stuff
  end
end

:api: public



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/pancake/bootloaders.rb', line 91

def add(name, opts = {}, &block)
  _bootloaders[name] = Class.new(Pancake::BootLoaderMixin::Base, &block)
  raise "You must declare a #run! method on your bootloader" unless _bootloaders[name].method_defined?(:run!)
  before = opts[:before]
  after  = opts[:after]

  if opts[:level]
    levels << opts[:level]
    levels.uniq!
  end

  # If there are no before or after keys, add it to the central bootloaders
  if before
    _bootloader_map[before][:before] << name
  elsif after
    _bootloader_map[after][:after] << name
  else
    _central_bootloaders << name unless _central_bootloaders.include?(name)
  end
  _bootloaders[name].options = opts
  _bootloaders[name]
end

#delete(name) ⇒ Object

Provides removal of a bootloader, by replacing it as an empty lambda :api: public



76
77
78
# File 'lib/pancake/bootloaders.rb', line 76

def delete(name)
  !!self[name] && _bootloaders[name] = nil
end

#each(conditions = {}) ⇒ Object

Yields each bootloader in order along with it’s name

Example

FooStack::BootLoader.each do |name, bootloader|
  # do stuff
end

:api: public



161
162
163
164
165
# File 'lib/pancake/bootloaders.rb', line 161

def each(conditions = {})
  _map_bootloaders(_central_bootloaders, conditions).each do |n|
    yield n, _bootloaders[n]
  end
end

#reset!Object

Resets the bootloaders on the stack :api: public



147
148
149
150
151
# File 'lib/pancake/bootloaders.rb', line 147

def reset!
  _central_bootloaders.clear
  _bootloaders.clear
  _bootloader_map.clear
end

#run!(options = {}) ⇒ Object

Runs the bootloaders in order :api: private



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/pancake/bootloaders.rb', line 116

def run!(options = {}) # :nodoc:
  unless options.keys.include?(:only) || options.keys.include?(:except)
    options[:only] = {:level => :default}
  end
  conditions = if options[:only]
    {:only => options.delete(:only)}
  else
    {:except => options.delete(:except)}
  end
  options[:stack_class] ||= stack

  each(conditions) do |name, bl|
    next if bl.nil?
    bl.call(options)
  end
end

#stackObject

Access to the stack that this bootloader is responsible for :api: public



141
142
143
# File 'lib/pancake/bootloaders.rb', line 141

def stack
  @stack ||= Object.full_const_get(self.name.split("::")[0..-2].join("::"))
end

#stack=(stack) ⇒ Object

Set the stack that this bootloader is responsible for. :api: private



135
136
137
# File 'lib/pancake/bootloaders.rb', line 135

def stack=(stack) # :nodoc:
  @stack = stack
end