Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Hanami::View::Rendering::LayoutScope to inherit from Hanami::Utils::BasicObject #160

Merged
merged 4 commits into from
Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 15 additions & 22 deletions lib/hanami/view/rendering/layout_scope.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'hanami/view/rendering/null_local'
require 'hanami/view/rendering/options'
require 'hanami/utils/escape'
require 'hanami/utils/basic_object'

module Hanami
module View
Expand All @@ -16,7 +17,7 @@ module Rendering
# Scope for layout rendering
#
# @since 0.1.0
class LayoutScope < BasicObject
class LayoutScope < Utils::BasicObject
# Initialize the scope
#
# @param layout [Hanami::Layout] the layout to render
Expand All @@ -32,27 +33,6 @@ def initialize(layout, scope)
@locals = nil
end

# Returns the classname as string
#
# @return classname
#
# @since 0.3.0
def class
(class << self; self end).superclass
end

# Returns an inspect String
#
# @return [String] inspect String (contains classname, objectid in hex, available ivars)
#
# @since 0.3.0
def inspect
base = "#<#{ self.class }:#{'%x' % (self.object_id << 1)}"
base << " @layout=\"#{@layout.inspect}\"" if @layout
base << " @scope=\"#{@scope.inspect}\"" if @scope
base << ">"
end

# Render a partial or a template within a layout template.
#
# @param options [Hash]
Expand Down Expand Up @@ -254,6 +234,19 @@ def renderer(options)

private

# Returns an inspect String
#
# @return [String] inspect String (contains classname, objectid in hex, available ivars)
#
# @since x.x.x
# @api private
def __inspect
result = ""
result += %( @layout="#{@layout.inspect}") if @layout
result += %( @scope="#{@scope.inspect}") if @scope
result
end

# @api private
def _options(options)
current_locals = locals.reject do |key, _|
Expand Down
22 changes: 22 additions & 0 deletions spec/unit/hanami/view/rendering/layout_scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,28 @@
end
end

describe '#is_a?' do
it 'returns true if checked against ::BasicObject' do
expect(@scope.is_a?(BasicObject)).to be(true)
end

it 'returns true if checked against Hanami::Utils::BasicObject' do
expect(@scope.is_a?(Hanami::Utils::BasicObject)).to be(true)
end

it "returns true if checked against #{described_class}" do
expect(@scope.is_a?(described_class)).to be(true)
end

it 'returns false if checked against ::Object' do
expect(@scope.is_a?(Object)).to be(false)
end

it 'returns false if checked against ::Module' do
expect(@scope.is_a?(Module)).to be(false)
end
end

describe '#render' do
describe 'render with no known render type' do
it 'raises UnknownRenderTypeError' do
Expand Down