lib/stasis.rb in stasis-0.1.20 vs lib/stasis.rb in stasis-0.1.21

- old
+ new

@@ -22,16 +22,16 @@ # Add the project directory to the load paths. $:.unshift File.dirname(__FILE__) -# Require all Stasis library files. +# Require all Stasis library files, except for 'stasis/dev_mode' and +# 'stasis/server'. Those are demand-loaded when the corresponding command-line +# options are passed. -require 'stasis/dev_mode' require 'stasis/options' require 'stasis/plugin' -require 'stasis/server' require 'stasis/scope' require 'stasis/scope/action' require 'stasis/scope/controller' @@ -80,22 +80,23 @@ @destination = File.expand_path(@destination, @root) load_paths unless options[:development] # Create plugin instances. - @plugins = find_plugins.collect { |klass| klass.new(self) } + @plugins = Plugin.plugins.collect { |klass| klass.new(self) } + self.class.register_instance(self) load_controllers end def load_paths # Create an `Array` of paths that Stasis will act upon. @paths = Dir.glob("#{@root}/**/*", File::FNM_DOTMATCH) # Reject paths that are directories or within the destination directory. @paths.reject! do |path| - !File.file?(path) || path[0..@destination.length-1] == @destination + !File.file?(path) || path[0..@destination.length] == @destination+'/' end # Reject paths that are controllers. @paths.reject! do |path| if File.basename(path) == 'controller.rb' @@ -268,17 +269,26 @@ # Respond with collected render output if `collect` option given. collect if render_options[:collect] end + def self.register_instance(inst) + @instances ||= [] + @instances << inst + end + + def add_plugin(plugin) + plugin = plugin.new(self) + plugins << plugin + controller._bind_plugin(plugin, :controller_method) + end + # Add a plugin to all existing controller instances. This method should be called by # all external plugins. def self.register(plugin) - ObjectSpace.each_object(::Stasis) do |stasis| - plugin = plugin.new(stasis) - stasis.plugins << plugin - stasis.controller._bind_plugin(plugin, :controller_method) + @instances.each do |stasis| + stasis.add_plugin(plugin) end end # Trigger an event on every plugin in the controller. def trigger(type) @@ -293,18 +303,7 @@ def each_priority(&block) priorities = @plugins.collect do |plugin| plugin.class._priority end priorities.uniq.sort.each(&block) - end - - # Returns an `Array` of `Stasis::Plugin` classes. - def find_plugins - plugins = [] - ObjectSpace.each_object(Class) do |klass| - if klass < ::Stasis::Plugin - plugins << klass - end - end - plugins end end