trilby 0.1 → 0.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.
Files changed (3) hide show
  1. data/lib/trilby.rb +28 -6
  2. data/lib/trilby/controller.rb +59 -0
  3. metadata +5 -4
data/lib/trilby.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'rubygems'
2
2
  require 'sinatra/base'
3
+ require 'trilby/controller'
3
4
 
4
5
  class Trilby < Sinatra::Base
5
6
 
@@ -8,6 +9,7 @@ class Trilby < Sinatra::Base
8
9
  alias :super_put :put
9
10
  alias :super_post :post
10
11
  alias :super_delete :delete
12
+ alias :super_before :before
11
13
  end
12
14
 
13
15
 
@@ -29,7 +31,7 @@ class Trilby < Sinatra::Base
29
31
  route = "#{controller}.#{method_name}"
30
32
  route += ".#{args[:format]}" if args[:format]
31
33
 
32
- path = @@routes[route]
34
+ path = @@routes[route].clone
33
35
 
34
36
  raise "Could not find route for #{controller}.#{method_name}" unless path
35
37
  path.gsub!(/:([a-z_])*/) { |p| args[p[1..-1].to_sym] || p }
@@ -45,6 +47,7 @@ class Trilby < Sinatra::Base
45
47
 
46
48
  def self.modify_path http_method, path, controller, method_name, args
47
49
 
50
+ return unless path
48
51
 
49
52
  # Add the pretty route to the @@routes hash
50
53
  if path.is_a? String
@@ -62,21 +65,40 @@ class Trilby < Sinatra::Base
62
65
  path
63
66
  end
64
67
 
65
-
66
68
  def self.get path, controller, method_name, args={}
67
69
  path = modify_path :get, path, controller, method_name, args
68
- instance_eval("super_get(path) { controller.new(self).#{method_name} }")
70
+ instance_eval("super_get(path) do
71
+ c = controller.new(self)
72
+ c.before_filter unless args[:skip_filter]
73
+ c.#{method_name}
74
+ end")
69
75
  end
70
76
  def self.put path, controller, method_name, args={}
71
77
  path = modify_path :put, path, controller, method_name, args
72
- instance_eval("super_put(path) { controller.new(self).#{method_name} }")
78
+ instance_eval("super_put(path) do
79
+ c = controller.new(self)
80
+ c.before_filter unless args[:skip_filter]
81
+ c.#{method_name}
82
+ end")
73
83
  end
74
84
  def self.post path, controller, method_name, args={}
75
85
  path = modify_path :post, path, controller, method_name, args
76
- instance_eval("super_post(path) { controller.new(self).#{method_name} }")
86
+ instance_eval("super_post(path)do
87
+ c = controller.new(self)
88
+ c.before_filter unless args[:skip_filter]
89
+ c.#{method_name}
90
+ end")
77
91
  end
78
92
  def self.delete path, controller, method_name, args={}
79
93
  path = modify_path :delete, path, controller, method_name, args
80
- instance_eval("super_delete(path) { controller.new(self).#{method_name} }")
94
+ instance_eval("super_delete(path) do
95
+ c = controller.new(self)
96
+ c.before_filter unless args[:skip_filter]
97
+ c.#{method_name}
98
+ end")
99
+ end
100
+ def self.before path, controller, method_name, args={}
101
+ path = modify_path :before, path, controller, method_name, args
102
+ instance_eval("super_before(path) {controller.new(self).#{method_name} }")
81
103
  end
82
104
  end
@@ -0,0 +1,59 @@
1
+
2
+ class TrilbyController
3
+ def initialize(sinatra)
4
+ @sinatra = sinatra
5
+ end
6
+
7
+ def before
8
+ # Empty passthru
9
+ end
10
+
11
+ def before_filter
12
+ if self.respond_to? :get_parent_controller
13
+ Kernel.const_get(get_parent_controller).new(@sinatra).before_filter
14
+ end
15
+ before
16
+ end
17
+
18
+ def self.parent_controller controller
19
+ define_method(:get_parent_controller) do |method|
20
+ return controller
21
+ end
22
+ end
23
+
24
+ def method_missing(meth, *args, &block)
25
+ if @sinatra.respond_to? meth
26
+ @sinatra.send(meth, *args, &block)
27
+ else
28
+ super
29
+ end
30
+ end
31
+
32
+ #
33
+ # This magic sauce is what makes trilby work with views.
34
+ #
35
+ def put_on_a_hat
36
+
37
+ # set all instance variables we have in the sinatra request object
38
+ self.instance_variables.each { |v| @sinatra.instance_variable_set(v, self.instance_variable_get(v)) }
39
+
40
+ end
41
+
42
+ #
43
+ # If a template dir is specified, use that (or don't use one if
44
+ # nil is specified). Otherwise, if the controller has a default
45
+ # view directory, use that.
46
+ #
47
+ def render template, options={}
48
+ put_on_a_hat
49
+
50
+ if options.has_key? :dir
51
+ template = :"#{options[:dir]}/#{template}" if options[:dir]
52
+
53
+ elsif view_dir
54
+ template = :"#{view_dir}/#{template}"
55
+ end
56
+
57
+ return erubis template, options
58
+ end
59
+ end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trilby
3
3
  version: !ruby/object:Gem::Version
4
- hash: 9
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- version: "0.1"
8
+ - 2
9
+ version: "0.2"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Kaleb Pomeroy
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2013-03-04 00:00:00 Z
17
+ date: 2013-03-07 00:00:00 Z
18
18
  dependencies:
19
19
  - !ruby/object:Gem::Dependency
20
20
  name: sinatra
@@ -40,6 +40,7 @@ extra_rdoc_files: []
40
40
 
41
41
  files:
42
42
  - lib/trilby.rb
43
+ - lib/trilby/controller.rb
43
44
  homepage: https://github.com/KalebPomeroy/trilby
44
45
  licenses: []
45
46