jeanine 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d62b1858b952c8c8b7575f4a5c8ff60d9401e846d6d9fe9d302573052715706c
4
- data.tar.gz: fd9d260653e2164bd98fc333dedcbfedf1e58fd0f7feb33d84435406668a76df
3
+ metadata.gz: dab8651a092f6e68e49ce6774d3c1c3156e1cf51aed9e266a4afbc438c62c041
4
+ data.tar.gz: defb2a775a5ae99a7e19deca237f778a7795525339c35f6d1da2cdb917d64179
5
5
  SHA512:
6
- metadata.gz: bcfb247647f312eb473cf7681b1415dd7cdd828f545711cdb5e7cf641c69127d832617e952ece9f83daf1c09e391624efc70acd7259aa3ad1d168a01b93aa135
7
- data.tar.gz: 6afa5f33deb9c2a2094e317bdd5b755aa184a97c7378da944cc73968c4069d17b3e8c37b8f996efb7cd81b8af115aaed13406e45afd9bd5e1382225513c44bbc
6
+ metadata.gz: dfd4e2901c604b06a262da1d05a660ade74e4810ba25d4917ae1f873bf9f306213ca28b20b2a3c028f4ca5f95706e90d51566edff3bbe10c8559dec735d36377
7
+ data.tar.gz: 92e87f254903c370d1708fdd09dff0921e77ff3d5fa804fb424cfae86febc7aab1419bbde3665b9813c0801d89d567eb4e8e1a36cd2aba4e90531bfb9620d354
@@ -1,18 +1,14 @@
1
+ require 'rack'
2
+
1
3
  require 'forwardable'
2
4
  require 'logger'
3
5
 
4
6
  require "jeanine/version"
5
7
  require 'jeanine/core_ext'
6
- require 'jeanine/router'
7
- require 'jeanine/app'
8
- require 'tilt'
9
8
 
10
9
  module Jeanine
11
- def self.view_paths
12
- @_view_paths ||= Set.new(["views"])
13
- end
14
- def self.tilt_cache
15
- @title_cache ||= Tilt::Cache.new
10
+ def self._installed_plugins
11
+ @_installed_plugins ||= []
16
12
  end
17
13
 
18
14
  def self.logger
@@ -26,4 +22,7 @@ module Jeanine
26
22
  def self.router
27
23
  @router ||= Router.new
28
24
  end
25
+
26
+ autoload :Router, 'jeanine/router'
27
+ autoload :App, 'jeanine/app'
29
28
  end
@@ -1,26 +1,35 @@
1
- require 'jeanine/callbacks'
2
1
  require 'jeanine/mimes'
3
2
  require 'jeanine/request'
4
- require 'jeanine/rescuing'
5
3
  require 'jeanine/response'
6
- require 'jeanine/renderer'
7
4
  require 'jeanine/routing'
8
- require 'jeanine/session'
9
- require 'jeanine/view_paths'
10
5
 
11
6
  module Jeanine
12
7
  class App
13
- include Session
14
- include Rescuing
15
8
  include Routing::Evaluation
16
9
 
17
10
  attr_reader :request, :response
18
11
 
12
+ def self.plugin(name)
13
+ unless Jeanine._installed_plugins.include?(name)
14
+ unless const_defined?("Jeanine::#{name}")
15
+ require "jeanine/#{name.to_s.underscore}"
16
+ end
17
+ include Kernel.const_get("Jeanine::#{name}")
18
+ Jeanine._installed_plugins << name
19
+ end
20
+ end
21
+
22
+ def self.installed_plugins
23
+ Jeanine._installed_plugins
24
+ end
25
+
26
+ # plugin :Callbacks
27
+ # plugin :Rendering
28
+ # plugin :Rescuing
29
+ # plugin :Session
30
+
19
31
  class << self
20
- include Callbacks
21
32
  include Routing::DSL
22
- include Rescuing
23
- include ViewPaths
24
33
 
25
34
  alias :_new :new
26
35
  def new(*args, &block)
@@ -47,21 +56,10 @@ module Jeanine
47
56
  end
48
57
 
49
58
  def call(env)
