filtration 0.0.2 → 0.0.3
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.rdoc +38 -7
- data/spec/filtration_spec.rb +116 -63
- data/spec/spec_dummies.rb +11 -0
- data/spec/spec_helper.rb +9 -2
- metadata +5 -3
data/README.rdoc
CHANGED
@@ -1,33 +1,64 @@
|
|
1
1
|
= Filtration
|
2
|
-
Filtration is a clean, lightweight way to filter arguments going into and out of methods.
|
2
|
+
Filtration is a clean, lightweight way to filter arguments going into and coming out of methods (similar to decorators in Python).
|
3
3
|
|
4
4
|
== Usage
|
5
5
|
Example:
|
6
6
|
class Bar
|
7
|
+
|
7
8
|
def foo(x)
|
8
9
|
x + 2
|
9
10
|
end
|
11
|
+
|
10
12
|
def foo_too(x)
|
11
13
|
x + 2
|
12
14
|
end
|
15
|
+
|
16
|
+
def foo_as_well(x)
|
17
|
+
"#{x.to_s} to the max!"
|
18
|
+
end
|
19
|
+
|
20
|
+
def foo_forever(x)
|
21
|
+
"#{x.to_s} to the max!"
|
22
|
+
end
|
23
|
+
|
13
24
|
end
|
14
25
|
|
15
26
|
class Foo < Bar
|
16
|
-
|
17
|
-
|
27
|
+
|
28
|
+
prefilter(:foo){|x| x * 2}
|
29
|
+
#=> (x * 2) + 2 = 6 (for x = 2)
|
30
|
+
|
31
|
+
postfilter(:foo_too){|x| x * 2}
|
32
|
+
#=> (x + 2) * 2 = 8 (for x = 2)
|
33
|
+
|
18
34
|
end
|
19
|
-
|
35
|
+
|
20
36
|
class AnotherFoo < Bar
|
21
|
-
|
22
|
-
|
37
|
+
|
38
|
+
prefilter :foo, :double
|
39
|
+
#=> (x * 2) + 2 = 6 (for x = 2)
|
40
|
+
|
41
|
+
postfilter :foo_too, :double
|
42
|
+
#=> (x + 2) * 2 => 8 (for x = 2)
|
23
43
|
|
24
44
|
def double(x)
|
25
45
|
x * 2
|
26
46
|
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
class YetAnotherFoo < Bar
|
51
|
+
|
52
|
+
prefilter :foo_as_well, &:upcase
|
53
|
+
#=> "#{x.upcase} to the max!" = 'FILTRATION to the max!' (for x = 'filtration')
|
54
|
+
|
55
|
+
postfilter :foo_forever, &:upcase
|
56
|
+
#=> "#{x} to the max!".upcase = 'FILTRATION TO THE MAX!' (for x = 'filtration')
|
57
|
+
|
27
58
|
end
|
28
59
|
|
29
60
|
== Requirements
|
30
61
|
Filtration stands on its own!
|
31
62
|
|
32
63
|
== Development
|
33
|
-
RSpec is used for testing.
|
64
|
+
RSpec is used for testing, with SimpleCov for code coverage.
|
data/spec/filtration_spec.rb
CHANGED
@@ -1,88 +1,141 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
def foo(x)
|
5
|
-
x + 2
|
6
|
-
end
|
7
|
-
end
|
3
|
+
describe Filtration do
|
8
4
|
|
9
|
-
|
10
|
-
def foo
|
11
|
-
2
|
12
|
-
end
|
13
|
-
end
|
5
|
+
describe '#prefilter' do
|
14
6
|
|
15
|
-
|
7
|
+
context 'given a valid target method' do
|
16
8
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
9
|
+
context 'and a code block for filtering' do
|
10
|
+
it 'executes the block on the target method argument' do
|
11
|
+
test = Class.new(Good) { prefilter(:foo){|x| x * 2} }
|
12
|
+
test.new.foo(2).should === 6
|
13
|
+
end
|
14
|
+
end
|
23
15
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
16
|
+
context 'and a valid filter method' do
|
17
|
+
it 'executes the filter method on the target method argument' do
|
18
|
+
test = Class.new(Good) do
|
19
|
+
prefilter :foo, :double
|
20
|
+
def double(x) x * 2; end
|
21
|
+
end
|
22
|
+
test.new.foo(2).should === 6
|
23
|
+
end
|
24
|
+
end
|
30
25
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
26
|
+
context 'and an invalid filter method' do
|
27
|
+
it 'raises an error' do
|
28
|
+
test = Class.new(Good) do
|
29
|
+
prefilter :foo, :double
|
30
|
+
def double() 2; end
|
31
|
+
end
|
32
|
+
expect { test.new.foo(2) }.to raise_error
|
33
|
+
end
|
36
34
|
end
|
35
|
+
|
37
36
|
end
|
38
|
-
Test1.new.foo(2).should === 6
|
39
|
-
end
|
40
37
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
38
|
+
context 'given an invalid target method' do
|
39
|
+
|
40
|
+
context 'and a code block for filtering' do
|
41
|
+
it 'raises an error' do
|
42
|
+
Class.new(Bad) do
|
43
|
+
extend RSpec::Matchers
|
44
|
+
expect { prefilter(:foo){|x| x * 2} }.to raise_error
|
45
|
+
end
|
46
|
+
end
|
46
47
|
end
|
47
|
-
end
|
48
|
-
Test2.new.foo(2).should === 8
|
49
|
-
end
|
50
48
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
49
|
+
context 'and a valid filter method' do
|
50
|
+
it 'raises an error' do
|
51
|
+
test = Class.new(Good) do
|
52
|
+
prefilter :foo, :double
|
53
|
+
def double(x) x * 2; end
|
54
|
+
end
|
55
|
+
expect { test.new.foo }.to raise_error
|
56
|
+
end
|
57
|
+
end
|
57
58
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
59
|
+
context 'and an invalid filter method' do
|
60
|
+
it 'raises an error' do
|
61
|
+
test = Class.new(Good) do
|
62
|
+
prefilter :foo, :double
|
63
|
+
def double() 2; end
|
64
|
+
end
|
65
|
+
expect { test.new.foo }.to raise_error
|
66
|
+
end
|
63
67
|
end
|
68
|
+
|
64
69
|
end
|
65
|
-
|
70
|
+
|
66
71
|
end
|
67
72
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
+
describe '#postfilter' do
|
74
|
+
|
75
|
+
context 'given a valid target method' do
|
76
|
+
|
77
|
+
context 'and a code block for filtering' do
|
78
|
+
it 'executes the block on the target method argument' do
|
79
|
+
test = Class.new(Good) { postfilter(:foo){|x| x * 2} }
|
80
|
+
test.new.foo(2).should === 8
|
81
|
+
end
|
73
82
|
end
|
74
|
-
|
83
|
+
|
84
|
+
context 'and a valid filter method' do
|
85
|
+
it 'executes the filter method on the target method argument' do
|
86
|
+
test = Class.new(Good) do
|
87
|
+
postfilter :foo, :double
|
88
|
+
def double(x) x * 2; end
|
89
|
+
end
|
90
|
+
test.new.foo(2).should === 8
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
context 'and an invalid filter method' do
|
95
|
+
it 'raises an error' do
|
96
|
+
test = Class.new(Good) do
|
97
|
+
postfilter :foo, :double
|
98
|
+
def double() 2; end
|
99
|
+
end
|
100
|
+
expect { test.new.foo(2) }.to raise_error
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
75
104
|
end
|
76
|
-
end
|
77
105
|
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
106
|
+
context 'given an invalid target method' do
|
107
|
+
|
108
|
+
context 'and a code block for filtering' do
|
109
|
+
it 'raises an error' do
|
110
|
+
Class.new(Bad) do
|
111
|
+
extend RSpec::Matchers
|
112
|
+
expect { postfilter(:foo){|x| x * 2} }.to raise_error
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
context 'and a valid filter method' do
|
118
|
+
it 'raises an error' do
|
119
|
+
test = Class.new(Good) do
|
120
|
+
postfilter :foo, :double
|
121
|
+
def double(x) x * 2; end
|
122
|
+
end
|
123
|
+
expect { test.new.foo }.to raise_error
|
124
|
+
end
|
83
125
|
end
|
84
|
-
|
126
|
+
|
127
|
+
context 'and an invalid filter method' do
|
128
|
+
it 'raises an error' do
|
129
|
+
test = Class.new(Good) do
|
130
|
+
postfilter :foo, :double
|
131
|
+
def double() 2; end
|
132
|
+
end
|
133
|
+
expect { test.new.foo }.to raise_error
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
85
137
|
end
|
138
|
+
|
86
139
|
end
|
87
140
|
|
88
141
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,4 +1,11 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
SimpleCov.start { add_filter '/spec/' }
|
3
|
+
|
1
4
|
$:.unshift(File.dirname(__FILE__) + '/../lib/')
|
2
5
|
|
3
|
-
require '
|
4
|
-
require '
|
6
|
+
require 'rubygems'
|
7
|
+
require 'bundler/setup'
|
8
|
+
Bundler.require(:default,:test)
|
9
|
+
|
10
|
+
require 'filtration'
|
11
|
+
require 'spec_dummies'
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: filtration
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.3
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- R. Scott Reis
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-04-
|
13
|
+
date: 2011-04-15 00:00:00 Z
|
14
14
|
dependencies: []
|
15
15
|
|
16
16
|
description:
|
@@ -26,6 +26,7 @@ files:
|
|
26
26
|
- README.rdoc
|
27
27
|
- lib/filtration.rb
|
28
28
|
- spec/spec_helper.rb
|
29
|
+
- spec/spec_dummies.rb
|
29
30
|
- spec/filtration_spec.rb
|
30
31
|
homepage:
|
31
32
|
licenses: []
|
@@ -56,7 +57,8 @@ rubyforge_project:
|
|
56
57
|
rubygems_version: 1.7.2
|
57
58
|
signing_key:
|
58
59
|
specification_version: 3
|
59
|
-
summary: Filtration pre/post method callback
|
60
|
+
summary: Filtration enables pre/post method callback, similar to Python decorators
|
60
61
|
test_files:
|
61
62
|
- spec/spec_helper.rb
|
63
|
+
- spec/spec_dummies.rb
|
62
64
|
- spec/filtration_spec.rb
|