pakyow-presenter/lib/presenter/presenter.rb in pakyow-presenter-0.6.3.1 vs pakyow-presenter/lib/presenter/presenter.rb in pakyow-presenter-0.7.0

- old
+ new

@@ -1,25 +1,24 @@ module Pakyow module Presenter class Presenter < PresenterBase attr_accessor :current_context - + + def initialize + reset_state() + end + # # Methods that are called by core. This is the interface that core expects a Presenter to have # - def reload! + def load load_views end - - def present_for_request(request) - @presented = false - @root_path = nil - @root_view_is_built = false - @root_view = nil - @view_path = nil - @container_name = nil + + def prepare_for_request(request) + reset_state() @request = request end def presented? @presented @@ -97,10 +96,29 @@ populate_view(v, view_info[:views]) end v end + # This is also for creating views from within a controller using the route based lookup mechanism. + # This method takes either a dir or file path and builds either a root_view or view, respectively. + def view_for_full_view_path(f_v_p, deep = false) + v = nil + real_path_info = @view_lookup_store.real_path_info(f_v_p) + if real_path_info + if real_path_info[:file_or_dir] == :file + v = View.new(real_path_info[:real_path]) + elsif real_path_info[:file_or_dir] == :dir + root_view = @view_lookup_store.view_info(f_v_p)[:root_view] + v = View.new(root_view) + if v && deep + populate_view(v, @view_lookup_store.view_info(f_v_p)[:views]) + end + end + end + v + end + def populate_view_for_view_path(view, v_p) return view unless view_info = @view_lookup_store.view_info(v_p) views = view_info[:views] populate_view(view, views) view @@ -122,31 +140,48 @@ end # protected # - + + def reset_state + @presented = false + @root_path = nil + @root_view_is_built = false + @root_view = nil + @view_path = nil + @container_name = nil + end + def build_root_view @root_view_is_built = true if @view_path v_p = @view_path - elsif @request.restful + elsif @request && @request.restful v_p = restful_view_path(@request.restful) - elsif @request.route_spec && @request.route_spec.index(':') + elsif @request && @request.route_spec && !@request.route_spec.is_a?(Regexp) && @request.route_spec.index(':') v_p = StringUtils.remove_route_vars(@request.route_spec) else - v_p = @request.env['PATH_INFO'] + v_p = @request && @request.working_path end return unless v_p - return unless view_info = @view_lookup_store.view_info(v_p) - @presented = true - @root_path ||= view_info[:root_view] - @root_view = LazyView.new(@root_path, true) - views = view_info[:views] - populate_view(self.view, views) + if Configuration::Base.presenter.view_caching + r_v = @populated_root_view_cache[v_p] + if r_v then + @root_view = r_v.dup + @presented = true + end + else + return unless view_info = @view_lookup_store.view_info(v_p) + @root_path ||= view_info[:root_view] + @root_view = LazyView.new(@root_path, true) + views = view_info[:views] + populate_view(self.view, views) + @presented = true + end end def restful_view_path(restful_info) if restful_info[:restful_action] == :show "#{StringUtils.remove_route_vars(@request.route_spec)}/show" @@ -155,21 +190,34 @@ end end def load_views @view_lookup_store = ViewLookupStore.new("#{Configuration::Presenter.view_dir}") + if Configuration::Base.presenter.view_caching then + @populated_root_view_cache = build_root_view_cache(@view_lookup_store.view_info) + end end + def build_root_view_cache(view_info) + r_v_c = {} + view_info.each{|dir,info| + r_v = LazyView.new(info[:root_view], true) + populate_view(r_v, info[:views]) + r_v_c[dir] = r_v + } + r_v_c + end + # populates the top_view using view_store data by recursively building # and substituting in child views named in the structure def populate_view(top_view, views) containers = top_view.elements_with_ids containers.each {|e| name = e.attr("id") path = views[name] if path v = populate_view(View.new(path), views) - top_view.reset_container(name) + top_view.reset_container(name) # TODO revisit how this is implemented; assumes all LazyViews are root views top_view.add_content_to_container(v, name) end } top_view end