50
- begin
51
- @env = env
52
- @request = Jeanine::Request.new(env)
53
- @response = Jeanine::Response.new
54
- catch(:halt) { route_eval }
55
- rescue => error
56
- handler = self.class.rescue_handlers[error.class]
57
- raise error unless handler
58
- if handler.is_a?(Symbol)
59
- @response.write(send(handler, error))
60
- else
61
- @response.write(instance_exec(error, &handler))
62
- end
63
- @response.complete!
64
- end
59
+ @env = env
60
+ @request = Jeanine::Request.new(env)
61
+ @response = Jeanine::Response.new
62
+ catch(:halt) { route_eval }
65
63
  end
66
64
 
67
65
  private
@@ -74,11 +72,6 @@ module Jeanine
74
72
  end
75
73
  end
76
74
 
77
- def render(*args)
78
- @response.action_variables = instance_variables_cache
79
- Renderer.new(@response).render(*args)
80
- end
81
-
82
75
  def instance_variables_cache
83
76
  instance_variables.each_with_object({}) do |var, obj|
84
77
  obj[var] = instance_variable_get(var)
@@ -1,28 +1,105 @@
1
1
  module Jeanine
2
2
  module Callbacks
3
- def _callbacks
4
- @_callbacks ||= {
5
- before: [],
6
- after: [],
7
- before_all: [],
8
- after_all: []
9
- }
3
+ def self.included(klass)
4
+ klass.extend InstanceMethods
5
+ klass.prepend EvaluationMethods
10
6
  end
11
7
 
12
- def before(*paths, &block)
13
- _register_callback(:before, paths, &block)
14
- end
8
+ module InstanceMethods
9
+ def _callbacks
10
+ @_callbacks ||= {
11
+ before: [],
12
+ after: [],
13
+ before_all: [],
14
+ after_all: []
15
+ }
16
+ end
17
+
18
+ def before(*paths, &block)
19
+ _register_callback(:before, paths, &block)
20
+ end
21
+
22
+ def after(*paths, &block)
23
+ _register_callback(:after, paths, &block)
24
+ end
25
+
26
+ private
15
27
 
16
- def after(*paths, &block)
17
- _register_callback(:after, paths, &block)
28
+ def _register_callback(type, paths = [], &block)
29
+ if paths == []
30
+ _callbacks["#{type}_all".to_sym] << { block: block }
31
+ else
32
+ _callbacks[type] << { paths: paths, block: block }
33
+ end
34
+ end
18
35
  end
19
36
 
20
- private
21
- def _register_callback(type, paths = [], &block)
22
- if paths == []
23
- _callbacks["#{type}_all".to_sym] << { block: block }
24
- else
25
- _callbacks[type] << { paths: paths, block: block }
37
+ module EvaluationMethods
38
+ def route_eval
39
+ before_find_route!
40
+ route = find_route
41
+
42
+ if route
43
+ before_evaluate_route!
44
+ result = instance_eval(&route[:block])
45
+ @response.write(result)
46
+ after_evaluate_route!
47
+ else
48
+ @response.status = 404
49
+ end
50
+ after_response!
51
+ @response.complete!
52
+ end
53
+
54
+ private
55
+
56
+ def before_find_route!
57
+ run_before_callbacks!(:before_all)
58
+ end
59
+
60
+ def before_evaluate_route!
61
+ run_before_callbacks!(:before)
62
+ end
63
+
64
+ def after_evaluate_route!
65
+ run_after_callbacks!(:after)
66
+ end
67
+
68
+ def after_response!
69
+ run_after_callbacks!(:after_all)
70
+ end
71
+
72
+ def run_before_callbacks!(type)
73
+ if type == :before_all
74
+ self.class._callbacks[type].each { |callback| eval_callback(&callback[:block]) }
75
+ else
76
+ matching_callbacks(type) do |callback|
77
+ eval_callback(&callback[:block])
78
+ end
79
+ end
80
+ end
81
+
82
+ def run_after_callbacks!(type)
83
+ if type == :after_all
84
+ self.class._callbacks[type].each { |callback| eval_callback(&callback[:block]) }
85
+ else
86
+ matching_callbacks(type) do |callback|
87
+ eval_callback(&callback[:block])
88
+ end
89
+ end
90
+ end
91
+
92
+ def matching_callbacks(type)
93
+ self.class._callbacks[type].select do |callback|
94
+ paths = callback[:paths]
95
+ if paths.detect { |path| path.match?(@request.path_info) }
96
+ yield callback
97
+ end
98
+ end
99
+ end
100
+
101
+ def eval_callback(*args, &callback)
102
+ instance_exec(*args, &callback)
26
103
  end
