lib/view.rb in garterbelt-0.0.4 vs lib/view.rb in garterbelt-0.0.5

- old
+ new

@@ -1,19 +1,21 @@ module Garterbelt class View - include RuPol::Swimsuit + # include RuPol::Swimsuit - attr_accessor :output, :buffer, :level, :escape + attr_accessor :output, :buffer, :level, :escape, :block, :options attr_reader :curator - def initialize(opts={}) + def initialize(opts={}, &block) + self.options = opts self.buffer = [] - self.level = (opts.delete(:level) || 0) + self.level = (options.delete(:level) || 0) self.output = "" self.escape = true + self.block = block if block_given? - self.curator = opts.delete(:curator) || self + self.curator = options.delete(:curator) || self params = self.class.default_variables.merge(opts) keys = params.keys unless ((self.class.required || []) - keys).empty? @@ -23,12 +25,12 @@ if self.class.selective_require && keys != self.class.required raise ArgumentError, "Allowed initalization options are only #{self.class.required.inspect}" end params.each do |key, value| - self.class.add_accssor(key) unless respond_to?(key) - send("#{key}=", value) + self.class.add_accessor(key) unless respond_to?(key) + instance_variable_set "@#{key}", value end end def curator=(parent_view) @curator = parent_view @@ -47,14 +49,14 @@ # VARIABLE ACCESS ----------------------------- class << self attr_accessor :required, :selective_require end - def self.add_accssor key + def self.add_accessor key key = key.to_s return if accessories.include?(key) - if instance_methods.include?(key) + if (instance_methods - Object.instance_methods).include?(key) raise ArgumentError, ":#{key} cannot be a required variable because it maps to an existing method" end accessories << key.to_s attr_accessor key @@ -98,11 +100,11 @@ alias :needs_only :requires_only end def self.build_accessors required.each do |m| - add_accssor m + add_accessor m end end # TAG HELPERS ----------------------- @@ -110,25 +112,30 @@ buffer << renderer renderer end def tag(type, *args, &block) - add_to_buffer ContentTag.new(parse_tag_arguments(type, args), &block) + t = if block_given? + ContentTag.new(parse_tag_arguments(type, args), &block) + else + ContentTag.new(parse_tag_arguments(type, args)) + end + add_to_buffer t end def closed_tag(type, *args) add_to_buffer ClosedTag.new(parse_tag_arguments(type, args)) end def non_escape_tag(*args, &block) if escape curator.escape = false - t = tag(*args, &block) + t = block_given? ? tag(*args, &block) : tag(*args) curator.escape = true t else - tag(*args, &block) + block_given? ? tag(*args, &block) : tag(*args) end end def text(content) add_to_buffer Text.new(:view => curator, :content => content) @@ -147,11 +154,11 @@ end end alias :raw :raw_text alias :rawtext :raw_text - def comment(content) + def comment_tag(content) add_to_buffer Comment.new(:view => curator, :content => content) end def doctype(type=:transitional) add_to_buffer Doctype.new(:view => curator, :type => type) @@ -195,20 +202,20 @@ 'th', 'thead', 'tr', 'tt', 'u', 'ul', 'var' ] CONTENT_TAGS.each do |type| class_eval <<-RUBY def #{type}(*args, &block) - tag(:#{type}, *args, &block) + block_given? ? tag(:#{type}, *args, &block) : tag(:#{type}, *args) end RUBY end NON_ESCAPE_TAGS = ['code', 'pre'] NON_ESCAPE_TAGS.each do |type| class_eval <<-RUBY def #{type}(*args, &block) - non_escape_tag(:#{type}, *args, &block) + block_given? ? non_escape_tag(:#{type}, *args, &block) : non_escape_tag(:#{type}, *args) end RUBY end CLOSED_TAGS = ['area', 'br', 'col', 'frame', 'hr', 'img', 'input'] @@ -228,11 +235,11 @@ end RUBY end def page_title(*args, &block) - tag(:title, *args, &block) + block_given? ? tag(:title, *args, &block) : tag(:title, *args) end def stylesheet_link(path) _link(:rel => "stylesheet", 'type' => "text/css", :href => "#{path}.css") end @@ -256,37 +263,46 @@ end render_buffer output end + def render_block + return output unless block + block.call + render_buffer + output + end + alias :to_s :render alias :to_html :render def render_buffer array = buffer.dup buffer.clear array.each do |item| if item.respond_to?(:render) item.render - item.recycle else output << item.to_s end end end - def self.render(opts={}) - content_method = opts[:method] - view = new + def self.render(opts={}, &block) + content_method = opts.delete(:method) + view = block_given? ? new(opts, &block) : new(opts) output = content_method ? view.render(content_method) : view.render - view.recycle output end def partial(*args, &block) if (klass = args.first).is_a?(Class) args.shift - view = klass.new(*args) + view = if block + klass.new(*args, &block) + else + klass.new(*args) + end else view = args.first end view.curator = curator self.buffer << view \ No newline at end of file