Skip to content

Commit

Permalink
Introducing Lotus::View.generate as shortcut for .duplicate and .conf…
Browse files Browse the repository at this point in the history
…igure
  • Loading branch information
jodosha committed Jun 11, 2014
1 parent 1781e70 commit e32f6a1
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 53 deletions.
2 changes: 1 addition & 1 deletion lib/lotus/layout.rb
Expand Up @@ -22,7 +22,7 @@ module Layout
# include Lotus::Layout
# end
def self.included(base)
conf = View.configuration
conf = Lotus::View::Configuration.for(base)
conf.add_layout(base)

base.class_eval do
Expand Down
19 changes: 19 additions & 0 deletions lib/lotus/view.rb
Expand Up @@ -72,19 +72,38 @@ def self.included(base)

include Utils::ClassAttribute

# @since 0.2.0
class_attribute :configuration
self.configuration = Configuration.new

# @since 0.2.0
def self.configure(&blk)
configuration.instance_eval(&blk)
end

# @since 0.2.0
def self.duplicate
dup.tap do |duplicated|
duplicated.configuration = configuration.duplicate
end
end

# @since 0.2.0
def self.generate(mod, views = 'Views', &blk)
duplicate.tap do |duplicated|
mod.module_eval %{
module #{ views }; end
Layout = Lotus::Layout.dup
}

duplicated.configure do
namespace "#{ mod }::#{ views }"
end

duplicated.configure(&blk) if block_given?
end
end

def self.load!
configuration.load!
end
Expand Down
9 changes: 9 additions & 0 deletions lib/lotus/view/configuration.rb
@@ -1,5 +1,7 @@
require 'set'
require 'lotus/utils/class'
require 'lotus/utils/kernel'
require 'lotus/utils/string'
require 'lotus/utils/load_paths'
require 'lotus/view/rendering/layout_finder'

Expand All @@ -12,6 +14,13 @@ class Configuration
attr_reader :views
attr_reader :layouts

def self.for(base)
# TODO this implementation is similar to Lotus::Controller::Configuration consider to extract it into Lotus::Utils
namespace = Utils::String.new(base).namespace
framework = Utils::Class.load!("(#{namespace}|Lotus)::View")
framework.configuration
end

def initialize
@namespace = Object
reset!
Expand Down
24 changes: 4 additions & 20 deletions lib/lotus/view/dsl.rb
Expand Up @@ -129,11 +129,7 @@ def format(value = nil)
# require 'lotus/view'
#
# module Furnitures
# View = Lotus::View.duplicate
#
# View.configure do
# namespace 'Furnitures'
# end
# View = Lotus::View.generate(self)
#
# class Standalone
# include Furnitures::View
Expand All @@ -153,11 +149,7 @@ def format(value = nil)
# require 'lotus/view'
#
# module Frontend
# View = Lotus::View.duplicate
#
# View.configure do
# namespace 'Frontend::Views'
# end
# View = Lotus::View.generate(self)
#
# class StandaloneView
# include Frontend::View
Expand Down Expand Up @@ -185,11 +177,7 @@ def format(value = nil)
#
# module Bookshelf
# module Web
# View = Lotus::View.duplicate
#
# View.configure do
# namespace 'Bookshelf::Web::Views'
# end
# View = Lotus::View.generate(self)
#
# module Views
# module Books
Expand All @@ -201,11 +189,7 @@ def format(value = nil)
# end
#
# module Api
# View = Lotus::View.duplicate
#
# View.configure do
# namespace 'Bookshelf::Api::Views'
# end
# View = Lotus::View.generate(self)
#
# module Views
# module Books
Expand Down
13 changes: 4 additions & 9 deletions test/fixtures.rb
Expand Up @@ -201,15 +201,10 @@ class Parent
end

module CardDeck
View = Lotus::View.duplicate

View.module_eval do
configuration.reset!
configure do
namespace CardDeck
root __dir__ + '/fixtures/templates/card_deck/app/templates'
layout :application
end
View = Lotus::View.generate(self) do
namespace CardDeck
root __dir__ + '/fixtures/templates/card_deck/app/templates'
layout :application
end

