sinatra-extension 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +27 -0
- data/README.md +40 -0
- data/lib/sinatra/extension.rb +77 -0
- data/spec/sinatra/extension_spec.rb +68 -0
- data/spec/spec_helper.rb +2 -0
- metadata +149 -0
data/LICENSE
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
copyright (c) 2010 Konstantin Haase. All rights reserved.
|
2
|
+
|
3
|
+
Developed by: Konstantin Haase
|
4
|
+
http://github.com/rkh/big_band
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
of this software and associated documentation files (the "Software"), to
|
8
|
+
deal with the Software without restriction, including without limitation the
|
9
|
+
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
10
|
+
sell copies of the Software, and to permit persons to whom the Software is
|
11
|
+
furnished to do so, subject to the following conditions:
|
12
|
+
1. Redistributions of source code must retain the above copyright notice,
|
13
|
+
this list of conditions and the following disclaimers.
|
14
|
+
2. Redistributions in binary form must reproduce the above copyright
|
15
|
+
notice, this list of conditions and the following disclaimers in the
|
16
|
+
documentation and/or other materials provided with the distribution.
|
17
|
+
3. Neither the name of Konstantin Haase, nor the names of other contributors
|
18
|
+
may be used to endorse or promote products derived from this Software without
|
19
|
+
specific prior written permission.
|
20
|
+
|
21
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
22
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
23
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
24
|
+
CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
25
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
26
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
27
|
+
WITH THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
Sinatra::Extension
|
2
|
+
==================
|
3
|
+
|
4
|
+
Mixin to ease [Sinatra](http://sinatrarb.com) extension development.
|
5
|
+
|
6
|
+
BigBand
|
7
|
+
-------
|
8
|
+
|
9
|
+
Sinatra::Extension is part of the [BigBand](http://github.com/rkh/big_band) stack.
|
10
|
+
Check it out if you are looking for other fancy Sinatra extensions.
|
11
|
+
|
12
|
+
Installation
|
13
|
+
------------
|
14
|
+
|
15
|
+
gem install sinatra-extension
|
16
|
+
|
17
|
+
Example
|
18
|
+
-------
|
19
|
+
|
20
|
+
module MyFancyExtension
|
21
|
+
extend Sinatra::Extension
|
22
|
+
|
23
|
+
enable :session
|
24
|
+
|
25
|
+
get '/foo' do
|
26
|
+
"bar"
|
27
|
+
end
|
28
|
+
|
29
|
+
enabled :fancy_mode do
|
30
|
+
use VeryFancyMiddleware
|
31
|
+
get '/fancy' do
|
32
|
+
"fancy!"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
class MyApp < Sinatra::Base
|
38
|
+
register MyFancyExtension
|
39
|
+
enable :fancy_mode
|
40
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'sinatra/base'
|
2
|
+
require 'sinatra/sugar'
|
3
|
+
require 'monkey-lib'
|
4
|
+
|
5
|
+
module Sinatra
|
6
|
+
module Delegator
|
7
|
+
def self.delegate(*methods)
|
8
|
+
methods.each do |method_name|
|
9
|
+
eval <<-RUBY, binding, '(__DELEGATE__)', 1
|
10
|
+
def #{method_name}(*args, &b) sinatra_application.send(#{method_name.inspect}, *args, &b) end
|
11
|
+
private #{method_name.inspect}
|
12
|
+
RUBY
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
delegate(*private_instance_methods(false))
|
17
|
+
|
18
|
+
def sinatra_application
|
19
|
+
::Sinatra::Application
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
module Extension
|
24
|
+
BasicObject = Object unless defined? BasicObject
|
25
|
+
class MethodRecorder < BasicObject
|
26
|
+
def initialize(list) @calls = list end
|
27
|
+
def method_missing(*a, &b) @calls << [a, b] end
|
28
|
+
end
|
29
|
+
|
30
|
+
include Delegator
|
31
|
+
|
32
|
+
def method_calls
|
33
|
+
@method_calls ||= []
|
34
|
+
end
|
35
|
+
|
36
|
+
def register_hooks
|
37
|
+
@register_hooks ||= []
|
38
|
+
end
|
39
|
+
|
40
|
+
def on_register(&block)
|
41
|
+
register_hooks << block
|
42
|
+
end
|
43
|
+
|
44
|
+
def enabled(option, &block)
|
45
|
+
mod = Module.new
|
46
|
+
mod.extend Sinatra::Extension
|
47
|
+
define_method(option) { nil }
|
48
|
+
define_method("#{option}?") { false }
|
49
|
+
define_method("#{option}=") do |value|
|
50
|
+
metadef(option) { value }
|
51
|
+
return value unless value
|
52
|
+
metadef("#{option}?") { true }
|
53
|
+
instance_yield block
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def register(*extensions)
|
58
|
+
on_register { register(*extensions) }
|
59
|
+
end
|
60
|
+
|
61
|
+
def configure(*args, &block)
|
62
|
+
on_register do
|
63
|
+
configure(*args) { |klass| klass.instance_yield block }
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def registered(klass)
|
68
|
+
register Sinatra::Sugar
|
69
|
+
method_calls.each { |a,b| klass.__send__(*a, &b) }
|
70
|
+
register_hooks.each { |hook| klass.instance_yield hook }
|
71
|
+
end
|
72
|
+
|
73
|
+
def sinatra_application
|
74
|
+
@sinatra_application ||= MethodRecorder.new method_calls
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require File.expand_path("../../spec_helper", __FILE__)
|
2
|
+
|
3
|
+
module Sinatra
|
4
|
+
module OtherExtension
|
5
|
+
end
|
6
|
+
|
7
|
+
module ExampleExtension
|
8
|
+
extend Sinatra::Extension
|
9
|
+
register OtherExtension
|
10
|
+
|
11
|
+
%w[get put post delete].each { |verb| __send__(verb, '/foo') { 'bar' } }
|
12
|
+
|
13
|
+
configure(:test) { set :foo, 42 }
|
14
|
+
configure(:development) { set :foo, 'oh no' }
|
15
|
+
|
16
|
+
on_register do
|
17
|
+
set :bar,:blah
|
18
|
+
end
|
19
|
+
|
20
|
+
helpers do
|
21
|
+
def foo(value) value end
|
22
|
+
end
|
23
|
+
|
24
|
+
enabled :special_foo do
|
25
|
+
helpers do
|
26
|
+
def foo(value) 42 end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe Sinatra::Extension do
|
33
|
+
before { app :ExampleExtension }
|
34
|
+
it_should_behave_like 'sinatra'
|
35
|
+
|
36
|
+
it 'should forward register correctly' do
|
37
|
+
app.should be_a(Sinatra::OtherExtension)
|
38
|
+
end
|
39
|
+
|
40
|
+
%w[head get put post delete].each do |verb|
|
41
|
+
it "should forward #{verb} correctly" do
|
42
|
+
browse_route(verb, '/foo').should be_ok
|
43
|
+
last_response.body.should == 'bar' unless verb == 'head'
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should forward configure correctly" do
|
48
|
+
app.should be_test # just to be sure
|
49
|
+
app.foo.should == 42
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should trigger on_register" do
|
53
|
+
app.bar.should == :blah
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should forward helpers correctly" do
|
57
|
+
app.new.foo(10).should == 10
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should apply enabled blocks only if given option has been enabled" do
|
61
|
+
app.should_not be_special_foo
|
62
|
+
app.special_foo.should be_nil
|
63
|
+
app.new.foo(10).should == 10
|
64
|
+
app.enable :special_foo
|
65
|
+
app.should be_special_foo
|
66
|
+
app.new.foo(10).should == 42
|
67
|
+
end
|
68
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,149 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra-extension
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 15
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 4
|
9
|
+
- 0
|
10
|
+
version: 0.4.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Konstantin Haase
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-06-07 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: monkey-lib
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 15
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
- 4
|
33
|
+
- 0
|
34
|
+
version: 0.4.0
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: sinatra-sugar
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 15
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
- 4
|
49
|
+
- 0
|
50
|
+
version: 0.4.0
|
51
|
+
type: :runtime
|
52
|
+
version_requirements: *id002
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: sinatra-test-helper
|
55
|
+
prerelease: false
|
56
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
hash: 15
|
62
|
+
segments:
|
63
|
+
- 0
|
64
|
+
- 4
|
65
|
+
- 0
|
66
|
+
version: 0.4.0
|
67
|
+
type: :development
|
68
|
+
version_requirements: *id003
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: sinatra
|
71
|
+
prerelease: false
|
72
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
hash: 15
|
78
|
+
segments:
|
79
|
+
- 1
|
80
|
+
- 0
|
81
|
+
version: "1.0"
|
82
|
+
type: :runtime
|
83
|
+
version_requirements: *id004
|
84
|
+
- !ruby/object:Gem::Dependency
|
85
|
+
name: rspec
|
86
|
+
prerelease: false
|
87
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
hash: 27
|
93
|
+
segments:
|
94
|
+
- 1
|
95
|
+
- 3
|
96
|
+
- 0
|
97
|
+
version: 1.3.0
|
98
|
+
type: :development
|
99
|
+
version_requirements: *id005
|
100
|
+
description: Mixin to ease Sinatra extension development (part of BigBand).
|
101
|
+
email: konstantin.mailinglists@googlemail.com
|
102
|
+
executables: []
|
103
|
+
|
104
|
+
extensions: []
|
105
|
+
|
106
|
+
extra_rdoc_files: []
|
107
|
+
|
108
|
+
files:
|
109
|
+
- lib/sinatra/extension.rb
|
110
|
+
- spec/sinatra/extension_spec.rb
|
111
|
+
- spec/spec_helper.rb
|
112
|
+
- README.md
|
113
|
+
- LICENSE
|
114
|
+
has_rdoc: yard
|
115
|
+
homepage: http://github.com/rkh/sinatra-extension
|
116
|
+
licenses: []
|
117
|
+
|
118
|
+
post_install_message:
|
119
|
+
rdoc_options: []
|
120
|
+
|
121
|
+
require_paths:
|
122
|
+
- lib
|
123
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
124
|
+
none: false
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
hash: 3
|
129
|
+
segments:
|
130
|
+
- 0
|
131
|
+
version: "0"
|
132
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
133
|
+
none: false
|
134
|
+
requirements:
|
135
|
+
- - ">="
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
hash: 3
|
138
|
+
segments:
|
139
|
+
- 0
|
140
|
+
version: "0"
|
141
|
+
requirements: []
|
142
|
+
|
143
|
+
rubyforge_project:
|
144
|
+
rubygems_version: 1.3.7
|
145
|
+
signing_key:
|
146
|
+
specification_version: 3
|
147
|
+
summary: Mixin to ease Sinatra extension development (part of BigBand).
|
148
|
+
test_files: []
|
149
|
+
|