happy 0.1.0.pre13 → 0.1.0.pre14
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +48 -53
- data/lib/happy/context.rb +2 -6
- data/lib/happy/controller.rb +6 -17
- data/lib/happy/version.rb +1 -1
- data/lib/happy.rb +20 -0
- data/spec/controller/actions_spec.rb +3 -1
- data/spec/controller/routing_spec.rb +13 -15
- data/spec/controller_spec.rb +7 -3
- data/spec/happy_spec.rb +2 -2
- data/spec/spec_helper.rb +1 -5
- metadata +22 -22
data/README.md
CHANGED
@@ -16,13 +16,11 @@ Furthermore, the way Happy handles incoming requests is vastly different from ho
|
|
16
16
|
# config.ru
|
17
17
|
require 'happy'
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
'Hello world'
|
22
|
-
end
|
19
|
+
Happy.route do
|
20
|
+
'Hello world'
|
23
21
|
end
|
24
22
|
|
25
|
-
run
|
23
|
+
run Happy
|
26
24
|
```
|
27
25
|
|
28
26
|
How about something a little bit closer to reality?
|
@@ -31,62 +29,59 @@ How about something a little bit closer to reality?
|
|
31
29
|
# config.ru
|
32
30
|
require 'happy'
|
33
31
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
layout 'application.erb'
|
38
|
-
|
39
|
-
# Happily deal with sub-paths!
|
40
|
-
path 'hello' do
|
41
|
-
# Let's use a different layout here.
|
42
|
-
layout 'hello_layout.erb'
|
32
|
+
Happy.route do
|
33
|
+
# Set a default layout for all responses!
|
34
|
+
layout 'application.erb'
|
43
35
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
end
|
36
|
+
# Happily deal with sub-paths!
|
37
|
+
path 'hello' do
|
38
|
+
# Let's use a different layout here.
|
39
|
+
layout 'hello_layout.erb'
|
49
40
|
|
50
|
-
|
41
|
+
# Happily deal with parameters contained in paths!
|
42
|
+
path :name do
|
43
|
+
# Just return a string to happily render it!
|
44
|
+
"Hello, #{params['name']}"!
|
51
45
|
end
|
52
46
|
|
53
|
-
|
54
|
-
|
55
|
-
# How about passing control to another controller? In this instance,
|
56
|
-
# we're invoking an instance of ResourceMounter, a controller class
|
57
|
-
# that serves a model resource RESTful-Rails-style. We'll save a
|
58
|
-
# reference to the controller for later.
|
59
|
-
|
60
|
-
articles = ResourceMounter.new(:class => Article)
|
61
|
-
articles.perform
|
62
|
-
|
63
|
-
# Or use the shortcut: invoke :resource_mounter, :class => Article
|
64
|
-
|
65
|
-
# This block of code is executed for every request, so you can do
|
66
|
-
# some crazy stuff here, including only defining specific paths
|
67
|
-
# when certain conditions are given. Just write Ruby! :)
|
68
|
-
|
69
|
-
if context.user_is_admin?
|
70
|
-
path 'delete_everything' do
|
71
|
-
Article.delete_all
|
72
|
-
|
73
|
-
# How about rendering a view template and passing
|
74
|
-
# variables to it?
|
75
|
-
render 'admin_message.erb',
|
76
|
-
:message => 'You just deleted everything. Grats!'
|
77
|
-
end
|
78
|
-
end
|
47
|
+
"Silly user, didn't provide a name!"
|
48
|
+
end
|
79
49
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
50
|
+
# Now let's do something a little bit more exciting!
|
51
|
+
#
|
52
|
+
# How about passing control to another controller? In this instance,
|
53
|
+
# we're invoking an instance of ResourceMounter, a controller class
|
54
|
+
# that serves a model resource RESTful-Rails-style. We'll save a
|
55
|
+
# reference to the controller for later.
|
56
|
+
|
57
|
+
articles = ResourceMounter.new(:class => Article)
|
58
|
+
articles.perform
|
59
|
+
|
60
|
+
# Or use the shortcut: invoke :resource_mounter, :class => Article
|
61
|
+
|
62
|
+
# This block of code is executed for every request, so you can do
|
63
|
+
# some crazy stuff here, including only defining specific paths
|
64
|
+
# when certain conditions are given. Just write Ruby! :)
|
65
|
+
|
66
|
+
if context.user_is_admin?
|
67
|
+
path 'delete_everything' do
|
68
|
+
Article.delete_all
|
69
|
+
|
70
|
+
# How about rendering a view template and passing
|
71
|
+
# variables to it?
|
72
|
+
render 'admin_message.erb',
|
73
|
+
:message => 'You just deleted everything. Grats!'
|
74
|
+
end
|
85
75
|
end
|
76
|
+
|
77
|
+
# If we reach this point, the request still hasn't been handled, so
|
78
|
+
# the user must by trying to access the root URL. How about a redirect
|
79
|
+
# to the URL of the previously invoked controller?
|
80
|
+
|
81
|
+
redirect! articles.root_url
|
86
82
|
end
|
87
83
|
|
88
|
-
|
89
|
-
run MyApp
|
84
|
+
run Happy
|
90
85
|
```
|
91
86
|
|
92
87
|
|
data/lib/happy/context.rb
CHANGED
@@ -5,8 +5,8 @@ module Happy
|
|
5
5
|
class Context
|
6
6
|
include Helpers
|
7
7
|
|
8
|
-
attr_reader :request, :
|
9
|
-
attr_accessor :layout, :controller
|
8
|
+
attr_reader :request, :previous_path, :remaining_path
|
9
|
+
attr_accessor :layout, :controller, :response
|
10
10
|
delegate :params, :session, :to => :request
|
11
11
|
|
12
12
|
def initialize(request, response)
|
@@ -30,10 +30,6 @@ module Happy
|
|
30
30
|
self.controller = old_controller
|
31
31
|
end
|
32
32
|
|
33
|
-
def response=(r)
|
34
|
-
@response = r
|
35
|
-
end
|
36
|
-
|
37
33
|
private
|
38
34
|
|
39
35
|
class << self
|
data/lib/happy/controller.rb
CHANGED
@@ -46,31 +46,20 @@ module Happy
|
|
46
46
|
end
|
47
47
|
|
48
48
|
def context
|
49
|
-
@env['happy.context'] ||=
|
49
|
+
@env['happy.context'] ||= Happy::Context.from_env(@env)
|
50
50
|
end
|
51
51
|
|
52
52
|
def route
|
53
|
-
|
53
|
+
# override this in subclasses
|
54
54
|
end
|
55
55
|
|
56
56
|
class << self
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
@route_blk = blk
|
61
|
-
end
|
62
|
-
|
63
|
-
def context(&blk)
|
64
|
-
context_class.class_exec(&blk)
|
65
|
-
end
|
66
|
-
|
67
|
-
def context_class
|
68
|
-
Happy::Context
|
69
|
-
end
|
70
|
-
|
57
|
+
# Create a new subclass of Happy::Controller, using the provided
|
58
|
+
# block for defining class methods et al.
|
59
|
+
#
|
71
60
|
def build(&blk)
|
72
61
|
Class.new(self).tap do |klass|
|
73
|
-
klass.
|
62
|
+
klass.class_eval(&blk) if blk
|
74
63
|
end
|
75
64
|
end
|
76
65
|
end
|
data/lib/happy/version.rb
CHANGED
data/lib/happy.rb
CHANGED
@@ -11,4 +11,24 @@ module Happy
|
|
11
11
|
def self.env
|
12
12
|
ActiveSupport::StringInquirer.new(ENV['RACK_ENV'] || 'development')
|
13
13
|
end
|
14
|
+
|
15
|
+
# Creates a new Happy::Controller class, using the provided block as
|
16
|
+
# its routing block.
|
17
|
+
#
|
18
|
+
def self.route(&blk)
|
19
|
+
@last_controller_class_created = Class.new(Happy::Controller).tap do |klass|
|
20
|
+
klass.send(:define_method, :route, &blk)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# Run the provided block against Happy::Context. Use this to add new
|
25
|
+
# methods to the request context class.
|
26
|
+
#
|
27
|
+
def self.context(&blk)
|
28
|
+
Context.class_exec(&blk)
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.call(env)
|
32
|
+
@last_controller_class_created.try(:call, env) or raise "Please use Happy.route to define some routes."
|
33
|
+
end
|
14
34
|
end
|
@@ -4,22 +4,20 @@ module Happy
|
|
4
4
|
describe Controller::Routing do
|
5
5
|
describe '#path' do
|
6
6
|
subject do
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
path('
|
12
|
-
|
13
|
-
serve! "Please provide a name."
|
14
|
-
end
|
15
|
-
path('number-:num') { serve! "num = #{params['num']}" }
|
16
|
-
path('return-value') { 'moo?' }
|
17
|
-
path('resource') do
|
18
|
-
get { 'GET resource' }
|
19
|
-
post { 'POST resource' }
|
20
|
-
end
|
21
|
-
serve! "root"
|
7
|
+
Happy.route do
|
8
|
+
path('foo') { serve! 'bar' }
|
9
|
+
path('one', 'two') { serve! 'onetwo' }
|
10
|
+
path('hello') do
|
11
|
+
path(:name) { serve! "Hello #{params['name']}" }
|
12
|
+
serve! "Please provide a name."
|
22
13
|
end
|
14
|
+
path('number-:num') { serve! "num = #{params['num']}" }
|
15
|
+
path('return-value') { 'moo?' }
|
16
|
+
path('resource') do
|
17
|
+
get { 'GET resource' }
|
18
|
+
post { 'POST resource' }
|
19
|
+
end
|
20
|
+
serve! "root"
|
23
21
|
end
|
24
22
|
end
|
25
23
|
|
data/spec/controller_spec.rb
CHANGED
@@ -3,11 +3,13 @@ require 'spec_helper'
|
|
3
3
|
module Happy
|
4
4
|
describe Controller do
|
5
5
|
subject do
|
6
|
-
Controller
|
7
|
-
route
|
6
|
+
class MyController < Happy::Controller
|
7
|
+
def route
|
8
8
|
serve! "it works"
|
9
9
|
end
|
10
10
|
end
|
11
|
+
|
12
|
+
MyController
|
11
13
|
end
|
12
14
|
|
13
15
|
it "is mountable as a Rack app" do
|
@@ -19,7 +21,9 @@ module Happy
|
|
19
21
|
describe ".build" do
|
20
22
|
subject do
|
21
23
|
Controller.build do
|
22
|
-
route
|
24
|
+
def route
|
25
|
+
serve! "yay!"
|
26
|
+
end
|
23
27
|
end
|
24
28
|
end
|
25
29
|
|
data/spec/happy_spec.rb
CHANGED
@@ -7,11 +7,11 @@ describe Happy do
|
|
7
7
|
end
|
8
8
|
|
9
9
|
it "provides #development?, #production? etc." do
|
10
|
-
ENV
|
10
|
+
ENV['RACK_ENV'] = 'development'
|
11
11
|
Happy.env.should be_development
|
12
12
|
Happy.env.should_not be_production
|
13
13
|
|
14
|
-
ENV
|
14
|
+
ENV['RACK_ENV'] = 'production'
|
15
15
|
Happy.env.should be_production
|
16
16
|
Happy.env.should_not be_development
|
17
17
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: happy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.0.
|
4
|
+
version: 0.1.0.pre14
|
5
5
|
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-06-
|
12
|
+
date: 2012-06-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
16
|
-
requirement: &
|
16
|
+
requirement: &70317632674040 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '3.1'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *70317632674040
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: rack
|
27
|
-
requirement: &
|
27
|
+
requirement: &70317632672560 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ~>
|
@@ -32,10 +32,10 @@ dependencies:
|
|
32
32
|
version: '1.4'
|
33
33
|
type: :runtime
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70317632672560
|
36
36
|
- !ruby/object:Gem::Dependency
|
37
37
|
name: happy-helpers
|
38
|
-
requirement: &
|
38
|
+
requirement: &70317632671280 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
41
|
- - ~>
|
@@ -43,10 +43,10 @@ dependencies:
|
|
43
43
|
version: 0.1.0.pre9
|
44
44
|
type: :runtime
|
45
45
|
prerelease: false
|
46
|
-
version_requirements: *
|
46
|
+
version_requirements: *70317632671280
|
47
47
|
- !ruby/object:Gem::Dependency
|
48
48
|
name: allowance
|
49
|
-
requirement: &
|
49
|
+
requirement: &70317632685880 !ruby/object:Gem::Requirement
|
50
50
|
none: false
|
51
51
|
requirements:
|
52
52
|
- - ! '>='
|
@@ -54,10 +54,10 @@ dependencies:
|
|
54
54
|
version: 0.1.1
|
55
55
|
type: :runtime
|
56
56
|
prerelease: false
|
57
|
-
version_requirements: *
|
57
|
+
version_requirements: *70317632685880
|
58
58
|
- !ruby/object:Gem::Dependency
|
59
59
|
name: happy-cli
|
60
|
-
requirement: &
|
60
|
+
requirement: &70317632681080 !ruby/object:Gem::Requirement
|
61
61
|
none: false
|
62
62
|
requirements:
|
63
63
|
- - ! '>='
|
@@ -65,10 +65,10 @@ dependencies:
|
|
65
65
|
version: 0.1.0.pre1
|
66
66
|
type: :runtime
|
67
67
|
prerelease: false
|
68
|
-
version_requirements: *
|
68
|
+
version_requirements: *70317632681080
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: rake
|
71
|
-
requirement: &
|
71
|
+
requirement: &70317632678780 !ruby/object:Gem::Requirement
|
72
72
|
none: false
|
73
73
|
requirements:
|
74
74
|
- - ! '>='
|
@@ -76,10 +76,10 @@ dependencies:
|
|
76
76
|
version: '0'
|
77
77
|
type: :development
|
78
78
|
prerelease: false
|
79
|
-
version_requirements: *
|
79
|
+
version_requirements: *70317632678780
|
80
80
|
- !ruby/object:Gem::Dependency
|
81
81
|
name: rspec
|
82
|
-
requirement: &
|
82
|
+
requirement: &70317632703660 !ruby/object:Gem::Requirement
|
83
83
|
none: false
|
84
84
|
requirements:
|
85
85
|
- - ~>
|
@@ -87,10 +87,10 @@ dependencies:
|
|
87
87
|
version: '2.8'
|
88
88
|
type: :development
|
89
89
|
prerelease: false
|
90
|
-
version_requirements: *
|
90
|
+
version_requirements: *70317632703660
|
91
91
|
- !ruby/object:Gem::Dependency
|
92
92
|
name: rspec-html-matchers
|
93
|
-
requirement: &
|
93
|
+
requirement: &70317632702220 !ruby/object:Gem::Requirement
|
94
94
|
none: false
|
95
95
|
requirements:
|
96
96
|
- - ! '>='
|
@@ -98,10 +98,10 @@ dependencies:
|
|
98
98
|
version: '0'
|
99
99
|
type: :development
|
100
100
|
prerelease: false
|
101
|
-
version_requirements: *
|
101
|
+
version_requirements: *70317632702220
|
102
102
|
- !ruby/object:Gem::Dependency
|
103
103
|
name: rack-test
|
104
|
-
requirement: &
|
104
|
+
requirement: &70317632701180 !ruby/object:Gem::Requirement
|
105
105
|
none: false
|
106
106
|
requirements:
|
107
107
|
- - ! '>='
|
@@ -109,10 +109,10 @@ dependencies:
|
|
109
109
|
version: '0'
|
110
110
|
type: :development
|
111
111
|
prerelease: false
|
112
|
-
version_requirements: *
|
112
|
+
version_requirements: *70317632701180
|
113
113
|
- !ruby/object:Gem::Dependency
|
114
114
|
name: watchr
|
115
|
-
requirement: &
|
115
|
+
requirement: &70317632700160 !ruby/object:Gem::Requirement
|
116
116
|
none: false
|
117
117
|
requirements:
|
118
118
|
- - ! '>='
|
@@ -120,7 +120,7 @@ dependencies:
|
|
120
120
|
version: '0'
|
121
121
|
type: :development
|
122
122
|
prerelease: false
|
123
|
-
version_requirements: *
|
123
|
+
version_requirements: *70317632700160
|
124
124
|
description: A happy little toolkit for writing web applications.
|
125
125
|
email:
|
126
126
|
- hendrik@mans.de
|