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.
@@ -25,7 +25,7 @@ module Racket
25
25
  # Minor version
26
26
  MINOR = 3
27
27
  # Teeny version
28
- TEENY = 1
28
+ TEENY = 2
29
29
  # Is it a prerelease?
30
30
  PRERELEASE = false
31
31
 
@@ -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
- @layout_base_dir = layout_base_dir
30
- @view_base_dir = view_base_dir
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
- template_path = Utils.get_template_path(controller)
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 a cached template. If the template has not been cached yet, this method will run a
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 [Object] store Where to store the location
87
- # @param [ViewParams] view_params
88
- # @return [String|Proc|nil]
89
- def store_in_cache(store, view_params)
90
- controller, path, type = view_params.to_a
91
- base_dir = instance_variable_get("@#{type}_base_dir".to_sym)
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
@@ -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
- default_layout: 'zebra.*',
7
- logger: nil,
8
- my_custom_secret: 42,
9
- mode: :dev,
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
 
@@ -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).should.equal(true)
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[DefaultSubController2].length.should.equal(5)
29
- app.router.action_cache[DefaultSubController2].include?(:index).should.equal(true)
30
- app.router.action_cache[DefaultSubController2].include?(:current_action).should.equal(true)
31
- app.router.action_cache[DefaultSubController2].include?(:current_params).should.equal(true)
32
- app.router.action_cache[DefaultSubController3].should.equal([:index])
33
- app.router.action_cache[DefaultInheritedController].should.equal([:index])
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.to_s)
167
+ Racket::Utils.build_path.should.equal(Pathname.pwd)
164
168
  Racket::Utils.build_path('foo', 'bar', 'baz').should.equal(
165
- File.join(Pathname.pwd.to_s, 'foo', 'bar', 'baz')
169
+ Pathname.pwd.join('foo').join('bar').join('baz')
166
170
  )
167
171
  end
168
172
 
@@ -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
- Dir.chdir(TEST_DEFAULT_APP_DIR) { require File.join(TEST_DIR, '_default.rb') }
29
- Dir.chdir(TEST_CUSTOM_APP_DIR) { require File.join(TEST_DIR, '_custom.rb') }
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.1
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-10 00:00:00.000000000 Z
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