Sha256: ab75112baaba17dd062c6087b5e6c638a0fc1b1048e1ee084a648b450c52f258

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

require 'lotus/view/rendering/null_template'
require 'lotus/view/rendering/templates_finder'

module Lotus
  module View
    module Rendering
      # Holds the references of all the registered layouts.
      # As now the registry is unique at the level of the framework.
      #
      # @api private
      # @since 0.1.0
      #
      # @see Lotus::Layout::ClassMethods#registry
      class LayoutRegistry < ::Hash
        # Initialize the registry
        #
        # @param view [Class] the view
        #
        # @api private
        # @since 0.1.0
        def initialize(view)
          super()

          @view = view
          prepare!
        end

        # Returns the layout for the given context.
        #
        # @param context [Hash] the rendering context
        # @option context [Symbol] :format the requested format
        #
        # @return [Lotus::Layout, Lotus::View::Rendering::NullTemplate]
        #   the layout associated with the given context or a `NullTemplate` if
        #   it can't be found.
        #
        # @raise [Lotus::View::MissingFormatError] if the given context doesn't
        #   have the :format key
        #
        # @api private
        # @since 0.1.0
        def resolve(context)
          fetch(format(context)) { NullTemplate.new }
        end

        protected
        def prepare!
          templates.each do |template|
            merge! template.format => template
          end
        end

        def templates
          TemplatesFinder.new(@view).find
        end

        def format(context)
          context.fetch(:format) { raise MissingFormatError }
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
lotus-view-0.2.0 lib/lotus/view/rendering/layout_registry.rb
lotus-view-0.1.0 lib/lotus/view/rendering/layout_registry.rb