Class: Pancake::Stack
- Extended by:
- Hooks::OnInherit, Middleware, Paths
- Defined in:
- lib/pancake/stack/app.rb,
lib/pancake/stack/stack.rb,
lib/pancake/stack/router.rb,
lib/pancake/stack/bootloader.rb,
lib/pancake/stack/configuration.rb
Direct Known Subclasses
Defined Under Namespace
Classes: BootLoader, Configuration, Router
Instance Attribute Summary collapse
-
#app_name ⇒ Object
Returns the value of attribute app_name.
Class Method Summary collapse
- ._router ⇒ Object
-
.add_root(*args) ⇒ Object
Adds the file to the stack root.
-
.configuration(label = self, &block) ⇒ Object
Provides access to the configuration block for the stack.
-
.create_bootloader_hook(*hooks) ⇒ Object
Creates a bootloader hook(s) of the given name.
-
.initialize_stack ⇒ Object
Iterates the list of roots in the stack, and initializes the app found their.
- .initialized? ⇒ Boolean
-
.load_rake_tasks!(opts = {}) ⇒ Object
Loads the rake task for this stack, and all mounted stacks.
-
.new_app_instance ⇒ Object
get a new instance of the application for this stack Ovewrite this to provide custom application initialization :api: overwritable.
-
.reset_router! ⇒ Object
Resets the router to use the stacks namespaced router.
- .router {|_router| ... } ⇒ Object
-
.stackup(opts = {}, &block) ⇒ Object
Construct a stack using the application, wrapped in the middlewares.
-
.symlink_public_files! ⇒ Object
Symlinks files in the public roots of this stack and all mounted stacks.
- .with_router {|router| ... } ⇒ Object
Instance Method Summary collapse
- #configuration(label = self.class) {|self.class.configuration(label)| ... } ⇒ Object
-
#initialize(app = nil, opts = {}) {|self.configuration(@app_name)| ... } ⇒ Stack
constructor
A new instance of Stack.
Methods included from Hooks::OnInherit
extended, inherited, on_inherit
Methods included from Middleware
build, extended, middlewares, stack, use
Methods included from Paths
dirs_and_glob_for, dirs_for, extended, paths_for, push_paths, unique_paths_for
Constructor Details
#initialize(app = nil, opts = {}) {|self.configuration(@app_name)| ... } ⇒ Stack
Returns a new instance of Stack.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/pancake/stack/stack.rb', line 45 def initialize(app = nil, opts = {}) @app_name = opts.delete(:app_name) || self.class self.class.initialize_stack unless self.class.initialized? Pancake.configuration.stacks[@app_name] = self # setup the configuration for this stack Pancake.configuration.configs[@app_name] = opts[:config] if opts[:config] self.configuration(@app_name) yield self.configuration(@app_name) if block_given? self.class::BootLoader.run!({ :stack_class => self.class, :stack => self, :app => app, :app_name => @app_name, :except => {:level => :init} }.merge(opts)) end |
Instance Attribute Details
#app_name ⇒ Object
Returns the value of attribute app_name.
3 4 5 |
# File 'lib/pancake/stack/stack.rb', line 3 def app_name @app_name end |
Class Method Details
._router ⇒ Object
9 10 11 12 13 14 15 16 17 |
# File 'lib/pancake/stack/router.rb', line 9 def self._router @_router ||= begin r = self::Router.new unless self == Pancake::Stack r.router = superclass._router.router.dup end r end end |
.add_root(*args) ⇒ Object
Adds the file to the stack root.
35 36 37 |
# File 'lib/pancake/stack/stack.rb', line 35 def self.add_root(*args) roots << Pancake.get_root(*args) end |
.configuration(label = self, &block) ⇒ Object
Provides access to the configuration block for the stack. If a block is provided, it opens the specific configuration instances anonymous class and allows you to edit it. If no block is provided, it just returns the configuration object.
:api: public
13 14 15 16 17 |
# File 'lib/pancake/stack/configuration.rb', line 13 def self.configuration(label = self, &block) config = Pancake.configuration.configs[label] ||= self::Configuration.new config.class.class_eval(&block) if block config end |
.create_bootloader_hook(*hooks) ⇒ Object
Creates a bootloader hook(s) of the given name. That are inheritable This will create hooks for use in a bootloader (but will not create the bootloader itself!)
148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/pancake/stack/stack.rb', line 148 def self.create_bootloader_hook(*hooks) hooks.each do |hook| class_inheritable_reader "_#{hook}" instance_variable_set("@_#{hook}", []) class_eval <<-RUBY def self.#{hook}(&blk) _#{hook} << blk if blk _#{hook} end RUBY end end |
.initialize_stack ⇒ Object
Iterates the list of roots in the stack, and initializes the app found their
21 22 23 24 25 26 27 28 |
# File 'lib/pancake/stack/stack.rb', line 21 def self.initialize_stack raise "Stack root not set" if roots.empty? # Run any :init level bootloaders for this stack self::BootLoader.run!(:stack_class => self, :only => {:level => :init}) @initialized = true end |
.initialized? ⇒ Boolean
41 42 43 |
# File 'lib/pancake/stack/stack.rb', line 41 def self.initialized? !!@initialized end |
.load_rake_tasks!(opts = {}) ⇒ Object
Loads the rake task for this stack, and all mounted stacks
To have your rake task loaded include a “tasks” director in the stack root
Tasks found in all stack roots are loaded in the order of the stack roots declearations
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/pancake/stack/stack.rb', line 78 def self.load_rake_tasks!(opts={}) stackup(opts) # load the application opts[:_rake_files_loaded] ||= [] # For each mounted application, load the rake tasks self::Router.mounted_applications.each do |app| if app.mounted_app.respond_to?(:load_rake_tasks!) app.mounted_app.load_rake_tasks!(opts) end end paths_for(:rake_tasks).each do |f| path = File.join(*f) unless opts[:_rake_files_loaded].include?(path) load path opts[:_rake_files_loaded] << path end end end |
.new_app_instance ⇒ Object
get a new instance of the application for this stack Ovewrite this to provide custom application initialization :api: overwritable
6 7 8 |
# File 'lib/pancake/stack/app.rb', line 6 def self.new_app_instance MISSING_APP end |
.reset_router! ⇒ Object
Resets the router to use the stacks namespaced router. This allows a router to mixin a module, and have that module mixed in to child stacks/routers. Effectively, this will reset the scope of inheritance so that a stack type can have particular route helpers
When the router is rest, any routes declared in parent stacks will be lost.
27 28 29 |
# File 'lib/pancake/stack/router.rb', line 27 def self.reset_router! self._router = self::Router.new end |
.router {|_router| ... } ⇒ Object
31 32 33 34 |
# File 'lib/pancake/stack/router.rb', line 31 def self.router yield _router if block_given? _router end |
.stackup(opts = {}, &block) ⇒ Object
Construct a stack using the application, wrapped in the middlewares
66 67 68 69 |
# File 'lib/pancake/stack/stack.rb', line 66 def self.stackup(opts = {}, &block) app = new(nil, opts, &block) Pancake.configuration.configs[app.app_name].router end |
.symlink_public_files! ⇒ Object
Symlinks files in the public roots of this stack and all mounted stacks. Provided a mounted application responds to the symlink_public_files!
method then it will be called. symlinks public files from all roots of the stacks to Pancake.root/public
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/pancake/stack/stack.rb', line 103 def self.symlink_public_files! raise "Pancake root not set" unless Pancake.root public_root = File.join(Pancake.root, "public") mount_point = configuration.router.base_url unique_paths_for(:public).sort_by{|(r,p)| p}.each do |(r,p)| # don't try to symlink the symlinks origin_path = File.join(r, p) next if r == public_root || FileTest.directory?(origin_path) output_path = File.join(public_root, mount_point, p) unless File.exists?(File.dirname(output_path)) FileUtils.mkdir_p(File.dirname(output_path)) end # unless the dir exists... create it puts "Linking #{output_path}" FileUtils.ln_s(origin_path, output_path, :force => true) end router.mounted_applications.each do |s| if s.mounted_app.respond_to?(:symlink_public_files!) s.mounted_app.symlink_public_files! end end end |
.with_router {|router| ... } ⇒ Object
36 37 38 39 |
# File 'lib/pancake/stack/router.rb', line 36 def self.with_router yield router if block_given? router end |
Instance Method Details
#configuration(label = self.class) {|self.class.configuration(label)| ... } ⇒ Object
19 20 21 22 |
# File 'lib/pancake/stack/configuration.rb', line 19 def configuration(label = self.class) yield self.class.configuration(label) if block_given? self.class.configuration(label) end |