Class: Hanami::View::Rendering::Scope

Inherits:
LayoutScope
  • Object
show all
Defined in:
lib/hanami/view/rendering/scope.rb

Overview

Rendering view scope

See Also:

Since:

  • 0.1.0

Direct Known Subclasses

Subscope

Instance Method Summary collapse

Methods inherited from LayoutScope

#local, #locals, #render, #respond_to?, #view

Constructor Details

#initialize(view, locals = {}) ⇒ Scope

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 scope

Parameters:

  • view (Class)

    the view

  • locals (Hash) (defaults to: {})

    a set of objects available during the rendering

Options Hash (locals):

  • :format (Symbol)

    the requested format

Since:

  • 0.1.0



23
24
25
26
27
28
# File 'lib/hanami/view/rendering/scope.rb', line 23

def initialize(view, locals = {})
  @view   = view
  @locals = locals
  @layout = layout
  @scope  = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object (protected)

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.

Since:

  • 0.1.0



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/hanami/view/rendering/scope.rb', line 71

def method_missing(m, *args, &block)
  ::Hanami::View::Escape.html(
    # FIXME: this isn't compatible with Hanami 2.0, as it extends a view
    # that we want to be frozen in the future
    #
    # See https://github.com/hanami/view/issues/130#issuecomment-319326236
    if @view.respond_to?(m, true)
      @view.__send__ m, *args, &block
    elsif @locals.key?(m)
      @locals[m]
    else
      super
    end
  )
end

Instance Method Details

#formatSymbol

Returns the requested format.

Returns:

  • (Symbol)

    the requested format (eg. :html, :json, :xml, etc..)

Since:

  • 0.1.0



47
48
49
# File 'lib/hanami/view/rendering/scope.rb', line 47

def format
  locals[:format]
end

#inspectString

Returns an inspect String

Returns:

  • (String)

    inspect String (contains classname, objectid in hex, available ivars)

Since:

  • 0.3.0



35
36
37
38
39
40
# File 'lib/hanami/view/rendering/scope.rb', line 35

def inspect
  base = "#<#{ self.class }: #{'%x' % (self.object_id << 1)}"
  base << " @view=\"#{@view}\"" if @view
  base << " @locals=\"#{@locals}\"" if @locals
  base << ">"
end

#respond_to_missing?(m, include_all) ⇒ TrueClass, FalseClass

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.

Implements “respond to” logic

Returns:

  • (TrueClass, FalseClass)

See Also:

Since:

  • 0.3.0



59
60
61
62
63
64
65
66
# File 'lib/hanami/view/rendering/scope.rb', line 59

def respond_to_missing?(m, include_all)
  # FIXME: this isn't compatible with Hanami 2.0, as it extends a view
  # that we want to be frozen in the future
  #
  # See https://github.com/hanami/view/issues/130#issuecomment-319326236
  @view.respond_to?(m, include_all) ||
    @locals.key?(m)
end