lib/hanami/view/rendering.rb in hanami-view-0.6.1 vs lib/hanami/view/rendering.rb in hanami-view-0.7.0

- old
+ new

@@ -1,7 +1,8 @@ require 'hanami/view/rendering/registry' require 'hanami/view/rendering/scope' +require 'hanami/view/rendering/null_local' module Hanami module View # Rendering methods # @@ -102,10 +103,29 @@ # view.render # => {title: ...} def render layout.render end + # It tries to invoke a method for the view or a local for the given key. + # If the lookup fails, it returns a null object. + # + # @return [Objeect,Hanami::View::Rendering::NullLocal] the returning value + # + # @since 0.7.0 + # + # @example + # <% if local(:plan).overdue? %> + # <h2>Your plan is overdue.</h2> + # <% end %> + def local(key) + if respond_to?(key) + __send__(key) + else + locals.fetch(key) { NullLocal.new(key) } + end + end + protected # The output of the template rendering process. # # @return [String] the rendering output # @@ -244,11 +264,11 @@ # @since 0.1.0 # # @see Hanami::View.load! def load! super - registry.freeze + load_registry! end private # The registry that holds all the registered subclasses. @@ -257,9 +277,14 @@ # @since 0.1.0 # # @see Hanami::View::Rendering::Registry def registry @registry ||= Registry.new(self) + end + + def load_registry! + @registry = nil + registry.freeze end end end end