racket-mvc 0.3.1 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/racket/application.rb +25 -53
- data/lib/racket/controller.rb +15 -24
- data/lib/racket/current.rb +9 -3
- data/lib/racket/helpers/view.rb +1 -0
- data/lib/racket/router.rb +22 -36
- data/lib/racket/settings/base.rb +9 -0
- data/lib/racket/settings/controller.rb +2 -1
- data/lib/racket/utils.rb +4 -0
- data/lib/racket/utils/application.rb +83 -0
- data/lib/racket/utils/exceptions.rb +1 -1
- data/lib/racket/utils/file_system.rb +86 -25
- data/lib/racket/utils/helpers.rb +74 -0
- data/lib/racket/utils/routing.rb +70 -0
- data/lib/racket/utils/views.rb +164 -81
- data/lib/racket/version.rb +1 -1
- data/lib/racket/view_manager.rb +10 -62
- data/spec/_custom.rb +5 -8
- data/spec/_default.rb +19 -15
- data/spec/racket.rb +16 -2
- metadata +4 -2
data/lib/racket/version.rb
CHANGED
data/lib/racket/view_manager.rb
CHANGED
@@ -19,17 +19,9 @@
|
|
19
19
|
module Racket
|
20
20
|
# Handles rendering in Racket applications.
|
21
21
|
class ViewManager
|
22
|
-
# Struct for holding view parameters.
|
23
|
-
ViewParams = Struct.new(:controller, :path, :type)
|
24
|
-
|
25
|
-
attr_reader :layout_cache
|
26
|
-
attr_reader :view_cache
|
27
|
-
|
28
22
|
def initialize(layout_base_dir, view_base_dir)
|
29
|
-
@
|
30
|
-
@
|
31
|
-
@layout_cache = {}
|
32
|
-
@view_cache = {}
|
23
|
+
@locator = Utils::Views::TemplateLocator.new(layout_base_dir, view_base_dir)
|
24
|
+
@renderer = Utils::Views::ViewRenderer
|
33
25
|
end
|
34
26
|
|
35
27
|
# Renders a controller based on the request path and the variables set in the
|
@@ -38,63 +30,19 @@ module Racket
|
|
38
30
|
# @param [Controller] controller
|
39
31
|
# @return [Hash]
|
40
32
|
def render(controller)
|
41
|
-
|
42
|
-
view = get_template(ViewParams.new(controller, template_path, :view))
|
43
|
-
layout = view ? get_template(ViewParams.new(controller, template_path, :layout)) : nil
|
44
|
-
if view then output = Utils.render_template(controller, view, layout)
|
45
|
-
else output = controller.racket.action_result
|
46
|
-
end
|
47
|
-
Utils.send_response(controller.response, output)
|
33
|
+
@renderer.render(controller, *get_view_and_layout(controller))
|
48
34
|
end
|
49
35
|
|
50
36
|
private
|
51
37
|
|
52
|
-
# Returns
|
53
|
-
# lookup against the provided parameters.
|
54
|
-
#
|
55
|
-
# @param [ViewParams] view_params
|
56
|
-
# @return [String|Proc|nil]
|
57
|
-
def ensure_in_cache(view_params)
|
58
|
-
_, path, type = view_params.to_a
|
59
|
-
store = instance_variable_get("@#{type}_cache".to_sym)
|
60
|
-
return store[path] if store.key?(path)
|
61
|
-
store_in_cache(store, view_params)
|
62
|
-
end
|
63
|
-
|
64
|
-
# Tries to locate a template matching +path+ in the file system and returns the path if a
|
65
|
-
# matching file is found. If no matching file is found, +nil+ is returned. The result is cached,
|
66
|
-
# meaning that the filesystem lookup for a specific path will only happen once.
|
67
|
-
#
|
68
|
-
# @param [ViewParams] view_params
|
69
|
-
# @return [String|nil]
|
70
|
-
def get_template(view_params)
|
71
|
-
template = ensure_in_cache(view_params)
|
72
|
-
# If template is a Proc, call it
|
73
|
-
if template.is_a?(Proc)
|
74
|
-
controller, path, type = view_params.to_a
|
75
|
-
template =
|
76
|
-
Utils.lookup_template(
|
77
|
-
instance_variable_get("@#{type}_base_dir".to_sym),
|
78
|
-
[File.dirname(path), Utils.call_template_proc(template, controller)].join('/')
|
79
|
-
)
|
80
|
-
end
|
81
|
-
template
|
82
|
-
end
|
83
|
-
|
84
|
-
# Stores the location of a template (not its contents) in the cache.
|
38
|
+
# Returns the view and layout that should be used for rendering.
|
85
39
|
#
|
86
|
-
# @param [
|
87
|
-
# @
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
default_template = controller.settings.fetch("default_#{type}".to_sym)
|
93
|
-
template = Utils.lookup_template_with_default(base_dir, path, default_template)
|
94
|
-
Application.inform_dev(
|
95
|
-
"Using #{type} #{template.inspect} for #{controller.class}.#{controller.racket.action}."
|
96
|
-
)
|
97
|
-
store[path] = template
|
40
|
+
# @param [Racket::Controller] controller
|
41
|
+
# @return [Array]
|
42
|
+
def get_view_and_layout(controller)
|
43
|
+
view = @locator.get_view(controller)
|
44
|
+
layout = view ? @locator.get_layout(controller) : nil
|
45
|
+
[view, layout]
|
98
46
|
end
|
99
47
|
end
|
100
48
|
end
|
data/spec/_custom.rb
CHANGED
@@ -2,14 +2,11 @@ describe 'A custom Racket test Application' do
|
|
2
2
|
extend Rack::Test::Methods
|
3
3
|
def app
|
4
4
|
@app ||= Racket::Application.using(
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
view_dir: 'templates'
|
11
|
-
},
|
12
|
-
true
|
5
|
+
default_layout: 'zebra.*',
|
6
|
+
logger: nil,
|
7
|
+
my_custom_secret: 42,
|
8
|
+
mode: :dev,
|
9
|
+
view_dir: 'templates'
|
13
10
|
)
|
14
11
|
end
|
15
12
|
|
data/spec/_default.rb
CHANGED
@@ -17,20 +17,24 @@ describe 'A default Racket test Application' do
|
|
17
17
|
app.router.routes[DefaultSubController3].should.equal('/sub3')
|
18
18
|
app.router.routes[DefaultInheritedController].should.equal('/sub3/inherited')
|
19
19
|
|
20
|
-
app.router.action_cache[DefaultRootController].length.should.equal(5)
|
21
|
-
app.router.action_cache[DefaultRootController].include?(:index).should.equal(true)
|
22
|
-
app.router.action_cache[DefaultRootController].include?(:my_first_route)
|
23
|
-
app.router.action_cache[DefaultRootController].include?(:my_second_route).should.equal(true)
|
24
|
-
app.router.action_cache[DefaultSubController1].length.should.equal(4)
|
25
|
-
app.router.action_cache[DefaultSubController1].include?(:route_to_root).should.equal(true)
|
26
|
-
app.router.action_cache[DefaultSubController1].include?(:route_to_nonexisting)
|
20
|
+
app.router.action_cache.items[DefaultRootController].length.should.equal(5)
|
21
|
+
app.router.action_cache.items[DefaultRootController].include?(:index).should.equal(true)
|
22
|
+
app.router.action_cache.items[DefaultRootController].include?(:my_first_route)
|
27
23
|
.should.equal(true)
|
28
|
-
app.router.action_cache[
|
29
|
-
|
30
|
-
app.router.action_cache[
|
31
|
-
app.router.action_cache[
|
32
|
-
app.router.action_cache[
|
33
|
-
|
24
|
+
app.router.action_cache.items[DefaultRootController].include?(:my_second_route)
|
25
|
+
.should.equal(true)
|
26
|
+
app.router.action_cache.items[DefaultSubController1].length.should.equal(4)
|
27
|
+
app.router.action_cache.items[DefaultSubController1].include?(:route_to_root).should.equal(true)
|
28
|
+
app.router.action_cache.items[DefaultSubController1].include?(:route_to_nonexisting)
|
29
|
+
.should.equal(true)
|
30
|
+
app.router.action_cache.items[DefaultSubController2].length.should.equal(5)
|
31
|
+
app.router.action_cache.items[DefaultSubController2].include?(:index).should.equal(true)
|
32
|
+
app.router.action_cache.items[DefaultSubController2].include?(:current_action)
|
33
|
+
.should.equal(true)
|
34
|
+
app.router.action_cache.items[DefaultSubController2].include?(:current_params)
|
35
|
+
.should.equal(true)
|
36
|
+
app.router.action_cache.items[DefaultSubController3].should.equal([:index])
|
37
|
+
app.router.action_cache.items[DefaultInheritedController].should.equal([:index])
|
34
38
|
end
|
35
39
|
|
36
40
|
it 'should set rack variables correctly' do
|
@@ -160,9 +164,9 @@ describe 'A default Racket test Application' do
|
|
160
164
|
end
|
161
165
|
|
162
166
|
it 'should be able to build paths correctly' do
|
163
|
-
Racket::Utils.build_path.should.equal(Pathname.pwd
|
167
|
+
Racket::Utils.build_path.should.equal(Pathname.pwd)
|
164
168
|
Racket::Utils.build_path('foo', 'bar', 'baz').should.equal(
|
165
|
-
|
169
|
+
Pathname.pwd.join('foo').join('bar').join('baz')
|
166
170
|
)
|
167
171
|
end
|
168
172
|
|
data/spec/racket.rb
CHANGED
@@ -23,9 +23,23 @@ require 'racket/helpers/file.rb'
|
|
23
23
|
require 'rack/test'
|
24
24
|
require 'bacon'
|
25
25
|
|
26
|
+
# Method tests should always be run first.
|
26
27
|
require File.join(TEST_DIR, '_request.rb')
|
27
28
|
|
28
|
-
|
29
|
-
|
29
|
+
# Application tests.
|
30
|
+
suites = [
|
31
|
+
-> { Dir.chdir(TEST_DEFAULT_APP_DIR) { require File.join(TEST_DIR, '_default.rb') } },
|
32
|
+
-> { Dir.chdir(TEST_CUSTOM_APP_DIR) { require File.join(TEST_DIR, '_custom.rb') } }
|
33
|
+
]
|
30
34
|
|
35
|
+
# Leave off randomization for now. Sessions does not seem to be reset correctly between suites!
|
36
|
+
# suites.shuffle!
|
37
|
+
|
38
|
+
# Run application tests.
|
39
|
+
suites.each do |suite|
|
40
|
+
Racket::Application.reset!
|
41
|
+
suite.call
|
42
|
+
end
|
43
|
+
|
44
|
+
# Invalid application test should always be run last.
|
31
45
|
require File.join(TEST_DIR, '_invalid.rb')
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: racket-mvc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Lars Olsson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-10-
|
11
|
+
date: 2015-10-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: http_router
|
@@ -160,8 +160,10 @@ files:
|
|
160
160
|
- lib/racket/settings/base.rb
|
161
161
|
- lib/racket/settings/controller.rb
|
162
162
|
- lib/racket/utils.rb
|
163
|
+
- lib/racket/utils/application.rb
|
163
164
|
- lib/racket/utils/exceptions.rb
|
164
165
|
- lib/racket/utils/file_system.rb
|
166
|
+
- lib/racket/utils/helpers.rb
|
165
167
|
- lib/racket/utils/routing.rb
|
166
168
|
- lib/racket/utils/views.rb
|
167
169
|
- lib/racket/version.rb
|