-
Notifications
You must be signed in to change notification settings - Fork 30
/
range_filter.rb
106 lines (91 loc) · 2.06 KB
/
range_filter.rb
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
module Pacer
module Routes
module RouteOperations
def range(from, to)
args = { :filter => :range }
args[:begin] = from if from
args[:end] = to if to
chain_route args
end
def limit(max)
chain_route :filter => :range, :limit => max
end
alias take limit
def offset(amount)
chain_route :filter => :range, :offset => amount
end
alias drop offset
def at(pos)
chain_route :filter => :range, :index => pos
end
end
end
module Filter
module RangeFilter
def limit(n = nil)
@limit = n
if range.begin == -1
@range = range.begin...n
else
@range = range.begin...(range.begin + n)
end
self
end
def limit=(n)
limit n
n
end
def offset(n = nil)
s = n
s += 1 if range.begin == -1
if range.end == -1
@range = (range.begin + s)..-1
elsif range.exclude_end?
@range = (range.begin + s)...(range.end + n)
else
@range = (range.begin + s)..(range.end + n)
end
self
end
def offset=(n)
offset n
n
end
def range=(range)
@range = range
end
def begin=(n)
@range = n..range.end
end
def end=(n)
@range = range.begin..n
end
def index=(index)
@range = index..index
end
def range
@range ||= -1..-1
end
protected
def attach_pipe(end_pipe)
from = @range.begin
to = @range.end
if @range.exclude_end?
if to == 0
pipe = Pacer::Pipes::NeverPipe.new
pipe.set_starts end_pipe if end_pipe
return pipe
elsif to > 0
to -= 1
end
end
pipe = Pacer::Pipes::RangeFilterPipe.new from, to
pipe.set_starts end_pipe if end_pipe
pipe
end
def inspect_string
"#{ inspect_class_name }(#{ range.inspect })"
end
end
end
end