27
104
  end
28
105
  end
@@ -3,4 +3,11 @@ class String
3
3
  return nil if self.empty?
4
4
  self
5
5
  end
6
+ def underscore
7
+ self.gsub(/::/, '/').
8
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
9
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
10
+ tr("-", "_").
11
+ downcase
12
+ end
6
13
  end
@@ -1,18 +1,16 @@
1
1
  module Broding
2
- module Environment
3
- def env
4
- @_env ||= (ENV["RACK_ENV"].presence || "development")
5
- end
2
+ def env
3
+ @_env ||= (ENV["RACK_ENV"].presence || "development")
4
+ end
6
5
 
7
- def groups(*groups)
8
- hash = groups.extract_options!
9
- env = Jeanine.env
10
- groups.unshift(:default, env)
11
- groups.concat ENV["BRODY_GROUPS"].to_s.split(",")
12
- groups.concat hash.map { |k, v| k if v.map(&:to_s).include?(env) }
13
- groups.compact!
14
- groups.uniq!
15
- groups
16
- end
6
+ def groups(*groups)
7
+ hash = groups.extract_options!
8
+ env = Jeanine.env
9
+ groups.unshift(:default, env)
10
+ groups.concat ENV["BRODY_GROUPS"].to_s.split(",")
11
+ groups.concat hash.map { |k, v| k if v.map(&:to_s).include?(env) }
12
+ groups.compact!
13
+ groups.uniq!
14
+ groups
17
15
  end
18
16
  end
@@ -1,11 +1,15 @@
1
- require 'rack'
2
-
3
1
  module Jeanine
4
2
  class Mimes
5
3
  MimeTypeNotFound = Class.new(StandardError)
6
4
  MIME_TYPES_INVERTED = ::Rack::Mime::MIME_TYPES.invert
7
5
 
6
+ def self.loaded?
7
+ @loaded
8
+ end
9
+
8
10
  def self.load!
11
+ return if loaded?
12
+ @loaded = true
9
13
  register(:json, Rack::Mime::MIME_TYPES[".json"])
10
14
  register(:html, Rack::Mime::MIME_TYPES[".html"])
11
15
  register(:text, Rack::Mime::MIME_TYPES[".text"])
@@ -1,6 +1,15 @@
1
1
  require 'jeanine/view'
2
+ require 'jeanine/view_paths'
2
3
 
3
4
  module Jeanine
5
+ def self.view_paths
6
+ @_view_paths ||= Set.new(["views"])
7
+ end
8
+
9
+ def self.tilt_cache
10
+ @title_cache ||= Tilt::Cache.new
11
+ end
12
+
4
13
  class Renderer
5
14
  def self._renderers
6
15
  @_renderers ||= Set.new
@@ -139,4 +148,21 @@ module Jeanine
139
148
  text
140
149
  end
141
150
  end
151
+
152
+ module Rendering
153
+ def self.included(klass)
154
+ klass.extend ClassMethods
155
+ end
156
+
157
+ def render(*args)
158
+ @response.action_variables = instance_variables_cache
159
+ Renderer.new(@response).render(*args)
160
+ end
161
+
162
+ module ClassMethods
163
+ def append_view_path(path)
164
+ Jeanine.view_paths << path
165
+ end
166
+ end
167
+ end
142
168
  end
@@ -1,5 +1,4 @@
1
1
  require 'rack/request'
2
- require 'jeanine/headers'
3
2
 
4
3
  module Jeanine
5
4
  class Request < Rack::Request
@@ -17,10 +16,6 @@ module Jeanine
17
16
  super
18
17
  end
19
18
 
20
- def headers
21
- @headers ||= Jeanine::Headers.new(self)
22
- end
23
-
24
19
  def post?
25
20
  request_method == 'POST'
26
21
  end
@@ -1,17 +1,41 @@
1
1
  module Jeanine
2
2
  module Rescuing
