lib/dress.rb in dress-0.0.1 vs lib/dress.rb in dress-0.0.2
- old
+ new
@@ -88,5 +88,114 @@
end
def Dress(&block)
Dress.style(&block)
end
+
+# TODO move to monkey patch
+class Nokogiri::XML::Builder
+ def n(*docs)
+ docs.each do |doc|
+ case doc
+ when String
+ self << doc
+ when Nokogiri::XML::Node
+ insert(doc)
+ #self << doc.to_s
+ #self.doc.children.each
+ else
+ raise "bad node: #{doc}"
+ end
+ end
+ end
+
+ def t(*texts)
+ texts.each do |text|
+ self.text(text.to_s)
+ self.text(" ")
+ end
+ end
+end
+
+class Dress::Maker
+ require 'active_support'
+ require 'active_support/core_ext'
+
+ class_inheritable_hash :layout_defs
+ self.layout_defs = {}
+
+ class << self
+
+
+ def layouts
+ #read_inheritable_attribute(:layouts).keys
+ layout_defs.keys
+ end
+
+ def layout(name=nil,&block)
+ layout_defs[name] = block
+ end
+
+ def with(name,page,*args,&block)
+ content = self.new.send(page,*args,&block)
+ l = layout(name).clone
+ l.at("content").replace(content)
+ l
+ end
+
+ def render(page,*args,&block)
+ self.new.render(page,*args,&block)
+ end
+
+ def render_with(layout,page,*args,&block)
+ self.new.render_with(layout,page,*args,&block)
+ end
+ end
+
+ def render(page,*args,&block)
+ # use default layout
+ render_with(nil,page,*args,&block)
+ end
+
+ def render_with(layout,page,*args,&block)
+ content = self.send(page,*args,&block)
+ l = self.instance_eval(&self.class.layout_defs[layout])
+ l.at("content").replace(content)
+ l
+ end
+
+ def method_missing(method,*args,&block)
+ Nokogiri.make {
+ #builder = self
+ d = self.send(method,*args,&block)
+ }
+ end
+end
+
+def DressMaker(&block)
+ c = Class.new(Dress::Maker)
+ c.class_eval(&block)
+ c
+end
+
+
+# TODO break this out to a separate loadable file
+class Dress::ActiveView < Dress::Maker
+ require 'action_pack'
+ require 'action_view'
+ extend ActionView::Helpers
+ include ActionView::Helpers
+ DEFAULT_CONFIG = ActionView::DEFAULT_CONFIG unless defined?(DEFAULT_CONFIG)
+
+ def config
+ self.config = DEFAULT_CONFIG unless @config
+ @config
+ end
+
+ def config=(config)
+ @config = ActiveSupport::OrderedOptions.new.merge(config)
+ end
+
+ def initialize(controller)
+ @controller = controller
+ end
+end