Class: Pipeliner::FilterManager

Inherits:
Object
  • Object
show all
Defined in:
lib/pipeliner/FilterManager.rb

Instance Method Summary collapse

Constructor Details

#initializeFilterManager

Create a new FilterManager



7
8
9
10
# File 'lib/pipeliner/FilterManager.rb', line 7

def initialize
    @filters = {}
    @lock = Mutex.new
end

Instance Method Details

#add(type, filter = nil, &block) ⇒ Object

type

Object type to apply filter to

filter

Pipeline::Filter to add

Add a Filter to be applied to the given types



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/pipeliner/FilterManager.rb', line 15

def add(type, filter=nil, &block)
    if((filter.nil? && !block_given?) || (filter && !filter.is_a?(Filter)))
        raise ArgumentError.new('Filter or proc must be provided for filter')
    end
    const = Splib.find_const(type)
    type = const unless const.nil?
    @lock.synchronize do
        @filters[type] ||= {}
    end
    if(block_given?)
        unless(block.arity == 1 || block.arity < 0)
            raise ArgumentError.new('Block must accept a parameter')
        end
        @lock.synchronize do
            @filters[type][:procs] ||= []
            unless(@filters[type][:procs].include?(block))
                @filters[type][:procs] << block
            end
        end
    end
    if(filter)
        @lock.synchronize do
            unless(@filters[type].include?(filter))
                @filters[type][:filters] ||= []
                unless(@filters[type][:filters].include?(filter))
                    @filters[type][:filters] << filter
                end
            end
        end
    end
    filter ? block_given? ? [filter, block] : filter : block
end

#apply_filters(o) ⇒ Object

o

Object to apply filters to

Applies any Filters applicable to object type



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/pipeliner/FilterManager.rb', line 86

def apply_filters(o)
    @filters.keys.find_all{|type| Splib.type_of?(o, type)}.each do |type|
        @filters[type].each_pair do |k,v|
            begin
                case k
                when :filters
                    v.each{|f|o = f.filter(o)}
                when :procs
                    v.each{|pr|o = pr.call(o)}
                end
            rescue ArgumentError
                # ignore this
            end
        end
    end
    o
end

#clearObject

Remove all filters



105
106
107
# File 'lib/pipeliner/FilterManager.rb', line 105

def clear
    @filters.clear
end

#filters(type = nil) ⇒ Object

type

Object types

Return filters of given type or all filters if not type is supplied



74
75
76
77
78
79
80
81
82
# File 'lib/pipeliner/FilterManager.rb', line 74

def filters(type=nil)
    unless(type)
        @filters.dup
    else
        const = Splib.find_const(type)
        type = const unless const.nil?
        @filters[type] ? @filters[type].dup : nil
    end
end

#remove(filter, type = nil) ⇒ Object

filter

Pipeline::Filter to remove

type

Object type filter is applied to.

Remove Filter from given type. If no type is given all references to the given filter will be removed



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pipeliner/FilterManager.rb', line 52

def remove(filter, type=nil)
    if(type)
        const = Splib.find_const(type)
        type = const unless const.nil?
    end
    @lock.synchronize do
        (type ? [@filters[type]] : @filters.values).each do |set|
            [:filters, :procs].each do |t|
                if(set[t])
                    set[t].delete_if{|v| v == filter}
                    set.delete(t) if set[t].empty?
                end
            end
        end
        @filters.delete_if{|k,v|v.empty?}
    end
    nil
end