filtration 0.0.1
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/LICENSE +19 -0
- data/README.rdoc +24 -0
- data/lib/filtration.rb +23 -0
- data/spec/filtration_spec.rb +41 -0
- data/spec/spec_helper.rb +4 -0
- metadata +62 -0
data/LICENSE
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
Copyright (c) 2011 Vertive, Inc.
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
of this software and associated documentation files (the "Software"), to deal
|
5
|
+
in the Software without restriction, including without limitation the rights
|
6
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
copies of the Software, and to permit persons to whom the Software is
|
8
|
+
furnished to do so, subject to the following conditions:
|
9
|
+
|
10
|
+
The above copyright notice and this permission notice shall be included in
|
11
|
+
all copies or substantial portions of the Software.
|
12
|
+
|
13
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
= Filtration
|
2
|
+
Filtration is a clean, lightweight way to filter arguments going into and out of methods.
|
3
|
+
|
4
|
+
== Usage
|
5
|
+
Example:
|
6
|
+
class Bar
|
7
|
+
def foo(x)
|
8
|
+
x + 2
|
9
|
+
end
|
10
|
+
def foo_too(x)
|
11
|
+
x + 2
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class Foo < Bar
|
16
|
+
prefilter(:foo){|x| x * 2} # (x * 2) + 2
|
17
|
+
postfilter(:foo_too){|x| x * 2} # (x + 2) * 2
|
18
|
+
end
|
19
|
+
|
20
|
+
== Requirements
|
21
|
+
Filtration stands on its own!
|
22
|
+
|
23
|
+
== Development
|
24
|
+
RSpec is used for testing.
|
data/lib/filtration.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
module Filtration
|
2
|
+
|
3
|
+
def prefilter(method,&block)
|
4
|
+
old_method = instance_method(method)
|
5
|
+
raise "Method #{method} takes 0 arguments" if old_method.arity == 0
|
6
|
+
define_method(method) do |*args|
|
7
|
+
old_method.bind(self).call(block.call(*args))
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def postfilter(method,&block)
|
12
|
+
old_method = instance_method(method)
|
13
|
+
raise "Method #{method} takes 0 arguments" if old_method.arity == 0
|
14
|
+
define_method(method) do |*args|
|
15
|
+
block.call(old_method.bind(self).call(*args))
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
class Object
|
22
|
+
extend Filtration
|
23
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Filtration do
|
4
|
+
|
5
|
+
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
|
12
|
+
prefilter(:foo){|x| x * 2}
|
13
|
+
end
|
14
|
+
Test1.new.foo(2).should === 6
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'executes a code block after the specified method' do
|
18
|
+
class Foo2
|
19
|
+
def foo(x)
|
20
|
+
x + 2
|
21
|
+
end
|
22
|
+
end
|
23
|
+
class Test2 < Foo2
|
24
|
+
postfilter(:foo){|x| x * 2}
|
25
|
+
end
|
26
|
+
Test2.new.foo(2).should === 8
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'raises an error if the method has no arguments' do
|
30
|
+
class Foo3
|
31
|
+
def foo
|
32
|
+
2
|
33
|
+
end
|
34
|
+
end
|
35
|
+
class Test3 < Foo3
|
36
|
+
extend RSpec::Matchers
|
37
|
+
lambda { prefilter(:foo){|x| x * 2} }.should raise_error
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,62 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: filtration
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- R. Scott Reis
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-04-12 00:00:00 Z
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email:
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README.rdoc
|
24
|
+
files:
|
25
|
+
- LICENSE
|
26
|
+
- README.rdoc
|
27
|
+
- lib/filtration.rb
|
28
|
+
- spec/spec_helper.rb
|
29
|
+
- spec/filtration_spec.rb
|
30
|
+
homepage:
|
31
|
+
licenses: []
|
32
|
+
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options:
|
35
|
+
- -m
|
36
|
+
- README.rdoc
|
37
|
+
- -t
|
38
|
+
- Anemone
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: "0"
|
53
|
+
requirements: []
|
54
|
+
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.7.2
|
57
|
+
signing_key:
|
58
|
+
specification_version: 3
|
59
|
+
summary: Filtration pre/post method callback
|
60
|
+
test_files:
|
61
|
+
- spec/spec_helper.rb
|
62
|
+
- spec/filtration_spec.rb
|