Class: Hanami::View::Rendering::Registry Private

Inherits:
LayoutRegistry show all
Defined in:
lib/hanami/view/rendering/registry.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Holds all the references of all the registered subclasses of a view. We have one registry for each superclass view.

Examples:

require 'hanami/view'

module Articles
  class Index
    include Hanami::View
  end

  class Show
    include Hanami::View
  end

  class JsonShow < Show
    format :json
  end

  class XmlShow < Show
    format :xml

    def render
      ArticleSerializer.new(article).to_xml
    end
  end
end

# We have the following templates:
#
#  * articles/index.html.erb
#  * articles/index.atom.erb
#  * articles/show.html.erb
#  * articles/show.json.erb

# One registry per superclass view
Articles::Index.send(:registry).object_id     # => 70135342862240

Articles::Show.send(:registry).object_id      # => 70135342110540
Articles::XmlShow.send(:registry).object_id   # => 70135342110540
Articles::JsonShow.send(:registry).object_id  # => 70135342110540

# It holds the references for all the templates and the views
Articles::Index.send(:registry).inspect
  # => { :all  => [Articles::Index, nil],
  #      :atom => [Articles::Index, #<Hanami::View::Template ... @file="/path/to/templates/articles/index.atom.erb"],
  #      :html => [Articles::Index, #<Hanami::View::Template ... @file="/path/to/templates/articles/index.html.erb"] }

Articles::Show.send(:registry).inspect
  # => { :all  => [Articles::Show, nil],
  #      :html => [Articles::Show,     #<Hanami::View::Template ... @file="/path/to/templates/articles/show.html.erb"],
  #      :json => [Articles::JsonShow, #<Hanami::View::Template ... @file="/path/to/templates/articles/show.json.erb"],
  #      :xml  => [Articles::XmlShow, nil] }

See Also:

Since:

  • 0.1.0

Constant Summary collapse

DEFAULT_FORMAT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Default format for views without an explicit format.

See Also:

Since:

  • 0.1.0

:all

Instance Method Summary collapse

Methods inherited from LayoutRegistry

#initialize

Constructor Details

This class inherits a constructor from Hanami::View::Rendering::LayoutRegistry

Instance Method Details

#resolve(context) ⇒ Hanami::View

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the view for the given context.

Parameters:

  • context (Hash)

    the rendering context

Options Hash (context):

  • :format (Symbol)

    the requested format

Returns:

  • (Hanami::View)

    the view associated with the given context

Raises:

See Also:

Since:

  • 0.1.0



91
92
93
94
# File 'lib/hanami/view/rendering/registry.rb', line 91

def resolve(context)
  view, template = @registry.fetch(format(context)) { @registry[DEFAULT_FORMAT] }
  view.new(template, context)
end