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 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
- class MyApp < Happy::Controller
20
- route do
21
- 'Hello world'
22
- end
19
+ Happy.route do
20
+ 'Hello world'
23
21
  end
24
22
 
25
- run MyApp
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
- class MyApp < Happy::Controller
35
- route do
36
- # Set a default layout for all responses!
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
- # Happily deal with parameters contained in paths!
45
- path :name do
46
- # Just return a string to happily render it!
47
- "Hello, #{params['name']}"!
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
- "Silly user, didn't provide a name!"
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
- # Now let's do something a little bit more exciting!
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
- # If we reach this point, the request still hasn't been handled, so
81
- # the user must by trying to access the root URL. How about a redirect
82
- # to the URL of the previously invoked controller?
83
-
84
- redirect! articles.root_url
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
- # It's just a Rack app, so let's run it.
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, :response, :previous_path, :remaining_path
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
@@ -46,31 +46,20 @@ module Happy
46
46
  end
47
47
 
48
48
  def context
49
- @env['happy.context'] ||= self.class.context_class.from_env(@env)
49
+ @env['happy.context'] ||= Happy::Context.from_env(@env)
50
50
  end
51
51
 
52
52
  def route
53
- instance_exec(&self.class.route_blk) if self.class.route_blk
53
+ # override this in subclasses
54
54
  end
55
55
 
56
56
  class << self
57
- attr_reader :route_blk
58
-
59
- def route(&blk)
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.instance_eval(&blk) if blk
62
+ klass.class_eval(&blk) if blk
74
63
  end
75
64
  end
76
65
  end
data/lib/happy/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Happy
2
- VERSION = "0.1.0.pre13"
2
+ VERSION = "0.1.0.pre14"
3
3
  end
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
@@ -66,7 +66,9 @@ module Happy
66
66
  describe '#run' do
67
67
  it "passes control to another controller" do
68
68
  class InnerController < Controller
69
- route { 'awesome!' }
69
+ def route
70
+ 'awesome!'
71
+ end
70
72
  end
71
73
 
72
74
  def app
@@ -4,22 +4,20 @@ module Happy
4
4
  describe Controller::Routing do
5
5
  describe '#path' do
6
6
  subject do
7
- Controller.build do
8
- route do
9
- path('foo') { serve! 'bar' }
10
- path('one', 'two') { serve! 'onetwo' }
11
- path('hello') do
12
- path(:name) { serve! "Hello #{params['name']}" }
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
 
@@ -3,11 +3,13 @@ require 'spec_helper'
3
3
  module Happy
4
4
  describe Controller do
5
5
  subject do
6
- Controller.build do
7
- route do
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 { serve! "yay!" }
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.should_receive(:[]).twice.with('RACK_ENV').and_return('development')
10
+ ENV['RACK_ENV'] = 'development'
11
11
  Happy.env.should be_development
12
12
  Happy.env.should_not be_production
13
13
 
14
- ENV.should_receive(:[]).twice.with('RACK_ENV').and_return('production')
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
@@ -18,11 +18,7 @@ module SpecHelpers
18
18
  end
19
19
 
20
20
  def build_controller(&blk)
21
- Happy::Controller.build do
22
- route do
23
- instance_exec(&blk)
24
- end
25
- end
21
+ Happy.route(&blk)
26
22
  end
27
23
  end
28
24
 
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.pre13
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-06 00:00:00.000000000 Z
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: &70218841196860 !ruby/object:Gem::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: *70218841196860
24
+ version_requirements: *70317632674040
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rack
27
- requirement: &70218841195560 !ruby/object:Gem::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: *70218841195560
35
+ version_requirements: *70317632672560
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: happy-helpers
38
- requirement: &70218841194520 !ruby/object:Gem::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: *70218841194520
46
+ version_requirements: *70317632671280
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: allowance
49
- requirement: &70218841193840 !ruby/object:Gem::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: *70218841193840
57
+ version_requirements: *70317632685880
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: happy-cli
60
- requirement: &70218841193140 !ruby/object:Gem::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: *70218841193140
68
+ version_requirements: *70317632681080
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rake
71
- requirement: &70218841192140 !ruby/object:Gem::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: *70218841192140
79
+ version_requirements: *70317632678780
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rspec
82
- requirement: &70218841189600 !ruby/object:Gem::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: *70218841189600
90
+ version_requirements: *70317632703660
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: rspec-html-matchers
93
- requirement: &70218841188740 !ruby/object:Gem::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: *70218841188740
101
+ version_requirements: *70317632702220
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: rack-test
104
- requirement: &70218841187540 !ruby/object:Gem::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: *70218841187540
112
+ version_requirements: *70317632701180
113
113
  - !ruby/object:Gem::Dependency
114
114
  name: watchr
115
- requirement: &70218841186200 !ruby/object:Gem::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: *70218841186200
123
+ version_requirements: *70317632700160
124
124
  description: A happy little toolkit for writing web applications.
125
125
  email:
126
126
  - hendrik@mans.de