Browse files

Mostly working under single controller model, just need to fix up som…

…e tests
  • Loading branch information...
1 parent af64d12 commit bfa08ecfedc39ec36ed329181346e12cb89dad0b @winton committed Jul 17, 2011
View
2 README.md
@@ -66,7 +66,7 @@ Example directory structure:
Before
------
-Use `before` blocks within `controller.rb` to execute code before templates render.
+Use `before` blocks within `controller.rb` to execute code right before a template renders.
`controller.rb`:
View
2 config/gemsets.yml
@@ -2,7 +2,7 @@ stasis:
beanstalk-client: ~>1.1.0
directory_watcher: ~>1.4.0
rake: >=0.8.7
- rocco: ~>0.6
+ rocco: ~>0.8
rspec: ~>1.0
slop: ~>1.6.0
tilt: ~>1.3
View
24 lib/stasis.rb
@@ -22,7 +22,10 @@
# [sl]: http://slim-lang.com/
# [ti]: https://github.com/rtomayko/tilt
-require 'slim' rescue nil
+begin
+ require 'slim'
+rescue Exception => e
+end
# Activate the [Tilt][ti] gem.
@@ -127,15 +130,15 @@ def generate(options={})
@paths.reject! do |path|
if File.basename(path) == 'controller.rb'
# Add controller to `Controller` instance.
- @controller._add(path) if match
+ @controller._add(path)
true
else
false
end
end
# Trigger all plugin `before_all` events.
- trigger(:before_all, '*')
+ trigger(:before_all)
@paths.uniq.each do |path|
@path = path
@@ -222,35 +225,32 @@ def generate(options={})
end
# Trigger all plugin `after_all` events, passing the `Stasis` instance.
- trigger(:after_all, '*')
+ trigger(:after_all)
end
# Add a plugin to all existing controller instances. This method should be called by
# all external plugins.
def self.register(plugin)
- plugin = plugin.new
ObjectSpace.each_object(::Stasis) do |stasis|
+ plugin = plugin.new(stasis)
stasis.plugins << plugin
- stasis.controllers.each do |controller|
- controller._bind_plugin(plugin, :controller_method)
- end
+ stasis.controller._bind_plugin(plugin, :controller_method)
end
end
# Trigger an event on every plugin in the controller.
def trigger(type)
each_priority do |priority|
- args = @controller._send_to_plugin(priority, type)
+ @controller._send_to_plugin(priority, type)
end
- args
end
private
# Iterate through plugin priority integers (sorted) and yield each to a block.
def each_priority(&block)
- priorities = @plugins.collect do |klass|
- klass._priority || 0
+ priorities = @plugins.collect do |plugin|
+ plugin.class._priority
end
priorities.uniq.sort.each(&block)
end
View
3 lib/stasis/plugin.rb
@@ -7,7 +7,8 @@ class <<self
attr_accessor :_methods
# `Fixnum` -- The execution priority for this plugin (defaults to 0).
- attr_accessor :_priority
+ attr_writer :_priority
+ def _priority; @priority || 0; end
# The methods in this `Array` essentially all take the same kind of parameters.
# Either a `Hash` or an `Array` of method names. No matter what, the input is
View
8 lib/stasis/plugins/before.rb
@@ -13,16 +13,14 @@ def initialize(stasis)
# This method is bound to all controllers. Stores a block in the `@blocks` `Hash`,
# where the key is a path and the value is an `Array` of blocks.
- def before(&block)
- paths = [ nil ] if stasis.paths.empty?
+ def before(*paths, &block)
+ paths = [ nil ] if paths.empty?
if block
- @stasis.paths.each do |path|
+ paths.each do |path|
path = @stasis.controller._resolve(path, true)
@blocks[path] ||= []
@blocks[path] << block
end
- else
- @blocks[path] || []
end
end
View
6 lib/stasis/plugins/instead.rb
@@ -3,9 +3,13 @@ class Instead < Plugin
action_method :instead
+ def initialize(stasis)
+ @_stasis = stasis
+ end
+
# This method is bound to all actions.
def instead(string)
- action._render = string
+ @_stasis.action._render = string
end
end
end
View
4 lib/stasis/plugins/render.rb
@@ -40,7 +40,7 @@ def render(path_or_options={}, options={}, &block)
output =
if Tilt.mappings.keys.include?(File.extname(path)[1..-1])
- scope = options[:scope] ||= action
+ scope = options[:scope] ||= @stasis.action
Tilt.new(path).render(scope, locals, &block)
else
File.read(path)
@@ -49,7 +49,7 @@ def render(path_or_options={}, options={}, &block)
unless callback == false
# Trigger all plugin `after_render` events.
temporary_path(path) do
- action, path = @stasis.trigger(:after_render)
+ @stasis.trigger(:after_render)
end
end
View
1 lib/stasis/scope.rb
@@ -48,7 +48,6 @@ def _send_to_plugin(priority, type)
plugin.send(real_method)
end
end
- args
end
end
end
View
2 lib/stasis/scope/controller.rb
@@ -46,7 +46,7 @@ def _resolve(path, force=false)
elsif path.is_a?(Regexp)
path
# If the path is relative...
- elsif path[0..0] != '/' && (File.file?(p = File.expand_path("#{File.dirname(path)}/#{path}")) || force)
+ elsif path[0..0] != '/' && @_stasis.path && (File.file?(p = File.expand_path("#{File.dirname(@_stasis.path)}/#{path}")) || force)
p
# If the path is root...
elsif File.file?(p = File.expand_path("#{@_stasis.root}/#{path}")) || force
View
2 spec/fixtures/project/controller.rb
@@ -73,5 +73,5 @@ def helper
before do
$render_order ||= []
- $render_order << _[:path]
+ $render_order << _stasis.path
end
View
14 spec/fixtures/project/plugin.rb
@@ -1,13 +1,15 @@
class Plugin < Stasis::Plugin
+
before_all :plugin
- def plugin(controller, controllers, paths)
- if controller._[:root] == controller._[:dir]
- controller.before("custom_plugin.html") do
- instead "pass"
- end
+ def initialize(stasis)
+ @stasis = stasis
+ end
+
+ def plugin
+ @stasis.controller.before("custom_plugin.html") do
+ instead "pass"
end
- [ controller, controllers, paths ]
end
end

0 comments on commit bfa08ec

Please sign in to comment.