Class: Ro::Collection
- Inherits:
-
Object
show all
- Includes:
- Enumerable, Klass
- Defined in:
- lib/ro/collection.rb,
lib/ro/collection/list.rb
Defined Under Namespace
Classes: List, Page
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from Klass
included
Constructor Details
Returns a new instance of Collection.
8
9
10
11
|
# File 'lib/ro/collection.rb', line 8
def initialize(path)
@path = Path.for(path)
@root = Root.for(@path.parent)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, **kws, &block) ⇒ Object
207
208
209
|
# File 'lib/ro/collection.rb', line 207
def method_missing(name, *args, **kws, &block)
get(name) || super
end
|
Instance Attribute Details
Returns the value of attribute path.
6
7
8
|
# File 'lib/ro/collection.rb', line 6
def path
@path
end
|
Returns the value of attribute root.
6
7
8
|
# File 'lib/ro/collection.rb', line 6
def root
@root
end
|
Instance Method Details
199
200
201
|
# File 'lib/ro/collection.rb', line 199
def [](name)
get(name)
end
|
#each(offset: nil, limit: nil, &block) ⇒ Object
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
# File 'lib/ro/collection.rb', line 45
def each(offset:nil, limit:nil, &block)
accum = []
if offset
i = -1
n = 0
subdirectories do |subdirectory|
i += 1
next if i < offset
node = node_for(subdirectory)
block ? block.call(node) : accum.push(node)
n += 1
break if limit && n >= limit
end
else
subdirectories do |subdirectory|
node = node_for(subdirectory)
block ? block.call(node) : accum.push(node)
end
end
block ? self : accum
end
|
#first(*args) ⇒ Object
161
162
163
164
165
166
167
|
# File 'lib/ro/collection.rb', line 161
def first(*args)
if args.size.zero?
node_for(subdirectories.first)
else
subdirectories.first(*args).map{|subdirectory| node_for(subdirectory)}
end
end
|
#get(name) ⇒ Object
189
190
191
192
193
194
195
196
197
|
# File 'lib/ro/collection.rb', line 189
def get(name)
paths_for(name).each do |path|
next unless path.exist?
return node_for(path)
end
nil
end
|
17
18
19
|
# File 'lib/ro/collection.rb', line 17
def id
name
end
|
#identifier ⇒ Object
25
26
27
|
# File 'lib/ro/collection.rb', line 25
def identifier
type
end
|
29
30
31
|
# File 'lib/ro/collection.rb', line 29
def inspect
identifier
end
|
#last(*args) ⇒ Object
169
170
171
172
173
174
175
|
# File 'lib/ro/collection.rb', line 169
def last(*args)
if args.size.zero?
node_for(subdirectories.last)
else
subdirectories.last(*args).map{|subdirectory| node_for(subdirectory)}
end
end
|
#load(&block) ⇒ Object
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
# File 'lib/ro/collection.rb', line 100
def load(&block)
n = 8
q = Queue.new o = Queue.new
producer =
Thread.new do
Thread.current.abort_on_exception = true
subdirectories do |subdirectory|
q.push(subdirectory)
end
end
loaders =
n.times.map do
Thread.new do
Thread.current.abort_on_exception = true
loop do
subdirectory = q.pop
begin
node = node_for(subdirectory)
o.push(node)
rescue => e
o.push(e) nil end
end
end
end
accum = []
consumer =
Thread.new do
Thread.current.abort_on_exception = true
loop do
node = o.pop
block ? block.call(node) : accum.push(node)
end
end
producer.join
loaders.map{|loader| loader.join}
consumer.join
block ? self : accum
end
|
13
14
15
|
# File 'lib/ro/collection.rb', line 13
def name
@path.name
end
|
#node_for(path) ⇒ Object
33
34
35
|
# File 'lib/ro/collection.rb', line 33
def node_for(path)
Node.new(path)
end
|
#page(number, size: 10) ⇒ Object
78
79
80
81
82
83
84
|
# File 'lib/ro/collection.rb', line 78
def page(number, size: 10)
offset = [(number - 1), 0].max * size
limit = [size, 1].max
nodes = each(offset:, limit:)
Page.new(nodes, number:)
end
|
#paginate(size: 10, &block) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/ro/collection.rb', line 86
def paginate(size: 10, &block)
number = 0
accum = []
loop do
number += 1
page = self.page(number, size:)
break if page.empty?
block ? block.call(page) : accum.push(page)
end
block ? self : accum
end
|
#paths_for(name) ⇒ Object
181
182
183
184
185
186
187
|
# File 'lib/ro/collection.rb', line 181
def paths_for(name)
[
subdirectory_for(name),
subdirectory_for(Slug.for(name, :join => '-')),
subdirectory_for(Slug.for(name, :join => '_')),
]
end
|
177
178
179
|
# File 'lib/ro/collection.rb', line 177
def size
subdirectories.size
end
|
203
204
205
|
# File 'lib/ro/collection.rb', line 203
def slice(...)
subdirectories.slice(...).map{|subdirectory| node_for(subdirectory)}
end
|
#subdirectories ⇒ Object
37
38
39
|
# File 'lib/ro/collection.rb', line 37
def subdirectories(...)
@path.subdirectories(...)
end
|
#subdirectory_for(name) ⇒ Object
41
42
43
|
# File 'lib/ro/collection.rb', line 41
def subdirectory_for(name)
@path.subdirectory_for(name)
end
|
#to_array ⇒ Object
Also known as:
to_a, all, nodes
151
152
153
|
# File 'lib/ro/collection.rb', line 151
def to_array(...)
each(...)
end
|
21
22
23
|
# File 'lib/ro/collection.rb', line 21
def type
name
end
|