happy 0.1.0.pre20 → 0.1.0.pre21

Sign up to get free protection for your applications and to get access to all the features.
data/example/config.ru CHANGED
@@ -1,19 +1,26 @@
1
+ # Just run this script through the 'rackup' command.
2
+
1
3
  lib_path = File.expand_path("#{File.dirname(__FILE__)}/../lib")
2
4
  $LOAD_PATH.unshift lib_path unless $LOAD_PATH.include?(lib_path)
3
5
 
4
6
  require 'happy'
5
- require 'cgi'
6
7
 
7
- class TestApp < Happy::Controller
8
- def examples; @examples ||= {}; end
8
+ # Controllers are the core building blocks of Happy applications.
9
+ # They're also just Rack apps, so in any Happy app, you will
10
+ # declare at least a "root" controller class and run that through Rack.
9
11
 
10
- def example(name, path_name = nil, &blk)
11
- path_name ||= name.parameterize
12
- examples[name] = path_name
12
+ class TestApp < Happy::Controller
13
13
 
14
- # Create a path containing the example's code block
15
- path(path_name, &blk)
16
- end
14
+ # A controller's most important method is #route. Whenever a request hits
15
+ # your application, a new instance of your root controller class is
16
+ # created, and its #route method is called.
17
+ #
18
+ # This method can be as simple or complex as you like. In this example
19
+ # application, it wraps around a couple of annotated Happy examples.
20
+ # Usually, you'd use the #path method to specify code to be executed if
21
+ # a certain path was requested; in this app, we're using a custom #example
22
+ # method, that does the same thing but also records the example in a hash
23
+ # so we can generated a "table of contents" in index.erb.
17
24
 
18
25
  def route
19
26
  example 'Returning just a string' do
@@ -25,6 +32,11 @@ class TestApp < Happy::Controller
25
32
  serve! "I'm not being served, since the above call to #serve! halted processing."
26
33
  end
27
34
 
35
+ example 'Content-type' do
36
+ content_type 'text/css'
37
+ "/* I'm CSS! */\n\nbody { color: red }\n"
38
+ end
39
+
28
40
  example 'Path parameters' do
29
41
  path 'hello' do
30
42
  path :name do
@@ -100,6 +112,16 @@ class TestApp < Happy::Controller
100
112
 
101
113
  render 'index.erb'
102
114
  end
115
+
116
+ def examples; @examples ||= {}; end
117
+
118
+ def example(name, path_name = nil, &blk)
119
+ path_name ||= name.parameterize
120
+ examples[name] = path_name
121
+
122
+ # Create a path containing the example's code block
123
+ path(path_name, &blk)
124
+ end
103
125
  end
104
126
 
105
127
  run TestApp
data/happy.gemspec CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |gem|
20
20
  gem.add_dependency 'happy-helpers', '~> 0.1.0.pre11'
21
21
  gem.add_dependency 'allowance', '>= 0.1.1'
22
22
 
23
- gem.add_dependency 'happy-cli', '>= 0.1.0.pre1'
23
+ # gem.add_dependency 'happy-cli', '>= 0.1.0.pre1'
24
24
 
25
25
  gem.add_development_dependency 'rake'
26
26
  gem.add_development_dependency 'rspec', '~> 2.8'
@@ -3,17 +3,21 @@ module Happy
3
3
  module Configurable
4
4
  extend ActiveSupport::Concern
5
5
 
6
- def config
7
- self.class.config
6
+ def options
7
+ @options ||= self.class.options.dup
8
+ end
9
+
10
+ def set(k, v)
11
+ options[k.to_sym] = v
8
12
  end
9
13
 
10
14
  module ClassMethods
11
- def config
12
- @config ||= {}
15
+ def options
16
+ @options ||= {}
13
17
  end
14
18
 
15
19
  def set(k, v)
16
- config[k.to_sym] = v
20
+ options[k.to_sym] = v
17
21
  end
18
22
  end
19
23
  end
@@ -2,9 +2,16 @@ require 'happy-helpers'
2
2
 
3
3
  module Happy
4
4
  class Controller
5
+ # A collection of useful helper methods.
6
+ #
5
7
  module Helpers
8
+ # Load a whole bunch of helpers fromi HappyHelpers. This includes stuff
9
+ # like url_for, link_to and more.
6
10
  include HappyHelpers::Helpers
7
11
 
12
+ # Renders "something". This method takes a closer look at what this
13
+ # "something" is and then dispatches to a more specific method.
14
+ #
8
15
  def render(what, options = {}, &blk)
9
16
  case what
10
17
  when NilClass then ''
@@ -14,11 +21,15 @@ module Happy
14
21
  end
15
22
  end
16
23
 
24
+ # Render a template from the controller's view folder.
25
+ #
17
26
  def render_template(name, variables = {}, &blk)
