sinatra-extension 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +2 -1
- data/lib/sinatra/extension.rb +12 -5
- data/spec/sinatra/extension_spec.rb +23 -3
- metadata +13 -13
data/README.md
CHANGED
data/lib/sinatra/extension.rb
CHANGED
@@ -41,19 +41,26 @@ module Sinatra
|
|
41
41
|
register_hooks << block
|
42
42
|
end
|
43
43
|
|
44
|
-
def
|
44
|
+
def on_set(option, default_value = nil, &block)
|
45
45
|
mod = Module.new
|
46
46
|
mod.extend Sinatra::Extension
|
47
|
-
define_method(option) {
|
48
|
-
define_method("#{option}?") {
|
47
|
+
define_method(option) { default_value }
|
48
|
+
define_method("#{option}?") { !!__send__(option) }
|
49
49
|
define_method("#{option}=") do |value|
|
50
50
|
metadef(option) { value }
|
51
|
-
|
52
|
-
metadef("#{option}?") { true }
|
51
|
+
metadef("#{option}?") { !!value }
|
53
52
|
instance_yield block
|
54
53
|
end
|
55
54
|
end
|
56
55
|
|
56
|
+
def on_disable(option, &block)
|
57
|
+
on_set(option, true) { instance_yield block unless __send__ option }
|
58
|
+
end
|
59
|
+
|
60
|
+
def on_enable(option, &block)
|
61
|
+
on_set(option, false) { instance_yield block if __send__ option }
|
62
|
+
end
|
63
|
+
|
57
64
|
def register(*extensions)
|
58
65
|
on_register { register(*extensions) }
|
59
66
|
end
|
@@ -21,7 +21,10 @@ module Sinatra
|
|
21
21
|
def foo(value) value end
|
22
22
|
end
|
23
23
|
|
24
|
-
|
24
|
+
set :counter, 0
|
25
|
+
on_set(:trigger) { set :counter, counter + 1 }
|
26
|
+
|
27
|
+
on_enable :special_foo do
|
25
28
|
helpers do
|
26
29
|
def foo(value) 42 end
|
27
30
|
end
|
@@ -57,9 +60,26 @@ describe Sinatra::Extension do
|
|
57
60
|
app.new.foo(10).should == 10
|
58
61
|
end
|
59
62
|
|
60
|
-
it "should
|
63
|
+
it "should trigger on_set block" do
|
64
|
+
app.set :counter, 0
|
65
|
+
app.counter.should == 0
|
66
|
+
app.enable :trigger
|
67
|
+
app.counter.should == 1
|
68
|
+
app.disable :trigger
|
69
|
+
app.counter.should == 2
|
70
|
+
app.set :trigger, :foo
|
71
|
+
app.counter.should == 3
|
72
|
+
app.trigger = 42
|
73
|
+
app.counter.should == 4
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should apply on_enable blocks only if given option has been enabled" do
|
77
|
+
app.should_not be_special_foo
|
78
|
+
app.special_foo.should be_false
|
79
|
+
app.new.foo(10).should == 10
|
80
|
+
app.disable :special_foo
|
61
81
|
app.should_not be_special_foo
|
62
|
-
app.special_foo.should
|
82
|
+
app.special_foo.should be_false
|
63
83
|
app.new.foo(10).should == 10
|
64
84
|
app.enable :special_foo
|
65
85
|
app.should be_special_foo
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-extension
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 11
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
8
|
+
- 5
|
9
9
|
- 0
|
10
|
-
version: 0.
|
10
|
+
version: 0.5.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Konstantin Haase
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-07-09 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -26,12 +26,12 @@ dependencies:
|
|
26
26
|
requirements:
|
27
27
|
- - ~>
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
hash:
|
29
|
+
hash: 11
|
30
30
|
segments:
|
31
31
|
- 0
|
32
|
-
-
|
32
|
+
- 5
|
33
33
|
- 0
|
34
|
-
version: 0.
|
34
|
+
version: 0.5.0
|
35
35
|
type: :runtime
|
36
36
|
version_requirements: *id001
|
37
37
|
- !ruby/object:Gem::Dependency
|
@@ -42,12 +42,12 @@ dependencies:
|
|
42
42
|
requirements:
|
43
43
|
- - ~>
|
44
44
|
- !ruby/object:Gem::Version
|
45
|
-
hash:
|
45
|
+
hash: 11
|
46
46
|
segments:
|
47
47
|
- 0
|
48
|
-
-
|
48
|
+
- 5
|
49
49
|
- 0
|
50
|
-
version: 0.
|
50
|
+
version: 0.5.0
|
51
51
|
type: :runtime
|
52
52
|
version_requirements: *id002
|
53
53
|
- !ruby/object:Gem::Dependency
|
@@ -58,12 +58,12 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - ~>
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
hash:
|
61
|
+
hash: 11
|
62
62
|
segments:
|
63
63
|
- 0
|
64
|
-
-
|
64
|
+
- 5
|
65
65
|
- 0
|
66
|
-
version: 0.
|
66
|
+
version: 0.5.0
|
67
67
|
type: :development
|
68
68
|
version_requirements: *id003
|
69
69
|
- !ruby/object:Gem::Dependency
|