Class: Hanami::View::Rendering::LayoutFinder Private

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/view/rendering/layout_finder.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.

Defines the logic to find a layout

See Also:

Since:

  • 0.1.0

Constant Summary collapse

SUFFIX =

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.

Layout class name suffix

Since:

  • 0.1.0

'Layout'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view) ⇒ LayoutFinder

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.

Initialize the finder

Parameters:

  • view (Class, #layout)

Since:

  • 0.1.0



82
83
84
# File 'lib/hanami/view/rendering/layout_finder.rb', line 82

def initialize(view)
  @view = view
end

Class Method Details

.find(layout, namespace = Object) ⇒ Hanami::Layout

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.

Find a layout from the given name.

Examples:

With given name

require 'hanami/view'

Hanami::View::Rendering::LayoutFinder.find(:article) # =>
  ArticleLayout

With a class

require 'hanami/view'

Hanami::View::Rendering::LayoutFinder.find(ArticleLayout) # =>
  ArticleLayout

With namespace

require 'hanami/view'

Hanami::View::Rendering::LayoutFinder.find(:application, CardDeck) # =>
  CardDeck::ApplicationLayout

With nil

require 'hanami/view'

Hanami::View::Rendering::LayoutFinder.find(nil) # =>
  Hanami::View::Rendering::NullLayout

With unknown layout

require 'hanami/view'

Hanami::View::Rendering::LayoutFinder.find(:unknown) # =>
  Hanami::View::Rendering::NullLayout

Parameters:

  • layout (Symbol, String, NilClass)

    layout name or nil if you want to fallback to the framework defaults (see ‘Hanami::View.layout`).

  • namespace (Class, Module) (defaults to: Object)

    a Ruby namespace where to lookup

Returns:

  • (Hanami::Layout)

    the layout for the given name or ‘Hanami::View.layout`

Since:

  • 0.1.0



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/hanami/view/rendering/layout_finder.rb', line 64

def self.find(layout, namespace = Object)
  case layout
  when Symbol, String
    # TODO Move this low level logic into a Hanami::Utils solution
    class_name = "#{ Utils::String.classify(layout) }#{ SUFFIX }"
    namespace  = Utils::Class.load!(namespace)
    namespace.const_get(class_name)
  when Class
    layout
  end || NullLayout
end

Instance Method Details

#findHanami::Layout

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.

Find the layout for the view

Examples:

With layout

require 'hanami/view'

module Articles
  class Show
    include Hanami::View
    layout :article
  end
end

Hanami::View::Rendering::LayoutFinder.new(Articles::Show) # =>
  ArticleLayout

Without layout

require 'hanami/view'

module Dashboard
  class Index
    include Hanami::View
  end
end

Hanami::View.layout # => :application

Hanami::View::Rendering::LayoutFinder.new(Dashboard::Index) # =>
  ApplicationLayout

Returns:

See Also:

Since:

  • 0.1.0



122
123
124
# File 'lib/hanami/view/rendering/layout_finder.rb', line 122

def find
  self.class.find(@view.layout)
end