context-filters 0.8.0 → 0.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/context-filters/context.rb +3 -3
- data/lib/context-filters/filters.rb +18 -4
- data/lib/context-filters/global_context.rb +2 -2
- data/lib/context-filters/local_context.rb +2 -2
- data/lib/context-filters/version.rb +1 -1
- data/test/context-filters/context_test.rb +6 -11
- data/test/context-filters/filters_test.rb +15 -5
- data/test/context-filters/global_context_test.rb +9 -4
- data/test/context-filters/local_context_test.rb +2 -5
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a378eabd84a4a77fffc09011562666ec496ab827
|
4
|
+
data.tar.gz: 3c63a922769d75d58efd77748888a45520b72dc5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e0b7a8bc527aeca7a99e7fc0f6fd8cbb9a086cd3e06d07ede4f3003bd8fc08ac5b7853a88d13e90f96ac0f0fc659ff5f9fe1efd0e4e11a55a99946407479a034
|
7
|
+
data.tar.gz: 12184765863f24c3f73885d8ec33457d1d7f379649b3843d81d487da9aaf8b79de95718d054b1acd1c9cd4122d8de01be02b9c528a1dbbdd76112742cbe1fc42
|
@@ -15,9 +15,9 @@ class ContextFilters::Context < ContextFilters::GlobalContext
|
|
15
15
|
|
16
16
|
# run the given method on global and local filters
|
17
17
|
# @param method [Proc] the method to evaluate for filters matching context
|
18
|
-
def evaluate_filters(method)
|
19
|
-
super(method) do
|
20
|
-
evaluate_local_filters(method)
|
18
|
+
def evaluate_filters(target, method)
|
19
|
+
super(target, method) do
|
20
|
+
evaluate_local_filters(target, method)
|
21
21
|
end
|
22
22
|
end
|
23
23
|
|
@@ -47,12 +47,26 @@ class ContextFilters::Filters
|
|
47
47
|
@filters[options] << block
|
48
48
|
end
|
49
49
|
|
50
|
-
# applies matching filters to the given method
|
51
|
-
#
|
50
|
+
# applies matching filters to the given target method, also uses
|
51
|
+
# filters matching extra +:target => target+ when options is a Hash
|
52
|
+
# @param target [Object] an object to run the method on
|
53
|
+
# @param method [symbol] method name that takes a transformation
|
52
54
|
# block as param
|
53
55
|
# @param options [Object] a filter for selecting matching blocks
|
54
|
-
def apply(method, options =
|
55
|
-
|
56
|
+
def apply(target, method, options = nil)
|
57
|
+
select_filters(target, options).each{|block| target.send(method, &block) }
|
58
|
+
end
|
59
|
+
|
60
|
+
# Select matching filters and filters including targets when
|
61
|
+
# options is a +Hash+
|
62
|
+
# @param target [Object] an object to run the method on
|
63
|
+
# @param options [Object] a filter for selecting matching blocks
|
64
|
+
def select_filters(target, options)
|
65
|
+
found = @filters.fetch(options, [])
|
66
|
+
if Hash === options
|
67
|
+
then found += @filters.fetch(options.merge(:target => target), [])
|
68
|
+
end
|
69
|
+
found
|
56
70
|
end
|
57
71
|
|
58
72
|
# Array of already defined filters
|
@@ -59,12 +59,12 @@ class ContextFilters::GlobalContext
|
|
59
59
|
# work for +priority.nil?+ or on the end of the priorities,
|
60
60
|
# @param method [Proc] the method to evaluate with filters matching current context
|
61
61
|
# @yield on first +priority.nil?+ or on the end when none
|
62
|
-
def evaluate_filters(method)
|
62
|
+
def evaluate_filters(target, method)
|
63
63
|
local_called = false
|
64
64
|
|
65
65
|
@priority_filters.each do |priority, filters|
|
66
66
|
|
67
|
-
@context.each { |options| filters.apply(method, options) }
|
67
|
+
@context.each { |options| filters.apply(target, method, options) }
|
68
68
|
|
69
69
|
if priority.nil? && block_given? && !local_called
|
70
70
|
yield
|
@@ -34,8 +34,8 @@ module ContextFilters::LocalContext
|
|
34
34
|
# @param method [Proc] a method to call with each filter stored in
|
35
35
|
# +local_filters+
|
36
36
|
# @api private
|
37
|
-
def evaluate_local_filters(method)
|
38
|
-
local_filters.each { |block|
|
37
|
+
def evaluate_local_filters(target, method)
|
38
|
+
local_filters.each { |block| target.send(method, &block) }
|
39
39
|
end
|
40
40
|
|
41
41
|
end
|
@@ -18,29 +18,24 @@ describe ContextFilters::Context do
|
|
18
18
|
FilterTestSubject.new(3)
|
19
19
|
end
|
20
20
|
|
21
|
-
let(:change_method) do
|
22
|
-
filter_test_subject.method(:change)
|
23
|
-
end
|
24
|
-
|
25
21
|
describe "#evaluate_filters" do
|
26
22
|
|
27
23
|
it "does not apply filters when no filters" do
|
28
24
|
subject.priority_filters.expects(:apply).never
|
29
|
-
subject.evaluate_filters(
|
25
|
+
subject.evaluate_filters(filter_test_subject, :change)
|
30
26
|
end
|
31
27
|
|
32
28
|
it "does apply global filters" do
|
33
|
-
method = Proc.new{}
|
34
29
|
subject.context << :a
|
35
30
|
subject.filter(nil, :b) { true }
|
36
|
-
subject.priority_filters.to_a[0][1].expects(:apply).once.with(
|
37
|
-
subject.priority_filters.to_a[0][1].expects(:apply).once.with(
|
38
|
-
subject.evaluate_filters(
|
31
|
+
subject.priority_filters.to_a[0][1].expects(:apply).once.with(filter_test_subject, :change, nil)
|
32
|
+
subject.priority_filters.to_a[0][1].expects(:apply).once.with(filter_test_subject, :change, :a)
|
33
|
+
subject.evaluate_filters(filter_test_subject, :change)
|
39
34
|
end
|
40
35
|
|
41
36
|
it "does apply local filters" do
|
42
37
|
subject.stubs(:local_filters).returns([Proc.new{|value| value+4}])
|
43
|
-
subject.evaluate_filters(
|
38
|
+
subject.evaluate_filters(filter_test_subject, :change)
|
44
39
|
filter_test_subject.value.must_equal(7)
|
45
40
|
end
|
46
41
|
|
@@ -51,7 +46,7 @@ describe ContextFilters::Context do
|
|
51
46
|
|
52
47
|
subject.filter(nil,&multiplication)
|
53
48
|
subject.local_filter(addition) do
|
54
|
-
subject.evaluate_filters(
|
49
|
+
subject.evaluate_filters(filter_test_subject, :change)
|
55
50
|
end
|
56
51
|
|
57
52
|
filter_test_subject.value.must_equal(10)
|
@@ -59,7 +59,7 @@ describe ContextFilters::Filters do
|
|
59
59
|
it "does not apply filter" do
|
60
60
|
subject.store({x: 1}) { |value| "better #{value}" }
|
61
61
|
|
62
|
-
subject.apply(apply_test_subject
|
62
|
+
subject.apply(apply_test_subject, :change, {x: 2})
|
63
63
|
|
64
64
|
apply_test_subject.value.must_equal("test me")
|
65
65
|
end
|
@@ -67,7 +67,7 @@ describe ContextFilters::Filters do
|
|
67
67
|
it "applies single filter" do
|
68
68
|
subject.store({x: 1}) { |value| "better #{value}" }
|
69
69
|
|
70
|
-
subject.apply(apply_test_subject
|
70
|
+
subject.apply(apply_test_subject, :change, {x: 1})
|
71
71
|
|
72
72
|
apply_test_subject.value.must_equal("better test me")
|
73
73
|
end
|
@@ -77,7 +77,7 @@ describe ContextFilters::Filters do
|
|
77
77
|
subject.store({x: 1}) { |value| "#{value} please" }
|
78
78
|
subject.store({x: 1}) { |value| "#{value}!" }
|
79
79
|
|
80
|
-
subject.apply(apply_test_subject
|
80
|
+
subject.apply(apply_test_subject, :change, {x: 1})
|
81
81
|
|
82
82
|
apply_test_subject.value.must_equal("better test me please!")
|
83
83
|
end
|
@@ -86,12 +86,22 @@ describe ContextFilters::Filters do
|
|
86
86
|
subject.store({x: 1}) { |value| "dont #{value}" }
|
87
87
|
subject.store({x: 2}) { |value| "#{value} now" }
|
88
88
|
|
89
|
-
subject.apply(apply_test_subject
|
90
|
-
subject.apply(apply_test_subject
|
89
|
+
subject.apply(apply_test_subject, :change, {x: 1})
|
90
|
+
subject.apply(apply_test_subject, :change, {x: 2})
|
91
91
|
|
92
92
|
apply_test_subject.value.must_equal("dont test me now")
|
93
93
|
end
|
94
94
|
|
95
|
+
it "applies only one target filter" do
|
96
|
+
subject.store({x: 1, target: apply_test_subject}) { |value| "dont #{value}" }
|
97
|
+
subject.store({x: 2, target: nil}) { |value| "#{value} now" }
|
98
|
+
|
99
|
+
subject.apply(apply_test_subject, :change, {x: 1})
|
100
|
+
subject.apply(apply_test_subject, :change, {x: 2})
|
101
|
+
|
102
|
+
apply_test_subject.value.must_equal("dont test me")
|
103
|
+
end
|
104
|
+
|
95
105
|
end #apply
|
96
106
|
|
97
107
|
end
|
@@ -6,6 +6,7 @@ See the file LICENSE for copying permission.
|
|
6
6
|
|
7
7
|
require "test_helper"
|
8
8
|
require "context-filters/global_context"
|
9
|
+
require "context-filters/filter_test_subject"
|
9
10
|
|
10
11
|
describe ContextFilters::GlobalContext do
|
11
12
|
|
@@ -13,6 +14,10 @@ describe ContextFilters::GlobalContext do
|
|
13
14
|
ContextFilters::GlobalContext.new
|
14
15
|
end
|
15
16
|
|
17
|
+
let(:filter_test_subject) do
|
18
|
+
FilterTestSubject.new(3)
|
19
|
+
end
|
20
|
+
|
16
21
|
describe "#initialize" do
|
17
22
|
|
18
23
|
it "sets up initial variables" do
|
@@ -32,16 +37,16 @@ describe ContextFilters::GlobalContext do
|
|
32
37
|
|
33
38
|
it "does not apply filters when no filters" do
|
34
39
|
subject.priority_filters.expects(:apply).never
|
35
|
-
subject.evaluate_filters(
|
40
|
+
subject.evaluate_filters(filter_test_subject, :change)
|
36
41
|
end
|
37
42
|
|
38
43
|
it "does apply filters" do
|
39
44
|
method = Proc.new{}
|
40
45
|
subject.context << :a
|
41
46
|
subject.filter(nil, :b) { true }
|
42
|
-
subject.priority_filters.to_a[0][1].expects(:apply).once.with(
|
43
|
-
subject.priority_filters.to_a[0][1].expects(:apply).once.with(
|
44
|
-
subject.evaluate_filters(
|
47
|
+
subject.priority_filters.to_a[0][1].expects(:apply).once.with(filter_test_subject, :change, nil)
|
48
|
+
subject.priority_filters.to_a[0][1].expects(:apply).once.with(filter_test_subject, :change, :a)
|
49
|
+
subject.evaluate_filters(filter_test_subject, :change)
|
45
50
|
end
|
46
51
|
|
47
52
|
end #evaluate_command
|
@@ -18,15 +18,12 @@ describe ContextFilters::LocalContext do
|
|
18
18
|
FilterTestSubject.new(3)
|
19
19
|
end
|
20
20
|
|
21
|
-
let(:change_method) do
|
22
|
-
filter_test_subject.method(:change)
|
23
|
-
end
|
24
|
-
|
25
21
|
it "has default value for #local_filters" do
|
26
22
|
subject.local_filters.must_equal([])
|
27
23
|
end
|
28
24
|
|
29
25
|
it "adds local filters" do
|
26
|
+
change_method = Proc.new {}
|
30
27
|
subject.local_filter(change_method) do
|
31
28
|
subject.local_filters.must_equal([change_method])
|
32
29
|
end
|
@@ -36,7 +33,7 @@ describe ContextFilters::LocalContext do
|
|
36
33
|
it "runs change" do
|
37
34
|
method = Proc.new { |value| value+4 }
|
38
35
|
subject.stubs(:local_filters).returns([method])
|
39
|
-
subject.evaluate_local_filters(
|
36
|
+
subject.evaluate_local_filters(filter_test_subject, :change)
|
40
37
|
filter_test_subject.value.must_equal(7)
|
41
38
|
end
|
42
39
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: context-filters
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.9.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Michal Papis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10-
|
11
|
+
date: 2014-10-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: guard
|
@@ -142,9 +142,9 @@ summary: Generic support for filters applied in context
|
|
142
142
|
test_files:
|
143
143
|
- test/test_helper.rb
|
144
144
|
- test/context-filters/filter_test_subject.rb
|
145
|
+
- test/context-filters/priority_filters_test.rb
|
146
|
+
- test/context-filters/local_context_test.rb
|
145
147
|
- test/context-filters/context_test.rb
|
146
|
-
- test/context-filters/filters_test.rb
|
147
148
|
- test/context-filters/global_context_test.rb
|
148
|
-
- test/context-filters/
|
149
|
-
- test/context-filters/priority_filters_test.rb
|
149
|
+
- test/context-filters/filters_test.rb
|
150
150
|
has_rdoc:
|