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 (new 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 new structure format.
-
#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
245
246
247
|
# File 'lib/ro/collection.rb', line 245
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
237
238
239
|
# File 'lib/ro/collection.rb', line 237
def [](name)
get(name)
end
|
#each(offset: nil, limit: nil, &block) ⇒ Object
T020: Modified to discover nodes from metadata files (new structure)
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
|
# File 'lib/ro/collection.rb', line 60
def each(offset:nil, limit:nil, &block)
return to_enum(:each, offset: offset, limit: limit) unless block_given?
files = metadata_files
if offset
i = -1
n = 0
files.each do |metadata_file|
i += 1
next if i < offset
node = Node.new(self, metadata_file)
block.call(node)
n += 1
break if limit && n >= limit
end
else
files.each do |metadata_file|
node = Node.new(self, metadata_file)
block.call(node)
end
end
self
end
|
#first(*args) ⇒ Object
182
183
184
185
186
187
188
|
# File 'lib/ro/collection.rb', line 182
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
# File 'lib/ro/collection.rb', line 211
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
190
191
192
193
194
195
196
|
# File 'lib/ro/collection.rb', line 190
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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
|
# File 'lib/ro/collection.rb', line 119
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 new structure format
38
39
40
41
42
43
44
45
46
47
48
49
|
# File 'lib/ro/collection.rb', line 38
def metadata_files
extensions = %w[yml yaml json toml]
files = []
extensions.each do |ext|
@path.glob("*.#{ext}").each do |file|
files << file if file.file?
end
end
files.sort
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
97
98
99
100
101
102
103
|
# File 'lib/ro/collection.rb', line 97
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
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/ro/collection.rb', line 105
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
202
203
204
205
206
207
208
|
# File 'lib/ro/collection.rb', line 202
def paths_for(name)
[
subdirectory_for(name),
subdirectory_for(Slug.for(name, :join => '-')),
subdirectory_for(Slug.for(name, :join => '_')),
]
end
|
198
199
200
|
# File 'lib/ro/collection.rb', line 198
def size
subdirectories.size
end
|
241
242
243
|
# File 'lib/ro/collection.rb', line 241
def slice(...)
subdirectories.slice(...).map{|subdirectory| node_for(subdirectory)}
end
|
#subdirectories ⇒ Object
51
52
53
|
# File 'lib/ro/collection.rb', line 51
def subdirectories(...)
@path.subdirectories(...)
end
|
#subdirectory_for(name) ⇒ Object
55
56
57
|
# File 'lib/ro/collection.rb', line 55
def subdirectory_for(name)
@path.subdirectory_for(name)
end
|
#to_array(offset: nil, limit: nil) ⇒ Object
Also known as:
to_a, all, nodes
170
171
172
173
174
|
# File 'lib/ro/collection.rb', line 170
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
|