filtration 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -15,7 +15,16 @@ Example:
15
15
  class Foo < Bar
16
16
  prefilter(:foo){|x| x * 2} # (x * 2) + 2
17
17
  postfilter(:foo_too){|x| x * 2} # (x + 2) * 2
18
- end
18
+ end
19
+
20
+ class AnotherFoo < Bar
21
+ prefilter :foo, :double # (x * 2) + 2
22
+ postfilter :foo_too, :double # (x + 2) * 2
23
+
24
+ def double(x)
25
+ x * 2
26
+ end
27
+ end
19
28
 
20
29
  == Requirements
21
30
  Filtration stands on its own!
data/lib/filtration.rb CHANGED
@@ -1,18 +1,38 @@
1
1
  module Filtration
2
2
 
3
- def prefilter(method,&block)
3
+ def prefilter(method, filter=nil, &block)
4
4
  old_method = instance_method(method)
5
5
  raise "Method #{method} takes 0 arguments" if old_method.arity == 0
6
+
7
+ raise "Cannot use both filter method and block" if !filter.nil? && !block.nil?
8
+ raise "Block or filter method missing" if filter.nil? && block.nil?
9
+
6
10
  define_method(method) do |*args|
7
- old_method.bind(self).call(block.call(*args))
11
+ if filter.nil?
12
+ old_method.bind(self).call(block.call(*args))
13
+ else
14
+ filter_method = method(filter)
15
+ raise "Filter method #{filter} takes 0 arguments" if filter_method.arity == 0
16
+ old_method.bind(self).call(self.send(filter,*args))
17
+ end
8
18
  end
9
19
  end
10
20
 
11
- def postfilter(method,&block)
21
+ def postfilter(method, filter=nil, &block)
12
22
  old_method = instance_method(method)
13
23
  raise "Method #{method} takes 0 arguments" if old_method.arity == 0
24
+
25
+ raise "Cannot use both filter method and block" if !filter.nil? && !block.nil?
26
+ raise "Block or filter method missing" if filter.nil? && block.nil?
27
+
14
28
  define_method(method) do |*args|
15
- block.call(old_method.bind(self).call(*args))
29
+ if filter.nil?
30
+ block.call(old_method.bind(self).call(*args))
31
+ else
32
+ filter_method = method(filter)
33
+ raise "Filter method #{filter} takes 0 arguments" if filter_method.arity == 0
34
+ self.send(filter,old_method.bind(self).call(*args))
35
+ end
16
36
  end
17
37
  end
18
38
 
@@ -1,40 +1,87 @@
1
1
  require 'spec_helper'
2
2
 
3
+ class Good
4
+ def foo(x)
5
+ x + 2
6
+ end
7
+ end
8
+
9
+ class Bad
10
+ def foo
11
+ 2
12
+ end
13
+ end
14
+
3
15
  describe Filtration do
4
16
 
5
17
  it 'executes a code block before the specified method' do
6
- class Foo1
7
- def foo(x)
8
- x + 2
9
- end
10
- end
11
- class Test1 < Foo1
18
+ class Test1 < Good
12
19
  prefilter(:foo){|x| x * 2}
13
20
  end
14
21
  Test1.new.foo(2).should === 6
15
22
  end
16
23
 
17
24
  it 'executes a code block after the specified method' do
18
- class Foo2
19
- def foo(x)
20
- x + 2
25
+ class Test2 < Good
26
+ postfilter(:foo){|x| x * 2}
27
+ end
28
+ Test2.new.foo(2).should === 8
29
+ end
30
+
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
21
36
  end
22
37
  end
23
- class Test2 < Foo2
24
- postfilter(:foo){|x| x * 2}
38
+ Test1.new.foo(2).should === 6
39
+ end
40
+
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
46
+ end
25
47
  end
26
48
  Test2.new.foo(2).should === 8
27
49
  end
28
50
 
29
51
  it 'raises an error if the method has no arguments' do
30
- class Foo3
31
- def foo
32
- 2
52
+ class Test5 < Bad
53
+ extend RSpec::Matchers
54
+ lambda { prefilter(:foo){|x| x * 2} }.should raise_error
55
+ end
56
+ end
57
+
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
63
+ end
64
+ end
65
+ lambda { Test6.new.foo(2) }.should raise_error
66
+ end
67
+
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
33
73
  end
74
+ lambda { prefilter(:foo,:bar){|x| x * 2} }.should raise_error
34
75
  end
35
- class Test3 < Foo3
76
+ end
77
+
78
+ it 'raises an error if neither filter nor block are specified' do
79
+ class Test8 < Good
36
80
  extend RSpec::Matchers
37
- lambda { prefilter(:foo){|x| x * 2} }.should raise_error
81
+ def bar(x)
82
+ x * 2
83
+ end
84
+ lambda { prefilter(:foo) }.should raise_error
38
85
  end
39
86
  end
40
87
 
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: filtration
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - R. Scott Reis
@@ -35,7 +35,7 @@ rdoc_options:
35
35
  - -m
36
36
  - README.rdoc
37
37
  - -t
38
- - Anemone
38
+ - Filtration
39
39
  require_paths:
40
40
  - lib
41
41
  required_ruby_version: !ruby/object:Gem::Requirement