context-filters 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/context-filters.rb +8 -0
- data/lib/context-filters/context.rb +23 -0
- data/lib/context-filters/filters.rb +68 -0
- data/lib/context-filters/global_context.rb +77 -0
- data/lib/context-filters/local_context.rb +39 -0
- data/lib/context-filters/priority_filters.rb +52 -0
- data/lib/context-filters/version.rb +5 -0
- data/test/context-filters/context_test.rb +62 -0
- data/test/context-filters/filter_test_subject.rb +15 -0
- data/test/context-filters/filters_test.rb +97 -0
- data/test/context-filters/global_context_test.rb +73 -0
- data/test/context-filters/local_context_test.rb +43 -0
- data/test/context-filters/priority_filters_test.rb +65 -0
- data/test/test_helper.rb +33 -0
- metadata +150 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5fefdc786067150db5a30a0f0d3951a8400dfaea
|
4
|
+
data.tar.gz: ceee00abeae9a01b2190f7dca8c903cd1e4125ed
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b54f7a9eb75421d1eccc8ba97b80e823dba1a1748cd1b5c66046f7e133f3f914a029376e84521f6b5b39cded1d9e228c39a8c56e79dbfdb4c29231168408bcec
|
7
|
+
data.tar.gz: dcfd179c6eb7d3965cf28b687fd515ae4003a88d2690b894f38d1a75b5e0712a330ef983e656cbb361a4ce8e536e21293faafbf9a4be0e18a581b2352bf372ac
|
@@ -0,0 +1,23 @@
|
|
1
|
+
=begin
|
2
|
+
Copyright 2014 Michal Papis <mpapis@gmail.com>
|
3
|
+
|
4
|
+
See the file LICENSE for copying permission.
|
5
|
+
=end
|
6
|
+
|
7
|
+
require "context-filters/global_context"
|
8
|
+
require "context-filters/local_context"
|
9
|
+
|
10
|
+
# manipulate set of context and filters for it,
|
11
|
+
# allow evaluating filters in given context
|
12
|
+
class ContextFilters::Context < CommandDesigner::GlobalContext
|
13
|
+
|
14
|
+
include ContextFilters::LocalContext
|
15
|
+
|
16
|
+
# run the given method on global and local filters
|
17
|
+
def evaluate_filters(method)
|
18
|
+
super(method) do
|
19
|
+
evaluate_local_filters(method)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
=begin
|
2
|
+
Copyright 2014 Michal Papis <mpapis@gmail.com>
|
3
|
+
|
4
|
+
See the file LICENSE for copying permission.
|
5
|
+
=end
|
6
|
+
|
7
|
+
require "context-filters/version"
|
8
|
+
|
9
|
+
# Store and apply filters using blocks
|
10
|
+
#
|
11
|
+
# @example
|
12
|
+
#
|
13
|
+
# class FiltersTestSubject
|
14
|
+
# attr_reader :value
|
15
|
+
# def initialize(value)
|
16
|
+
# @value = value
|
17
|
+
# end
|
18
|
+
# def change(&block)
|
19
|
+
# @value = block.call(@value)
|
20
|
+
# end
|
21
|
+
# end
|
22
|
+
# filters = ContextFilters::Filters.new
|
23
|
+
# filters.store(:addition) {|value| value + 1 }
|
24
|
+
# filters.store(:subtraction) {|value| value - 1 }
|
25
|
+
# filters.filters # => [:addition, :subtraction]
|
26
|
+
# object = FiltersTestSubject.new(3)
|
27
|
+
# object.value => 3
|
28
|
+
# filters.apply(object.method(:change), :addition)
|
29
|
+
# object.value => 4
|
30
|
+
# filters.apply(object.method(:change), :subtraction)
|
31
|
+
# object.value => 3
|
32
|
+
|
33
|
+
class ContextFilters::Filters
|
34
|
+
|
35
|
+
# initialize the filters storage
|
36
|
+
def initialize
|
37
|
+
@filters = {}
|
38
|
+
end
|
39
|
+
|
40
|
+
# stores the block for given options, if the options have a block
|
41
|
+
# already the new one is added to the list
|
42
|
+
# @param options [Object] options for filtering blocks
|
43
|
+
# @param block [Proc] block of code to add to the list of blocks
|
44
|
+
# for this options
|
45
|
+
def store(options = nil, &block)
|
46
|
+
@filters[options] ||= []
|
47
|
+
@filters[options] << block
|
48
|
+
end
|
49
|
+
|
50
|
+
# applies matching filters to the given method
|
51
|
+
# @param method [Method] an object method that takes a transformation
|
52
|
+
# block as param
|
53
|
+
# @param options [Object] a filter for selecting matching blocks
|
54
|
+
def apply(method, options = {})
|
55
|
+
@filters.fetch(options, []).each{|block| method.call(&block) }
|
56
|
+
end
|
57
|
+
|
58
|
+
# Array of already defined filters
|
59
|
+
def filters
|
60
|
+
@filters.keys
|
61
|
+
end
|
62
|
+
|
63
|
+
# @return [Boolean] true if there are any rules stored, false otherwise
|
64
|
+
def empty?
|
65
|
+
@filters.empty?
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
=begin
|
2
|
+
Copyright 2014 Michal Papis <mpapis@gmail.com>
|
3
|
+
|
4
|
+
See the file LICENSE for copying permission.
|
5
|
+
=end
|
6
|
+
|
7
|
+
require "context-filters/priority_filters"
|
8
|
+
|
9
|
+
# builds list of filters and provides dsl for building nested context
|
10
|
+
# and allows evaluating filters on methods in the current context
|
11
|
+
class ContextFilters::GlobalContext
|
12
|
+
|
13
|
+
# @return [Array] the context stack
|
14
|
+
attr_reader :context
|
15
|
+
|
16
|
+
# @return [PriorityFilters] shared list of filters
|
17
|
+
attr_reader :priority_filters
|
18
|
+
|
19
|
+
# initialize new GlobalContext, works in two modes:
|
20
|
+
# 1. start totally new context, takes one param - array of priorities,
|
21
|
+
# +nil+ to use one anonymous priority
|
22
|
+
# 2. build sub context, params: global_filters_list, parents_context,
|
23
|
+
# value to add to context
|
24
|
+
#
|
25
|
+
# @param priority_filters [Array,PriorityFilters] when PriorityFilters - uses it for priority_filters
|
26
|
+
# otherwise - initializes new priority_filters with it
|
27
|
+
# @param context [Array] parents context, duplicates to initialize own context
|
28
|
+
# @param options [Object] new context, ads it to current context
|
29
|
+
#
|
30
|
+
def initialize(priority_filters = nil, context = [], options = nil)
|
31
|
+
if ContextFilters::PriorityFilters === priority_filters
|
32
|
+
then @priority_filters = priority_filters
|
33
|
+
else @priority_filters = ContextFilters::PriorityFilters.new(priority_filters)
|
34
|
+
end
|
35
|
+
@context = context.dup + [options]
|
36
|
+
end
|
37
|
+
|
38
|
+
# defines new filter for given +priority+ and +options+
|
39
|
+
#
|
40
|
+
# @param priority [nil, Object] has to correspond to one of the initialized priorities
|
41
|
+
# @param options [Object] the options to use for new filter
|
42
|
+
# @param block [Proc] the transformation to use when the options match
|
43
|
+
#
|
44
|
+
def filter(priority, options = nil, &block)
|
45
|
+
@priority_filters.store(priority, options, &block)
|
46
|
+
end
|
47
|
+
|
48
|
+
# starts new context
|
49
|
+
# @param options [Object] options to start new context
|
50
|
+
# @param block [Proc] code block that will enable filtering for the given +options+
|
51
|
+
# @yield [GlobalContext] the new context
|
52
|
+
def in_context(options, &block)
|
53
|
+
self.class.new(@priority_filters, @context, options).tap(&block)
|
54
|
+
end
|
55
|
+
|
56
|
+
# evaluates all matching filters for given context, allows to do extra
|
57
|
+
# work for +priority.nil?+ or on the end of the priorities,
|
58
|
+
# @param method [Proc] the method to evaluate with filters matching current context
|
59
|
+
# @yield on first +priority.nil?+ or on the end when none
|
60
|
+
def evaluate_filters(method)
|
61
|
+
local_called = false
|
62
|
+
|
63
|
+
@priority_filters.each do |priority, filters|
|
64
|
+
|
65
|
+
@context.each { |options| filters.apply(method, options) }
|
66
|
+
|
67
|
+
if priority.nil? && block_given? && !local_called
|
68
|
+
yield
|
69
|
+
local_called = true
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
|
74
|
+
yield if block_given? && !local_called
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
=begin
|
2
|
+
Copyright 2014 Michal Papis <mpapis@gmail.com>
|
3
|
+
|
4
|
+
See the file LICENSE for copying permission.
|
5
|
+
=end
|
6
|
+
|
7
|
+
require "context-filters/version"
|
8
|
+
|
9
|
+
# allow defining local filters and evaluating code in context of thems
|
10
|
+
module ContextFilters::LocalContext
|
11
|
+
|
12
|
+
# @return [Array<Proc>] list of blocks to evaluate
|
13
|
+
def local_filters
|
14
|
+
@local_filters ||= []
|
15
|
+
end
|
16
|
+
|
17
|
+
# temporarly adds +filter_block+ to the list of filters to run and
|
18
|
+
# yields given block of code
|
19
|
+
#
|
20
|
+
# @param filter_block [Proc] a block of code to add to the list
|
21
|
+
# @yield a block in which +local_filters+ temporarly includes
|
22
|
+
# +filter_block+
|
23
|
+
def local_filter(filter_block, &block)
|
24
|
+
local_filters.push(filter_block)
|
25
|
+
block.call
|
26
|
+
ensure
|
27
|
+
local_filters.pop
|
28
|
+
nil
|
29
|
+
end
|
30
|
+
|
31
|
+
# iterates over +local_filters+ and applies them to the given +method+
|
32
|
+
#
|
33
|
+
# @param method [Proc] a method to call with each filter stored in
|
34
|
+
# +local_filters+
|
35
|
+
def evaluate_local_filters(method)
|
36
|
+
local_filters.each { |block| method.call(&block) }
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
=begin
|
2
|
+
Copyright 2014 Michal Papis <mpapis@gmail.com>
|
3
|
+
|
4
|
+
See the file LICENSE for copying permission.
|
5
|
+
=end
|
6
|
+
|
7
|
+
require "context-filters/filters"
|
8
|
+
|
9
|
+
# list of +filters+ sorted by +priorities+
|
10
|
+
class ContextFilters::PriorityFilters
|
11
|
+
|
12
|
+
attr_reader :priorities
|
13
|
+
|
14
|
+
# initializes priorities and coresponding list of filters
|
15
|
+
# @param priorities [Array|Object] a list of priorities to order filters
|
16
|
+
def initialize(priorities = nil)
|
17
|
+
@priorities = [priorities].flatten.freeze
|
18
|
+
@filters_array = @priorities.product([ContextFilters::Filters.new])
|
19
|
+
end
|
20
|
+
|
21
|
+
# adds a priority filter
|
22
|
+
#
|
23
|
+
# @param priority [Object] anything that was part of +priorities+ array
|
24
|
+
# @param options [Object] forwarded to Filters.store
|
25
|
+
# @param block [Proc] forwarded to Filters.store
|
26
|
+
# @raise [KeyError] when priority not matching priorities is used
|
27
|
+
def store(priority, options = nil, &block)
|
28
|
+
found = @filters_array.assoc(priority)
|
29
|
+
raise KeyError if found.nil?
|
30
|
+
found.last.store(options, &block)
|
31
|
+
end
|
32
|
+
|
33
|
+
# list of +filters+ sorted by +priorities+
|
34
|
+
def to_a
|
35
|
+
@filters_array
|
36
|
+
end
|
37
|
+
|
38
|
+
# iterate over +filters+ ordered by +priority+
|
39
|
+
# @yield [priority,filters] the next filters from sorted array
|
40
|
+
# @yieldparam priority [Object] the priority
|
41
|
+
# @yieldparam filters [Filters] the filters for priority
|
42
|
+
def each(&block)
|
43
|
+
to_a.each(&block) unless empty?
|
44
|
+
end
|
45
|
+
|
46
|
+
# check if all of the filters are empty
|
47
|
+
# return [Bolean] true if all filters are empty
|
48
|
+
def empty?
|
49
|
+
@filters_array.map(&:last).all?(&:empty?)
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
=begin
|
2
|
+
Copyright 2014 Michal Papis <mpapis@gmail.com>
|
3
|
+
|
4
|
+
See the file LICENSE for copying permission.
|
5
|
+
=end
|
6
|
+
|
7
|
+
require "test_helper"
|
8
|
+
require "context-filters/context"
|
9
|
+
require "context-filters/filter_test_subject"
|
10
|
+
|
11
|
+
describe ContextFilters::Context do
|
12
|
+
|
13
|
+
subject do
|
14
|
+
ContextFilters::Context.new
|
15
|
+
end
|
16
|
+
|
17
|
+
let(:filter_test_subject) do
|
18
|
+
FilterTestSubject.new(3)
|
19
|
+
end
|
20
|
+
|
21
|
+
let(:change_method) do
|
22
|
+
filter_test_subject.method(:change)
|
23
|
+
end
|
24
|
+
|
25
|
+
describe "#evaluate_filters" do
|
26
|
+
|
27
|
+
it "does not apply filters when no filters" do
|
28
|
+
subject.priority_filters.expects(:apply).never
|
29
|
+
subject.evaluate_filters(Proc.new{})
|
30
|
+
end
|
31
|
+
|
32
|
+
it "does apply global filters" do
|
33
|
+
method = Proc.new{}
|
34
|
+
subject.context << :a
|
35
|
+
subject.filter(nil, :b) { true }
|
36
|
+
subject.priority_filters.to_a[0][1].expects(:apply).once.with(method, nil)
|
37
|
+
subject.priority_filters.to_a[0][1].expects(:apply).once.with(method, :a)
|
38
|
+
subject.evaluate_filters(method)
|
39
|
+
end
|
40
|
+
|
41
|
+
it "does apply local filters" do
|
42
|
+
subject.stubs(:local_filters).returns([Proc.new{|value| value+4}])
|
43
|
+
subject.evaluate_filters(change_method)
|
44
|
+
filter_test_subject.value.must_equal(7)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "does apple global and local filters" do
|
48
|
+
filter_test_subject.value.must_equal(3)
|
49
|
+
addition = Proc.new { |value| value+1 }
|
50
|
+
multiplication = Proc.new { |value| value*3 }
|
51
|
+
|
52
|
+
subject.filter(nil,&multiplication)
|
53
|
+
subject.local_filter(addition) do
|
54
|
+
subject.evaluate_filters(change_method)
|
55
|
+
end
|
56
|
+
|
57
|
+
filter_test_subject.value.must_equal(10)
|
58
|
+
end
|
59
|
+
|
60
|
+
end #evaluate_command
|
61
|
+
|
62
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
=begin
|
2
|
+
Copyright 2014 Michal Papis <mpapis@gmail.com>
|
3
|
+
|
4
|
+
See the file LICENSE for copying permission.
|
5
|
+
=end
|
6
|
+
|
7
|
+
class FilterTestSubject
|
8
|
+
attr_accessor :value
|
9
|
+
def initialize(value)
|
10
|
+
@value = value
|
11
|
+
end
|
12
|
+
def change(&block)
|
13
|
+
@value = block.call(@value)
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,97 @@
|
|
1
|
+
=begin
|
2
|
+
Copyright 2014 Michal Papis <mpapis@gmail.com>
|
3
|
+
|
4
|
+
See the file LICENSE for copying permission.
|
5
|
+
=end
|
6
|
+
|
7
|
+
require "test_helper"
|
8
|
+
require "context-filters/filters"
|
9
|
+
require "context-filters/filter_test_subject"
|
10
|
+
|
11
|
+
describe ContextFilters::Filters do
|
12
|
+
|
13
|
+
subject do
|
14
|
+
ContextFilters::Filters.new
|
15
|
+
end
|
16
|
+
|
17
|
+
describe "#store" do
|
18
|
+
|
19
|
+
it "adds empty filter" do
|
20
|
+
subject.store() { true }
|
21
|
+
subject.filters.size.must_equal 1
|
22
|
+
subject.filters.first.must_equal(nil)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "adds options filter" do
|
26
|
+
subject.store(x: 2) { true }
|
27
|
+
subject.filters.size.must_equal 1
|
28
|
+
subject.filters.first.must_equal({x:2})
|
29
|
+
end
|
30
|
+
|
31
|
+
it "adds hash filter" do
|
32
|
+
subject.store({x: 3}) { true }
|
33
|
+
subject.filters.size.must_equal 1
|
34
|
+
subject.filters.first.must_equal({x:3})
|
35
|
+
end
|
36
|
+
|
37
|
+
it "adds nil filter" do
|
38
|
+
subject.store(nil) { true }
|
39
|
+
subject.filters.size.must_equal 1
|
40
|
+
subject.filters.first.must_equal(nil)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "adds filter block" do
|
44
|
+
subject.store() { 4 }
|
45
|
+
filters = subject.instance_variable_get(:@filters)
|
46
|
+
filters.size.must_equal 1
|
47
|
+
filters[nil].size.must_equal 1
|
48
|
+
filters[nil].first.call.must_equal(4)
|
49
|
+
end
|
50
|
+
|
51
|
+
end #store
|
52
|
+
|
53
|
+
describe "#apply" do
|
54
|
+
|
55
|
+
let(:apply_test_subject) do
|
56
|
+
FilterTestSubject.new("test me")
|
57
|
+
end
|
58
|
+
|
59
|
+
it "does not apply filter" do
|
60
|
+
subject.store({x: 1}) { |value| "better #{value}" }
|
61
|
+
|
62
|
+
subject.apply(apply_test_subject.method(:change), {x: 2})
|
63
|
+
|
64
|
+
apply_test_subject.value.must_equal("test me")
|
65
|
+
end
|
66
|
+
|
67
|
+
it "applies single filter" do
|
68
|
+
subject.store({x: 1}) { |value| "better #{value}" }
|
69
|
+
|
70
|
+
subject.apply(apply_test_subject.method(:change), {x: 1})
|
71
|
+
|
72
|
+
apply_test_subject.value.must_equal("better test me")
|
73
|
+
end
|
74
|
+
|
75
|
+
it "applies repeating filters" do
|
76
|
+
subject.store({x: 1}) { |value| "better #{value}" }
|
77
|
+
subject.store({x: 1}) { |value| "#{value} please" }
|
78
|
+
subject.store({x: 1}) { |value| "#{value}!" }
|
79
|
+
|
80
|
+
subject.apply(apply_test_subject.method(:change), {x: 1})
|
81
|
+
|
82
|
+
apply_test_subject.value.must_equal("better test me please!")
|
83
|
+
end
|
84
|
+
|
85
|
+
it "applies different filters" do
|
86
|
+
subject.store({x: 1}) { |value| "dont #{value}" }
|
87
|
+
subject.store({x: 2}) { |value| "#{value} now" }
|
88
|
+
|
89
|
+
subject.apply(apply_test_subject.method(:change), {x: 1})
|
90
|
+
subject.apply(apply_test_subject.method(:change), {x: 2})
|
91
|
+
|
92
|
+
apply_test_subject.value.must_equal("dont test me now")
|
93
|
+
end
|
94
|
+
|
95
|
+
end #apply
|
96
|
+
|
97
|
+
end
|
@@ -0,0 +1,73 @@
|
|
1
|
+
=begin
|
2
|
+
Copyright 2014 Michal Papis <mpapis@gmail.com>
|
3
|
+
|
4
|
+
See the file LICENSE for copying permission.
|
5
|
+
=end
|
6
|
+
|
7
|
+
require "test_helper"
|
8
|
+
require "context-filters/global_context"
|
9
|
+
|
10
|
+
describe ContextFilters::GlobalContext do
|
11
|
+
|
12
|
+
subject do
|
13
|
+
ContextFilters::GlobalContext.new
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#initialize" do
|
17
|
+
|
18
|
+
it "sets up initial variables" do
|
19
|
+
subject.priority_filters.must_be_kind_of ContextFilters::PriorityFilters
|
20
|
+
subject.priority_filters.must_be_empty
|
21
|
+
subject.context.must_equal([nil])
|
22
|
+
end
|
23
|
+
|
24
|
+
end #initialize
|
25
|
+
|
26
|
+
it "stores filters" do
|
27
|
+
subject.priority_filters.expects(:store).with(nil, :a).once
|
28
|
+
subject.filter(nil, :a) do true end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "#evaluate_filters" do
|
32
|
+
|
33
|
+
it "does not apply filters when no filters" do
|
34
|
+
subject.priority_filters.expects(:apply).never
|
35
|
+
subject.evaluate_filters(Proc.new{})
|
36
|
+
end
|
37
|
+
|
38
|
+
it "does apply filters" do
|
39
|
+
method = Proc.new{}
|
40
|
+
subject.context << :a
|
41
|
+
subject.filter(nil, :b) { true }
|
42
|
+
subject.priority_filters.to_a[0][1].expects(:apply).once.with(method, nil)
|
43
|
+
subject.priority_filters.to_a[0][1].expects(:apply).once.with(method, :a)
|
44
|
+
subject.evaluate_filters(method)
|
45
|
+
end
|
46
|
+
|
47
|
+
end #evaluate_command
|
48
|
+
|
49
|
+
describe "#group" do
|
50
|
+
|
51
|
+
it "nests" do
|
52
|
+
subject.in_context(:a) do |test_a|
|
53
|
+
|
54
|
+
test_a.must_be_kind_of ContextFilters::GlobalContext
|
55
|
+
test_a.priority_filters.object_id.must_equal(subject.priority_filters.object_id)
|
56
|
+
test_a.context.object_id.wont_equal(subject.context.object_id)
|
57
|
+
test_a.context.must_equal([nil, :a])
|
58
|
+
|
59
|
+
test_a.in_context(:b) do |test_b|
|
60
|
+
test_b.must_be_kind_of ContextFilters::GlobalContext
|
61
|
+
test_b.priority_filters.object_id.must_equal(test_a.priority_filters.object_id)
|
62
|
+
test_b.context.object_id.wont_equal(test_a.context.object_id)
|
63
|
+
test_b.context.must_equal([nil, :a, :b])
|
64
|
+
end
|
65
|
+
|
66
|
+
test_a.context.must_equal([nil, :a])
|
67
|
+
end
|
68
|
+
subject.context.must_equal([nil])
|
69
|
+
end
|
70
|
+
|
71
|
+
end #group
|
72
|
+
|
73
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
=begin
|
2
|
+
Copyright 2014 Michal Papis <mpapis@gmail.com>
|
3
|
+
|
4
|
+
See the file LICENSE for copying permission.
|
5
|
+
=end
|
6
|
+
|
7
|
+
require "test_helper"
|
8
|
+
require "context-filters/local_context"
|
9
|
+
require "context-filters/filter_test_subject"
|
10
|
+
|
11
|
+
describe ContextFilters::LocalContext do
|
12
|
+
|
13
|
+
subject do
|
14
|
+
Object.new.tap { |o| o.extend(ContextFilters::LocalContext) }
|
15
|
+
end
|
16
|
+
|
17
|
+
let(:filter_test_subject) do
|
18
|
+
FilterTestSubject.new(3)
|
19
|
+
end
|
20
|
+
|
21
|
+
let(:change_method) do
|
22
|
+
filter_test_subject.method(:change)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "has default value for #local_filters" do
|
26
|
+
subject.local_filters.must_equal([])
|
27
|
+
end
|
28
|
+
|
29
|
+
it "adds local filters" do
|
30
|
+
subject.local_filter(change_method) do
|
31
|
+
subject.local_filters.must_equal([change_method])
|
32
|
+
end
|
33
|
+
subject.local_filters.must_equal([])
|
34
|
+
end
|
35
|
+
|
36
|
+
it "runs change" do
|
37
|
+
method = Proc.new { |value| value+4 }
|
38
|
+
subject.stubs(:local_filters).returns([method])
|
39
|
+
subject.evaluate_local_filters(change_method)
|
40
|
+
filter_test_subject.value.must_equal(7)
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
=begin
|
2
|
+
Copyright 2014 Michal Papis <mpapis@gmail.com>
|
3
|
+
|
4
|
+
See the file LICENSE for copying permission.
|
5
|
+
=end
|
6
|
+
|
7
|
+
require "test_helper"
|
8
|
+
require "context-filters/priority_filters"
|
9
|
+
|
10
|
+
describe ContextFilters::PriorityFilters do
|
11
|
+
|
12
|
+
subject do
|
13
|
+
ContextFilters::PriorityFilters.allocate
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#initialize" do
|
17
|
+
|
18
|
+
it "initializes with no args" do
|
19
|
+
subject.send(:initialize)
|
20
|
+
subject.priorities.must_equal([nil])
|
21
|
+
end
|
22
|
+
|
23
|
+
it "initializes with objects" do
|
24
|
+
subject.send(:initialize, :a)
|
25
|
+
subject.priorities.must_equal([:a])
|
26
|
+
end
|
27
|
+
|
28
|
+
it "initializes with array" do
|
29
|
+
subject.send(:initialize, [:a, nil, :b])
|
30
|
+
subject.priorities.must_equal([:a, nil, :b])
|
31
|
+
end
|
32
|
+
|
33
|
+
it "initializes to_a" do
|
34
|
+
subject.send(:initialize, [:a, :b])
|
35
|
+
subject.to_a.size.must_equal(2)
|
36
|
+
subject.to_a.map(&:first).must_equal([:a, :b])
|
37
|
+
subject.to_a[0][1].must_be_kind_of ContextFilters::Filters
|
38
|
+
subject.to_a[1][1].must_be_kind_of ContextFilters::Filters
|
39
|
+
end
|
40
|
+
|
41
|
+
end #initialize
|
42
|
+
|
43
|
+
describe "#store" do
|
44
|
+
|
45
|
+
it "stores filters" do
|
46
|
+
subject.send(:initialize, :a)
|
47
|
+
subject.store(:a, :options) {true}
|
48
|
+
subject.to_a[0][1].filters.must_equal([:options])
|
49
|
+
end
|
50
|
+
|
51
|
+
it "throws exception on wrong priority" do
|
52
|
+
subject.send(:initialize, :a)
|
53
|
+
lambda {
|
54
|
+
subject.store(:b, :options) {true}
|
55
|
+
}.must_raise(KeyError)
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
|
60
|
+
it "returns list in to_a" do
|
61
|
+
subject.send(:initialize, [2, 1])
|
62
|
+
subject.to_a.must_be_kind_of Array
|
63
|
+
end
|
64
|
+
|
65
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
=begin
|
2
|
+
Copyright 2014 Michal Papis <mpapis@gmail.com>
|
3
|
+
|
4
|
+
See the file LICENSE for copying permission.
|
5
|
+
=end
|
6
|
+
|
7
|
+
require "rubygems"
|
8
|
+
|
9
|
+
if
|
10
|
+
RUBY_VERSION == "2.0.0" && # check Gemfile
|
11
|
+
$0 != "-e" # do not do that in guard
|
12
|
+
then
|
13
|
+
require "coveralls"
|
14
|
+
require "simplecov"
|
15
|
+
|
16
|
+
SimpleCov.start do
|
17
|
+
formatter SimpleCov::Formatter::MultiFormatter[
|
18
|
+
SimpleCov::Formatter::HTMLFormatter,
|
19
|
+
Coveralls::SimpleCov::Formatter,
|
20
|
+
]
|
21
|
+
command_name "Unit Tests"
|
22
|
+
add_filter "/test/"
|
23
|
+
end
|
24
|
+
|
25
|
+
Coveralls.noisy = true unless ENV["CI"]
|
26
|
+
end
|
27
|
+
|
28
|
+
# Autoload all lib/**/*.rb files so simplecov does not misses anything
|
29
|
+
Dir[File.expand_path("../../lib/**/*.rb", __FILE__)].each{|f| require f }
|
30
|
+
|
31
|
+
require "minitest/autorun" unless $0=="-e" # skip in guard
|
32
|
+
require "minitest/unit"
|
33
|
+
require "mocha/setup"
|
metadata
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: context-filters
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Michal Papis
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-10-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: guard
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: guard-minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '2.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '2.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: guard-yard
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.1'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.1'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: minitest
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '5.4'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '5.4'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: mocha
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.1'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.1'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ~>
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '10.3'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ~>
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '10.3'
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
- mpapis@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- lib/context-filters.rb
|
105
|
+
- lib/context-filters/context.rb
|
106
|
+
- lib/context-filters/filters.rb
|
107
|
+
- lib/context-filters/global_context.rb
|
108
|
+
- lib/context-filters/local_context.rb
|
109
|
+
- lib/context-filters/priority_filters.rb
|
110
|
+
- lib/context-filters/version.rb
|
111
|
+
- test/context-filters/context_test.rb
|
112
|
+
- test/context-filters/filter_test_subject.rb
|
113
|
+
- test/context-filters/filters_test.rb
|
114
|
+
- test/context-filters/global_context_test.rb
|
115
|
+
- test/context-filters/local_context_test.rb
|
116
|
+
- test/context-filters/priority_filters_test.rb
|
117
|
+
- test/test_helper.rb
|
118
|
+
homepage: https://github.com/remote-exec/context-filters
|
119
|
+
licenses:
|
120
|
+
- MIT
|
121
|
+
metadata: {}
|
122
|
+
post_install_message:
|
123
|
+
rdoc_options: []
|
124
|
+
require_paths:
|
125
|
+
- lib
|
126
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - '>='
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: 1.9.3
|
131
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
132
|
+
requirements:
|
133
|
+
- - '>='
|
134
|
+
- !ruby/object:Gem::Version
|
135
|
+
version: '0'
|
136
|
+
requirements: []
|
137
|
+
rubyforge_project:
|
138
|
+
rubygems_version: 2.2.2
|
139
|
+
signing_key:
|
140
|
+
specification_version: 4
|
141
|
+
summary: Generic support for filters applied in context
|
142
|
+
test_files:
|
143
|
+
- test/test_helper.rb
|
144
|
+
- test/context-filters/filter_test_subject.rb
|
145
|
+
- test/context-filters/context_test.rb
|
146
|
+
- test/context-filters/filters_test.rb
|
147
|
+
- test/context-filters/global_context_test.rb
|
148
|
+
- test/context-filters/local_context_test.rb
|
149
|
+
- test/context-filters/priority_filters_test.rb
|
150
|
+
has_rdoc:
|