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
-
#[](name) ⇒ Object
-
#each(offset: nil, limit: nil, &block) ⇒ Object
T020: Modified to discover nodes from metadata files (both new and old structure).
-
#first(*args) ⇒ Object
-
#get(name) ⇒ Object
T022: Modified to find nodes by metadata filename.
-
#id ⇒ Object
-
#identifier ⇒ Object
-
#initialize(path) ⇒ Collection
constructor
A new instance of Collection.
-
#inspect ⇒ Object
-
#last(*args) ⇒ Object
-
#load(&block) ⇒ Object
-
#metadata_files ⇒ Object
T021: Scan for metadata files in both new and old structure formats Returns array of hashes: [‘node-id’, path: Path, type: :new|:old].
-
#method_missing(name, *args, **kws, &block) ⇒ Object
-
#name ⇒ Object
-
#node_for(path) ⇒ Object
-
#page(number, size: 10) ⇒ Object
-
#paginate(size: 10, &block) ⇒ Object
-
#paths_for(name) ⇒ Object
-
#size ⇒ Object
-
#slice ⇒ Object
-
#subdirectories ⇒ Object
-
#subdirectory_for(name) ⇒ Object
-
#to_array(offset: nil, limit: nil) ⇒ Object
(also: #to_a, #all, #nodes)
-
#type ⇒ Object
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
281
282
283
|
# File 'lib/ro/collection.rb', line 281
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
273
274
275
|
# File 'lib/ro/collection.rb', line 273
def [](name)
get(name)
end
|
#each(offset: nil, limit: nil, &block) ⇒ Object
T020: Modified to discover nodes from metadata files (both new and old structure)
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/ro/collection.rb', line 81
def each(offset:nil, limit:nil, &block)
return to_enum(:each, offset: offset, limit: limit) unless block_given?
metadata_entries = metadata_files
if offset
i = -1
n = 0
metadata_entries.each do |entry|
i += 1
next if i < offset
node = if entry[:type] == :new
Node.new(self, entry[:path])
else
Node.new(entry[:path].parent)
end
block.call(node)
n += 1
break if limit && n >= limit
end
else
metadata_entries.each do |entry|
node = if entry[:type] == :new
Node.new(self, entry[:path])
else
Node.new(entry[:path].parent)
end
block.call(node)
end
end
self
end
|
#first(*args) ⇒ Object
218
219
220
221
222
223
224
|
# File 'lib/ro/collection.rb', line 218
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
T022: Modified to find nodes by metadata filename
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
# File 'lib/ro/collection.rb', line 247
def get(name)
extensions = %w[yml yaml json toml]
extensions.each do |ext|
metadata_file = @path.join("#{name}.#{ext}")
if metadata_file.exist? && metadata_file.file?
return Node.new(self, metadata_file)
end
end
[
Slug.for(name, :join => '-'),
Slug.for(name, :join => '_')
].each do |slug|
extensions.each do |ext|
metadata_file = @path.join("#{slug}.#{ext}")
if metadata_file.exist? && metadata_file.file?
return Node.new(self, metadata_file)
end
end
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
226
227
228
229
230
231
232
|
# File 'lib/ro/collection.rb', line 226
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
|
# File 'lib/ro/collection.rb', line 155
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
|
T021: Scan for metadata files in both new and old structure formats Returns array of hashes: [‘node-id’, path: Path, type: :new|:old]
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
|
# File 'lib/ro/collection.rb', line 39
def metadata_files
extensions = %w[yml yaml json toml]
metadata_map = {}
extensions.each do |ext|
@path.glob("*.#{ext}").each do |file|
next unless file.file?
node_id = file.basename.to_s.sub(/\.(yml|yaml|json|toml)$/, '')
metadata_map[node_id] ||= {id: node_id, path: file, type: :new}
end
end
subdirectories.each do |subdir|
node_id = subdir.basename.to_s
extensions.each do |ext|
attributes_file = subdir.join("attributes.#{ext}")
if attributes_file.exist? && attributes_file.file?
metadata_map[node_id] ||= {id: node_id, path: attributes_file, type: :old}
break
end
end
end
metadata_map.values.sort_by { |m| m[:id] }
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
133
134
135
136
137
138
139
|
# File 'lib/ro/collection.rb', line 133
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
141
142
143
144
145
146
147
148
149
150
151
152
153
|
# File 'lib/ro/collection.rb', line 141
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
238
239
240
241
242
243
244
|
# File 'lib/ro/collection.rb', line 238
def paths_for(name)
[
subdirectory_for(name),
subdirectory_for(Slug.for(name, :join => '-')),
subdirectory_for(Slug.for(name, :join => '_')),
]
end
|
234
235
236
|
# File 'lib/ro/collection.rb', line 234
def size
subdirectories.size
end
|
277
278
279
|
# File 'lib/ro/collection.rb', line 277
def slice(...)
subdirectories.slice(...).map{|subdirectory| node_for(subdirectory)}
end
|
#subdirectories ⇒ Object
72
73
74
|
# File 'lib/ro/collection.rb', line 72
def subdirectories(...)
@path.subdirectories(...)
end
|
#subdirectory_for(name) ⇒ Object
76
77
78
|
# File 'lib/ro/collection.rb', line 76
def subdirectory_for(name)
@path.subdirectory_for(name)
end
|
#to_array(offset: nil, limit: nil) ⇒ Object
Also known as:
to_a, all, nodes
206
207
208
209
210
|
# File 'lib/ro/collection.rb', line 206
def to_array(offset: nil, limit: nil)
accum = []
each(offset: offset, limit: limit) { |node| accum << node }
accum
end
|
21
22
23
|
# File 'lib/ro/collection.rb', line 21
def type
name
end
|