lib/lotus/view/rendering/layout_scope.rb in lotus-view-0.2.0 vs lib/lotus/view/rendering/layout_scope.rb in lotus-view-0.3.0

- old
+ new

@@ -2,11 +2,11 @@ module View module Rendering # Scope for layout rendering # # @since 0.1.0 - class LayoutScope + class LayoutScope < BasicObject # Initialize the scope # # @param layout [Lotus::Layout] the layout to render # @param scope [Lotus::View::Rendering::Scope] the scope of the current # view @@ -15,10 +15,31 @@ # @since 0.1.0 def initialize(layout, scope) @layout, @scope = layout, scope end + # Returns the classname as string + # + # @return classname + # + # @since 0.3.0 + def class + (class << self; self end).superclass + end + + # Returns an inspect String + # + # @return [String] inspect String (contains classname, objectid in hex, available ivars) + # + # @since 0.3.0 + def inspect + base = "#<#{ self.class }:#{'%x' % (self.object_id << 1)}" + base << " @layout=\"#{@layout}\"" if @layout + base << " @scope=\"#{@scope}\"" if @scope + base << ">" + end + # Render a partial or a template within a layout template. # # @param options [Hash] # @option options [String] :partial the partial template to render # @option options [String] :template the template to render @@ -88,10 +109,34 @@ # @since 0.1.0 def locals @locals || @scope.locals end + # Implements "respond to" logic + # + # @return [TrueClass,FalseClass] + # + # @since 0.3.0 + # + # @see http://ruby-doc.org/core/Object.html#method-i-respond_to-3F + def respond_to?(m, include_all = false) + respond_to_missing?(m, include_all) + end + + # Implements "respond to" logic + # + # @return [TrueClass,FalseClass] + # + # @since 0.3.0 + # @api private + # + # @see http://ruby-doc.org/core/Object.html#method-i-respond_to_missing-3F + def respond_to_missing?(m, include_all) + @layout.respond_to?(m, include_all) || + @scope.respond_to?(m, include_all) + end + protected # Forward all the missing methods to the view scope or to the layout. # # @api private # @since 0.1.0 @@ -106,15 +151,15 @@ # # Use like this: # <title><%= article.title %></title> # # # `article` will be looked up in the view scope first. # # If not found, it will be searched within the layout. - def method_missing(m) + def method_missing(m, *args, &blk) begin - @scope.__send__ m + @scope.__send__(m, *args, &blk) rescue - @layout.__send__ m + @layout.__send__(m, *args, &blk) end end def renderer(options) if options[:partial] @@ -127,10 +172,10 @@ private def _options(options) options.dup.tap do |opts| opts.merge!(format: format) opts[:locals] = locals - opts[:locals].merge!(options.fetch(:locals){ Hash[] }) + opts[:locals].merge!(options.fetch(:locals){ ::Hash.new }) end end end end end