18
- path = @controller ? @controller.config[:views] : './views'
27
+ path = options[:views] || './views'
19
28
  HappyHelpers::Templates.render(File.join(path, name), self, variables, &blk)
20
29
  end
21
30
 
31
+ # Render a resource.
32
+ #
22
33
  def render_resource(resource, options = {})
23
34
  # build name strings
24
35
  singular_name = resource.class.to_s.tableize.singularize
@@ -24,7 +24,9 @@ module Happy
24
24
  include Permissions
25
25
  include Helpers
26
26
 
27
- attr_reader :options, :env, :unprocessed_path, :processed_path
27
+ attr_reader :env
28
+
29
+ CASCADING_OPTIONS = [:views]
28
30
 
29
31
  # Creates a new instance of {Controller}. When a block is provided,
30
32
  # it is run against the new instance, allowing custom controller classes
@@ -35,7 +37,7 @@ module Happy
35
37
  # @param options [Hash]
36
38
  # Controller options.
37
39
  #
38
- def initialize(env_or_parent = {}, options = {}, &blk)
40
+ def initialize(env_or_parent = {}, opts = {}, &blk)
39
41
  if env_or_parent.is_a?(Happy::Controller)
40
42
  @parent_controller = env_or_parent
41
43
  @env = @parent_controller.env
@@ -47,7 +49,15 @@ module Happy
47
49
  @processed_path = []
48
50
  end
49
51
 
50
- @options = options
52
+ # Augment this instance's options hash with the options given to this constructor
53
+ options.merge!(opts)
54
+
55
+ # Copy missing options from our parent
56
+ if @parent_controller
57
+ CASCADING_OPTIONS.each do |opt|
58
+ options[opt] ||= @parent_controller.options[opt]
59
+ end
60
+ end
51
61
 
52
62
  # Save a copy of the current path as this controller's root path.
53
63
  @root_url = processed_path.join('/')
@@ -67,6 +77,8 @@ module Happy
67
77
 
68
78
  protected
69
79
 
80
+ attr_reader :unprocessed_path, :processed_path
81
+
70
82
  def root_url(*extras)
71
83
  url_for(@root_url, extras)
72
84
  end
@@ -92,10 +92,8 @@ module Happy
92
92
  end
93
93
 
94
94
  def route
95
- @options = {
96
- :singular_name => options[:class].to_s.tableize.singularize,
97
- :plural_name => options[:class].to_s.tableize.pluralize
98
- }.merge(@options)
95
+ options[:singular_name] ||= options[:class].to_s.tableize.singularize
96
+ options[:plural_name] ||= options[:class].to_s.tableize.pluralize
99
97
 
100
98
  path options[:plural_name] do
101
99
  get('new') { do_new }
data/lib/happy/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Happy
2
- VERSION = "0.1.0.pre20"
2
+ VERSION = "0.1.0.pre21"
3
3
  end
@@ -0,0 +1,59 @@
1
+ require 'spec_helper'
2
+
3
+ module Happy
4
+ describe Controller::Configurable do
5
+ class TestController < Happy::Controller
6
+ set :foo, 'bar'
7
+ end
8
+
9
+ describe '.set' do
10
+ it 'sets a class-level option' do
11
+ TestController.options[:foo].should == 'bar'
12
+ end
13
+ end
14
+
15
+ describe '#set' do
16
+ before do
17
+ @instance = TestController.new
18
+ @instance.set :foo, 'baz'
19
+ end
20
+
21
+ it 'sets an instance-level option, overriding the class default' do
22
+ @instance.options[:foo].should == 'baz'
23
+ end
24
+
25
+ it "doesn't modify the class-level default option" do
26
+ TestController.options[:foo].should == 'bar'
27
+ end
28
+ end
29
+
30
+ describe 'class-level options' do
31
+ it 'are the defaults for instance-level options' do
32
+ TestController.new.options[:foo].should == 'bar'
33
+ end
34
+ end
35
+
36
+ describe 'cascading options' do
37
+ class OuterController < Controller
38
+ set :views, './foo/'
39
+ set :foo, 'bar'
40
+ end
41
+
42
+ class InnerController < Controller
43
+ end
44
+
45
+ it "are copied from the parent controller if necessary" do
46
+ @instance = InnerController.new(OuterController.new)
47
+ @instance.options[:views].should == './foo/'
48
+ @instance.options[:foo].should be_nil
49
+ end
50
+ end
51
+
52
+ describe 'options passed to the initializer' do
53
+ it "override default options" do
54
+ @instance = TestController.new({}, :foo => 'baz')
55
+ @instance.options[:foo].should == 'baz'
56
+ end
57
+ end
58
+ end
59
+ end
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.pre20
4
+ version: 0.1.0.pre21
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-06-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
16
- requirement: &70246363046260 !ruby/object:Gem::Requirement
16
+ requirement: &70146774651160 !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: *70246363046260
24
+ version_requirements: *70146774651160
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rack
27
- requirement: &70246363043080 !ruby/object:Gem::Requirement
27
+ requirement: &70146774650120 !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: *70246363043080
35
+ version_requirements: *70146774650120
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: happy-helpers
38
- requirement: &70246363042320 !ruby/object:Gem::Requirement
38
+ requirement: &70146774648720 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ~>
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: 0.1.0.pre11
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70246363042320
46
+ version_requirements: *70146774648720
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: allowance
49
- requirement: &70246363058000 !ruby/object:Gem::Requirement
49
+ requirement: &70146774646980 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,21 +54,10 @@ dependencies:
54
54
  version: 0.1.1
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70246363058000
58
- - !ruby/object:Gem::Dependency
59
- name: happy-cli
60
- requirement: &70246363056880 !ruby/object:Gem::Requirement
61
- none: false
62
- requirements:
63
- - - ! '>='
64
- - !ruby/object:Gem::Version
65
- version: 0.1.0.pre1
66
- type: :runtime
67
- prerelease: false
68
- version_requirements: *70246363056880
57
+ version_requirements: *70146774646980
69
58
  - !ruby/object:Gem::Dependency