3
- def rescue_from(*exceptions, with: nil, &block)
4
- exceptions.each do |exception|
5
- if with
6
- rescue_handlers[exception] = with
7
- else
8
- rescue_handlers[exception] = block
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ base.prepend InstanceMethods
6
+ end
7
+
8
+ module InstanceMethods
9
+ def call(env)
10
+ begin
11
+ super
12
+ rescue => error
13
+ handler = self.class.rescue_handlers[error.class.to_s]
14
+ raise error unless handler
15
+ if handler.is_a?(Symbol)
16
+ @response.write(send(handler, error))
17
+ else
18
+ @response.write(instance_exec(error, &handler))
19
+ end
20
+ @response.complete!
9
21
  end
10
22
  end
11
23
  end
12
24
 
13
- def rescue_handlers
14
- @rescue_handlers ||= {}
25
+ module ClassMethods
26
+ def rescue_from(*exceptions, with: nil, &block)
27
+ exceptions.each do |exception|
28
+ if with
29
+ rescue_handlers[exception.to_s] = with
30
+ else
31
+ rescue_handlers[exception.to_s] = block
32
+ end
33
+ end
34
+ end
35
+
36
+ def rescue_handlers
37
+ @rescue_handlers ||= {}
38
+ end
15
39
  end
16
40
  end
17
41
  end
@@ -2,18 +2,13 @@ module Jeanine
2
2
  module Routing
3
3
  module Evaluation
4
4
  def route_eval
5
- before_find_route!
6
5
  route = find_route
7
-
8
6
  if route
9
- before_evaluate_route!
10
7
  result = instance_eval(&route[:block])
11
8
  @response.write(result)
12
- after_evaluate_route!
13
9
  else
14
10
  @response.status = 404
15
11
  end
16
- after_response!
17
12
  @response.complete!
18
13
  end
19
14
 
@@ -42,54 +37,6 @@ module Jeanine
42
37
  route
43
38
  end
44
39
 
45
- def before_find_route!
46
- run_before_callbacks!(:before_all)
47
- end
48
-
49
- def before_evaluate_route!
50
- run_before_callbacks!(:before)
51
- end
52
-
53
- def after_evaluate_route!
54
- run_after_callbacks!(:after)
55
- end
56
-
57
- def after_response!
58
- run_after_callbacks!(:after_all)
59
- end
60
-
61
- def run_before_callbacks!(type)
62
- if type == :before_all
63
- self.class._callbacks[type].each { |callback| eval_callback(&callback[:block]) }
64
- else
65
- matching_callbacks(type) do |callback|
66
- eval_callback(&callback[:block])
67
- end
68
- end
69
- end
70
-
71
- def run_after_callbacks!(type)
72
- if type == :after_all
73
- self.class._callbacks[type].each { |callback| eval_callback(&callback[:block]) }
74
- else
75
- matching_callbacks(type) do |callback|
76
- eval_callback(&callback[:block])
77
- end
78
- end
79
- end
80
-
81
- def matching_callbacks(type)
82
- self.class._callbacks[type].select do |callback|
83
- paths = callback[:paths]
84
- if paths.detect { |path| path.match?(@request.path_info) }
85
- yield callback
86
- end
87
- end
88
- end
89
-
90
- def eval_callback(*args, &callback)
91
- instance_exec(*args, &callback)
92
- end
93
40
  end
94
41
  end
95
42
  end
@@ -1,3 +1,3 @@
1
1
  module Jeanine
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -1,7 +1,8 @@
1
+ require 'tilt'
2
+
1
3
  module Jeanine
4
+
2
5
  module ViewPaths
3
- def append_view_path(path)
4
- Jeanine.view_paths << path
5
- end
6
+
6
7
  end
7
8
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jeanine
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Brody
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: pry
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
97
111
  description: A framework.
98
112
  email:
99
113
  - git@josh.mn
@@ -113,7 +127,7 @@ files:
113
127
  - lib/jeanine/headers.rb
114
128
  - lib/jeanine/mimes.rb
115
129
  - lib/jeanine/path_proxy.rb
116
- - lib/jeanine/renderer.rb
130
+ - lib/jeanine/rendering.rb
117
131
  - lib/jeanine/request.rb
118
132
  - lib/jeanine/rescuing.rb
119
133
  - lib/jeanine/response.rb