filtration 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.rdoc +16 -9
  2. data/lib/filtration.rb +15 -16
  3. metadata +14 -4
data/README.rdoc CHANGED
@@ -3,6 +3,7 @@ Filtration is a clean, lightweight way to filter arguments going into and coming
3
3
 
4
4
  == Usage
5
5
  Example:
6
+
6
7
  class Bar
7
8
 
8
9
  def foo(x)
@@ -26,20 +27,20 @@ Example:
26
27
  class Foo < Bar
27
28
 
28
29
  prefilter(:foo){|x| x * 2}
29
- #=> (x * 2) + 2 = 6 (for x = 2)
30
30
 
31
31
  postfilter(:foo_too){|x| x * 2}
32
- #=> (x + 2) * 2 = 8 (for x = 2)
33
-
32
+
34
33
  end
35
-
34
+
35
+ randy = Foo.new
36
+ randy.foo(2) #=> (x * 2) + 2 = 6 (for x = 2)
37
+ randy.foo_too(2) #=> (x + 2) * 2 = 8 (for x = 2)
38
+
36
39
  class AnotherFoo < Bar
37
40
 
38
41
  prefilter :foo, :double
39
- #=> (x * 2) + 2 = 6 (for x = 2)
40
42
 
41
43
  postfilter :foo_too, :double
42
- #=> (x + 2) * 2 => 8 (for x = 2)
43
44
 
44
45
  def double(x)
45
46
  x * 2
@@ -47,18 +48,24 @@ Example:
47
48
 
48
49
  end
49
50
 
51
+ savage = AnotherFoo.new
52
+ savage.foo(2) #=> (x * 2) + 2 = 6
53
+ savage.foo_too(2) #=> (x + 2) * 2 => 8
54
+
50
55
  class YetAnotherFoo < Bar
51
56
 
52
57
  prefilter :foo_as_well, &:upcase
53
- #=> "#{x.upcase} to the max!" = 'FILTRATION to the max!' (for x = 'filtration')
54
58
 
55
59
  postfilter :foo_forever, &:upcase
56
- #=> "#{x} to the max!".upcase = 'FILTRATION TO THE MAX!' (for x = 'filtration')
57
60
 
58
61
  end
59
62
 
63
+ machoman = YetAnotherFoo.new
64
+ machoman.foo_as_well('filtration') #=> "#{x.upcase} to the max!" = 'FILTRATION to the max!'
65
+ machoman.foo_forever('filtration') #=> "#{x} to the max!".upcase = 'FILTRATION TO THE MAX!'
66
+
60
67
  == Requirements
61
68
  Filtration stands on its own!
62
69
 
63
70
  == Development
64
- RSpec is used for testing, with SimpleCov for code coverage.
71
+ RSpec is used for testing, with SimpleCov for code coverage.
data/lib/filtration.rb CHANGED
@@ -1,13 +1,13 @@
1
1
  module Filtration
2
2
 
3
- def prefilter(method, filter=nil, &block)
4
- old_method = instance_method(method)
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
-
10
- define_method(method) do |*args|
3
+ # for method with arity > 0, 'target', intercept arguments and apply
4
+ # method 'filter' or passed 'block' to them before running 'target' with them
5
+ def prefilter(target, filter=nil, &block)
6
+ old_method = instance_method(target)
7
+ raise "Method #{target} takes 0 arguments" if old_method.arity == 0
8
+ raise "Must use either passed filter method or passed block" unless filter.nil? ^ block.nil?
9
+
10
+ define_method(target) do |*args|
11
11
  if filter.nil?
12
12
  old_method.bind(self).call(block.call(*args))
13
13
  else
@@ -18,14 +18,13 @@ module Filtration
18
18
  end
19
19
  end
20
20
 
21
- def postfilter(method, filter=nil, &block)
22
- old_method = instance_method(method)
23
- raise "Method #{method} takes 0 arguments" if old_method.arity == 0
21
+ # for method, 'target', intercept output and apply method 'filter' or
22
+ # passed 'block' to it before returning the value
23
+ def postfilter(target, filter=nil, &block)
24
+ old_method = instance_method(target)
25
+ raise "Must use either passed filter method or passed block" unless filter.nil? ^ block.nil?
24
26
 
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
-
28
- define_method(method) do |*args|
27
+ define_method(target) do |*args|
29
28
  if filter.nil?
30
29
  block.call(old_method.bind(self).call(*args))
31
30
  else
@@ -40,4 +39,4 @@ end
40
39
 
41
40
  class Object
42
41
  extend Filtration
43
- end
42
+ end
metadata CHANGED
@@ -1,8 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filtration
3
3
  version: !ruby/object:Gem::Version
4
- prerelease:
5
- version: 0.0.4
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 5
9
+ version: 0.0.5
6
10
  platform: ruby
7
11
  authors:
8
12
  - R. Scott Reis
@@ -10,7 +14,8 @@ autorequire:
10
14
  bindir: bin
11
15
  cert_chain: []
12
16
 
13
- date: 2011-04-15 00:00:00 Z
17
+ date: 2011-08-13 00:00:00 -05:00
18
+ default_executable:
14
19
  dependencies: []
15
20
 
16
21
  description:
@@ -28,6 +33,7 @@ files:
28
33
  - spec/spec_helper.rb
29
34
  - spec/spec_dummies.rb
30
35
  - spec/filtration_spec.rb
36
+ has_rdoc: true
31
37
  homepage:
32
38
  licenses: []
33
39
 
@@ -44,17 +50,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
44
50
  requirements:
45
51
  - - ">="
46
52
  - !ruby/object:Gem::Version
53
+ segments:
54
+ - 0
47
55
  version: "0"
48
56
  required_rubygems_version: !ruby/object:Gem::Requirement
49
57
  none: false
50
58
  requirements:
51
59
  - - ">="
52
60
  - !ruby/object:Gem::Version
61
+ segments:
62
+ - 0
53
63
  version: "0"
54
64
  requirements: []
55
65
 
56
66
  rubyforge_project:
57
- rubygems_version: 1.7.2
67
+ rubygems_version: 1.3.7
58
68
  signing_key:
59
69
  specification_version: 3
60
70
  summary: Filtration enables pre/post method callback, similar to Python decorators