filtration 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
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
- prefilter(:foo){|x| x * 2} # (x * 2) + 2
17
- postfilter(:foo_too){|x| x * 2} # (x + 2) * 2
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
- prefilter :foo, :double # (x * 2) + 2
22
- postfilter :foo_too, :double # (x + 2) * 2
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.
@@ -1,88 +1,141 @@
1
1
  require 'spec_helper'
2
2
 
3
- class Good
4
- def foo(x)
5
- x + 2
6
- end
7
- end
3
+ describe Filtration do
8
4
 
9
- class Bad
10
- def foo
11
- 2
12
- end
13
- end
5
+ describe '#prefilter' do
14
6
 
15
- describe Filtration do
7
+ context 'given a valid target method' do
16
8
 
17
- it 'executes a code block before the specified method' do
18
- class Test1 < Good
19
- prefilter(:foo){|x| x * 2}
20
- end
21
- Test1.new.foo(2).should === 6
22
- end
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
- it 'executes a code block after the specified method' do
25
- class Test2 < Good
26
- postfilter(:foo){|x| x * 2}
27
- end
28
- Test2.new.foo(2).should === 8
29
- end
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
- it 'executes a code block before the specified method' do
32
- class Test3 < Good
33
- prefilter :foo, :bar
34
- def bar(x)
35
- x * 2
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
- it 'executes a code block after the specified method' do
42
- class Test4 < Good
43
- postfilter :foo, :bar
44
- def bar(x)
45
- x * 2
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
- it 'raises an error if the method has no arguments' do
52
- class Test5 < Bad
53
- extend RSpec::Matchers
54
- lambda { prefilter(:foo){|x| x * 2} }.should raise_error
55
- end
56
- end
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
- it 'raises an error if the filter method has no arguments' do
59
- class Test6 < Good
60
- prefilter :foo, :bar
61
- def bar
62
- nil
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
- lambda { Test6.new.foo(2) }.should raise_error
70
+
66
71
  end
67
72
 
68
- it 'raises an error if both filter and block are specified' do
69
- class Test7 < Good
70
- extend RSpec::Matchers
71
- def bar(x)
72
- x * 2
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
- lambda { prefilter(:foo,:bar){|x| x * 2} }.should raise_error
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
- it 'raises an error if neither filter nor block are specified' do
79
- class Test8 < Good
80
- extend RSpec::Matchers
81
- def bar(x)
82
- x * 2
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
- lambda { prefilter(:foo) }.should raise_error
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
@@ -0,0 +1,11 @@
1
+ class Good
2
+ def foo(x)
3
+ x + 2
4
+ end
5
+ end
6
+
7
+ class Bad
8
+ def foo
9
+ 2
10
+ end
11
+ 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 'rspec'
4
- require 'filtration'
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.2
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-12 00:00:00 Z
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