class ApplicationLayout
Expand Down
23 changes: 4 additions & 19 deletions test/integration/template_name_test.rb
Expand Up @@ -86,11 +86,7 @@ module HardwareView
# index.rb Furnitures::Furnitures::Index
# application.rb Furnitures::Application
module Furnitures
View = Lotus::View.duplicate

View.configure do
namespace Furnitures

View = Lotus::View.generate(self) do
# This line is here only for ducumentation purposes, but it's commented
# because the path doesn't exist.
#
Expand Down Expand Up @@ -137,10 +133,7 @@ class Index
# backend/
# application.rb Backend::Application
module Frontend
View = Lotus::View.duplicate

View.configure do
namespace 'Frontend::Views'
View = Lotus::View.generate(self) do
# This line is here only for ducumentation purposes, but it's commented
# because the path doesn't exist.
#
Expand Down Expand Up @@ -192,11 +185,7 @@ class New
# application.rb Bookshelf::Api
module Bookshelf
module Web
View = Lotus::View.duplicate

View.configure do
namespace 'Bookshelf::Web::Views'

View = Lotus::View.generate(self) do
# This line is here only for ducumentation purposes, but it's
# commented because the path doesn't exist.
#
Expand All @@ -213,11 +202,7 @@ class Show
end

module Api
View = Lotus::View.duplicate

View.configure do
namespace 'Bookshelf::Api::Views'

View = Lotus::View.generate(self) do
# This line is here only for ducumentation purposes, but it's
# commented because the path doesn't exist.
#
Expand Down
7 changes: 3 additions & 4 deletions test/view/configuration_test.rb
Expand Up @@ -207,10 +207,7 @@ class LazyLayout
end

module LazyApp
View = Lotus::View.duplicate
View.configure do
namespace 'LazyApp::Views'
end
View = Lotus::View.generate(self)

module Views
module Dashboard
Expand All @@ -220,6 +217,7 @@ class Index
end

class ApplicationLayout
include LazyApp::Layout
end
end
end
Expand All @@ -232,6 +230,7 @@ class ApplicationLayout

it 'lazily loads the layout' do
expected = LazyApp::Views::ApplicationLayout
expected.template.must_equal 'application'

LazyApp::Views::Dashboard::Index.layout.must_equal expected
LazyApp::Views::Dashboard::Index.configuration.layout.must_equal expected
Expand Down
58 changes: 58 additions & 0 deletions test/view_test.rb
Expand Up @@ -137,6 +137,64 @@ class ConfigurationLayout
end
end

describe '.generate' do
before do
Lotus::View.configure { layout :application }

module Generated
View = Lotus::View.generate(self)
end

module GeneratedCustom
View = Lotus::View.generate(self, 'Viewz')
end

module GeneratedConfigure
View = Lotus::View.generate(self) do
layout :app
end

module Views
class AppLayout
include GeneratedConfigure::Layout
end
end
end
end

after do
Lotus::View.configuration.reset!

Object.send(:remove_const, :Generated)
Object.send(:remove_const, :GeneratedCustom)
Object.send(:remove_const, :GeneratedConfigure)
end

it 'duplicates the configuration of the framework' do
actual = Generated::View.configuration
expected = Lotus::View.configuration

actual.layout.must_equal(expected.layout)
end

it 'generates a namespace for views' do
assert defined?(Generated::Views), 'Generated::Views expected'
end

it 'generates a custom namespace for views' do
assert defined?(GeneratedCustom::Viewz), 'GeneratedCustom::Viewz expected'
end

it 'duplicates Layout' do
assert defined?(Generated::Layout), 'Generated::Layout expected'
end

it 'optionally accepts a block to configure the generated module' do
expected = GeneratedConfigure::Views::AppLayout
GeneratedConfigure::View.configuration.layout.must_equal expected
end
end

describe 'global layout' do
before do
Lotus::View.class_eval do
Expand Down

0 comments on commit e32f6a1

Please sign in to comment.