forked from freeCodeCamp/devdocs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter_stack.rb
More file actions
58 lines (45 loc) · 1.15 KB
/
filter_stack.rb
File metadata and controls
58 lines (45 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
47
48
49
50
51
52
53
54
55
56
57
58
module Docs
class FilterStack
extend Forwardable
def_delegators :@filters, :length, :inspect
attr_reader :filters
def initialize(filters = nil)
@filters = filters ? filters.dup : []
end
def push(*names)
@filters.push *filter_const(names)
end
def insert(index, *names)
@filters.insert assert_index(index), *filter_const(names)
end
alias_method :insert_before, :insert
def insert_after(index, *names)
insert assert_index(index) + 1, *names
end
def replace(index, name)
@filters[assert_index(index)] = filter_const(name)
end
def ==(other)
other.is_a?(self.class) && filters == other.filters
end
def to_a
@filters.dup
end
def inheritable_copy
self.class.new @filters
end
private
def filter_const(name)
if name.is_a? Array
name.map &method(:filter_const)
else
Docs.const_get "#{name}_filter".camelize
end
end
def assert_index(index)
i = index.is_a?(Integer) ? index : @filters.index(filter_const(index))
raise "No such filter to insert: #{index}" unless i
i
end
end
end