70
59
  name: rake
71
- requirement: &70246363056380 !ruby/object:Gem::Requirement
60
+ requirement: &70146774662260 !ruby/object:Gem::Requirement
72
61
  none: false
73
62
  requirements:
74
63
  - - ! '>='
@@ -76,10 +65,10 @@ dependencies:
76
65
  version: '0'
77
66
  type: :development
78
67
  prerelease: false
79
- version_requirements: *70246363056380
68
+ version_requirements: *70146774662260
80
69
  - !ruby/object:Gem::Dependency
81
70
  name: rspec
82
- requirement: &70246363055680 !ruby/object:Gem::Requirement
71
+ requirement: &70146774660060 !ruby/object:Gem::Requirement
83
72
  none: false
84
73
  requirements:
85
74
  - - ~>
@@ -87,10 +76,10 @@ dependencies:
87
76
  version: '2.8'
88
77
  type: :development
89
78
  prerelease: false
90
- version_requirements: *70246363055680
79
+ version_requirements: *70146774660060
91
80
  - !ruby/object:Gem::Dependency
92
81
  name: rspec-html-matchers
93
- requirement: &70246363054880 !ruby/object:Gem::Requirement
82
+ requirement: &70146774658460 !ruby/object:Gem::Requirement
94
83
  none: false
95
84
  requirements:
96
85
  - - ! '>='
@@ -98,10 +87,10 @@ dependencies:
98
87
  version: '0'
99
88
  type: :development
100
89
  prerelease: false
101
- version_requirements: *70246363054880
90
+ version_requirements: *70146774658460
102
91
  - !ruby/object:Gem::Dependency
103
92
  name: rack-test
104
- requirement: &70246363054000 !ruby/object:Gem::Requirement
93
+ requirement: &70146774657580 !ruby/object:Gem::Requirement
105
94
  none: false
106
95
  requirements:
107
96
  - - ! '>='
@@ -109,10 +98,10 @@ dependencies:
109
98
  version: '0'
110
99
  type: :development
111
100
  prerelease: false
112
- version_requirements: *70246363054000
101
+ version_requirements: *70146774657580
113
102
  - !ruby/object:Gem::Dependency
114
103
  name: watchr
115
- requirement: &70246363053540 !ruby/object:Gem::Requirement
104
+ requirement: &70146774656440 !ruby/object:Gem::Requirement
116
105
  none: false
117
106
  requirements:
118
107
  - - ! '>='
@@ -120,7 +109,7 @@ dependencies:
120
109
  version: '0'
121
110
  type: :development
122
111
  prerelease: false
123
- version_requirements: *70246363053540
112
+ version_requirements: *70146774656440
124
113
  description: A happy little toolkit for writing web applications.
125
114
  email:
126
115
  - hendrik@mans.de
@@ -161,6 +150,7 @@ files:
161
150
  - lib/happy/version.rb
162
151
  - spec/controller/actions_spec.rb
163
152
  - spec/controller/cascadable_spec.rb
153
+ - spec/controller/configurable_spec.rb
164
154
  - spec/controller/routing_spec.rb
165
155
  - spec/controller_spec.rb
166
156
  - spec/happy_spec.rb
@@ -192,6 +182,7 @@ summary: A happy little toolkit for writing web applications.
192
182
  test_files:
193
183
  - spec/controller/actions_spec.rb
194
184
  - spec/controller/cascadable_spec.rb
185
+ - spec/controller/configurable_spec.rb
195
186
  - spec/controller/routing_spec.rb
196
187
  - spec/controller_spec.rb
197
188
  - spec/happy_spec.rb