Skip to content
Browse files

Before all working

  • Loading branch information...
1 parent c66b35b commit 6a8dd7541b468ea4f7e4e9e0a61800b1e6956082 @winton committed Mar 23, 2011
Showing with 98 additions and 123 deletions.
  1. +28 −3 README.md
  2. +70 −120 lib/stasis.rb
View
31 README.md
@@ -77,10 +77,14 @@ The only reserved filename in a Stasis project is `controller.rb`.
You can have a `controller.rb` at any directory level:
controller.rb
- index.erb
+ index.html.erb
pages/
controller.rb
- page.erb
+ page.html.erb
+
+Controllers at the same directory level or above execute for a particular markup file.
+
+For example, `page.html.erb` uses both controllers, but `index.html.erb` only uses the top-level controller.
Callbacks
---------
@@ -131,12 +135,18 @@ Ignore
Sometimes you will want to ignore certain files entirely (no render, no copy).
-For example, ignore file names with an underscore at the beginning:
+For example, you'll often want to ignore filenames with an underscore at the beginning (partials):
### controller.rb
ignore /_.*/
+ # or
+
+ before /_.*/ do
+ @ignore = true
+ end
+
Layouts
-------
@@ -150,6 +160,12 @@ Create the layout markup:
### controller.rb
+ # set default layout for all views
+
+ layout 'layout.html.erb'
+
+ # or set layout for specific view
+
layout 'view.html.erb' => 'layout.html.erb'
# or
@@ -190,6 +206,15 @@ You may want some files to render or copy before others:
The default priority is `0`.
+Rendering
+---------
+
+Render other files within a callback, helper, or view:
+
+### view.html.erb
+
+ <%= render '_partial.html.erb', :locals => { :x => 'y' } %>
+
Summary
-------
View
190 lib/stasis.rb
@@ -5,161 +5,111 @@
require File.dirname(__FILE__) + '/stasis/gems'
Stasis::Gems.activate %w(tilt)
-require 'tilt'
$:.unshift File.dirname(__FILE__)
+require 'stasis/plugin'
+require 'stasis/plugins/after'
+require 'stasis/plugins/before'
+require 'stasis/plugins/destination'
+require 'stasis/plugins/helpers'
+require 'stasis/plugins/ignore'
+require 'stasis/plugins/layout'
+require 'stasis/plugins/priority'
+require 'stasis/plugins/render'
+
class Stasis
attr_reader :controllers, :root
def initialize(root)
@root = root
@controllers = Dir["#{root}/**/controller.rb"].inject({}) do |hash, path|
- context = Context::Controller.new path, root
- hash[context._stasis[:dir]] = context
+ context = Context::Controller.new(path, root)
+ hash[context._[:dir]] = context
hash
end
end
def generate(*paths)
paths = paths.collect { |p| Dir["#{root}/#{p}"] }.flatten
- priorities = @controllers.values.inject([]) do |array, context|
- array += context._stasis[:priority] || []
- end
- priorities.sort! { |a, b| b[1] <=> a[1] }
- priorities = priorities.inject({}) do |hash, (path_or_regexp, priority)|
- paths.each do |path|
- if path_or_regexp.is_a?(::Regexp) && path =~ path_or_regexp
- hash[path] = priority
- elsif path == path_or_regexp
- hash[path] = priority
- end
- end
- hash
- end
- priorities.merge! (paths - priorities.keys).inject({}) { |hash, path|
- hash[path] = 0
- hash
- }
- priorities = priorities.to_a.sort { |a, b| b[1] <=> a[1] }
- paths = priorities.collect { |(path, priority)| path }
- paths -= @controllers.values.inject([]) { |array, context|
- array += context._stasis[:ignore] || []
- }
- paths.each do |path|
- next unless File.file?(path)
- next unless Tilt.mappings.keys.include?(File.extname(path)[1..-1])
- rel_path = path[root.length+1..-1]
- context = Context::Render.new rel_path
- trigger :helpers, context, path
- trigger :before, context, path
- next unless context.destination
- template = Tilt.new path
- view = template.render(context)
- trigger :after, context, path
- if context.layout
- layout_path = "#{root}/#{context.layout}"
- trigger :before, context, layout_path
- template = Tilt.new layout_path
- layout = template.render(context) { view }
- trigger :after, context, layout_path
- end
- puts view.inspect
- puts layout.inspect
- end
+ puts paths.inspect
+ trigger(:before_all, '*', controllers, paths)
+ puts paths.inspect
+ # paths.each do |path|
+ # next unless File.file?(path)
+ # next unless Tilt.mappings.keys.include?(File.extname(path)[1..-1])
+ # rel_path = path[root.length+1..-1]
+ # context = Context::Render.new rel_path
+ # trigger :helpers, context, path
+ # trigger :before, context, path
+ # next unless context.destination
+ # template = Tilt.new path
+ # view = template.render(context)
+ # trigger :after, context, path
+ # if context.layout
+ # layout_path = "#{root}/#{context.layout}"
+ # trigger :before, context, layout_path
+ # template = Tilt.new layout_path
+ # layout = template.render(context) { view }
+ # trigger :after, context, layout_path
+ # end
+ # puts view.inspect
+ # puts layout.inspect
+ # end
end
-
- def trigger(type, context, path)
- dir = File.dirname path
- while dir != File.expand_path('../', root) && dir != '/'
- callbacks = controllers[dir]
- if callbacks
- blocks = callbacks.send(type, nil)
- blocks += callbacks.send(type, path)
- blocks.each do |block|
- if type == :helpers
- context.class.class_eval &block
- else
- context.instance_eval &block
- end
+
+ def trigger(type, path, *args, &block)
+ if path == '*'
+ controllers.values.each do |controller|
+ controller._send_to_plugin_by_type(type, *args, &block)
+ end
+ else
+ dir = File.dirname path
+ while dir != File.expand_path('../', root) && dir != '/'
+ if controller = controllers[dir]
+ controller._send_to_plugin_by_type(type, *args, &block)
end
+ dir = File.expand_path('../', dir)
end
- dir = File.expand_path('../', dir)
end
end
class Context
class Controller
- attr_reader :_stasis
+ attr_reader :_
+ include Plugin::Helpers
def initialize(path, root)
- dir = File.dirname path
- rel_dir = File.dirname(path)[root.length+1..-1]
- @_stasis = {
- :dir => dir,
+ @_ = {
+ :dir => File.dirname(path),
:path => path,
- :rel_dir => rel_dir,
- :resolve => lambda { |view|
- if view.nil?
- nil
- elsif view.is_a?(Regexp)
- view
- elsif File.file?(p = "#{root}#{rel_dir}/#{view}")
- p
- elsif File.file?(p = "#{root}/#{view}")
- p
- else
- false
- end
- },
+ :rel_dir => File.dirname(path)[root.length+1..-1],
:root => root
}
- instance_eval File.read(path), path
- end
-
- %w(after before helpers).each do |type|
- class_eval <<-EVAL
- def #{type}(view=nil, &block)
- @_stasis[:#{type}] ||= {}
- if block
- view = @_stasis[:resolve].call view
- return [] if view == false
- @_stasis[:#{type}][view] ||= []
- @_stasis[:#{type}][view] << block
- else
- @_stasis[:#{type}][view] || []
- end
+ @_[:plugins] = ::Stasis.constants.collect { |klass|
+ klass = klass.to_s
+ unless %w(Context Gems Plugin).include?(klass)
+ eval("::Stasis::#{klass}").new(@_)
end
- EVAL
- end
-
- def ignore(*array)
- @_stasis[:ignore] ||= []
- @_stasis[:ignore] += array.collect do |path|
- path = @_stasis[:resolve].call path
- path ? path : nil
- end
- @_stasis[:ignore].compact!
- end
-
- def priority(hash)
- @_stasis[:priority] ||= []
- @_stasis[:priority] += hash.to_a.collect do |pair|
- pair[0] = @_stasis[:resolve].call pair[0]
- pair[0] ? pair : nil
- end
- @_stasis[:priority].compact!
+ }.compact
+ _bind_plugins(:controller_method)
+ instance_eval File.read(path), path
end
end
- class Render
- attr_reader :destination, :layout, :source
+ class Action
+
+ attr_reader :_, :_controller, :_destination, :_source
+ include Plugin::Helpers
- def initialize(rel_path)
- @destination = rel_path.split('.')[0..-2].join('.')
- @source = rel_path
+ def initialize(controller)
+ @_ = controller._
+ @_controller = controller
+ @_destination = _[:rel_path].split('.')[0..-2].join('.')
+ @_source = _[:rel_path]
+ _bind_plugins(:action_method)
end
end
end

0 comments on commit 6a8dd75

Please sign in to comment.
Something went wrong with that request. Please try again.