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

#initialize(path) ⇒ Collection

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

#pathObject (readonly)

Returns the value of attribute path.



6
7
8
# File 'lib/ro/collection.rb', line 6

def path
  @path
end

#rootObject (readonly)

Returns the value of attribute root.



6
7
8
# File 'lib/ro/collection.rb', line 6

def root
  @root
end

Instance Method Details

#[](name) ⇒ Object



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 enumerator if no block given and no offset/limit
  return to_enum(:each, offset: offset, limit: limit) unless block_given?

  # Get metadata from both old and new structure
   = 

  if offset
    i = -1
    n = 0
    .each do |entry|
      i += 1
      next if i < offset

      # Create node based on structure type
      node = if entry[:type] == :new
        Node.new(self, entry[:path])
      else
        # Old structure: pass subdirectory path
        Node.new(entry[:path].parent)
      end

      block.call(node)
      n += 1
      break if limit && n >= limit
    end
  else
    .each do |entry|
      # Create node based on structure type
      node = if entry[:type] == :new
        Node.new(self, entry[:path])
      else
        # Old structure: pass subdirectory path
        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)
  # Try to find metadata file for this node ID
  extensions = %w[yml yaml json toml]
  extensions.each do |ext|
     = @path.join("#{name}.#{ext}")
    if .exist? && .file?
      return Node.new(self, )
    end
  end

  # Also try with slugified versions
  [
    Slug.for(name, :join => '-'),
    Slug.for(name, :join => '_')
  ].each do |slug|
    extensions.each do |ext|
       = @path.join("#{slug}.#{ext}")
      if .exist? && .file?
        return Node.new(self, )
      end
    end
  end

  nil
end

#idObject



17
18
19
# File 'lib/ro/collection.rb', line 17

def id
  name
end

#identifierObject



25
26
27
# File 'lib/ro/collection.rb', line 25

def identifier
  type
end

#inspectObject



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 # FIXME
  o = Queue.new # FIXME

  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) # FIXME
            nil # FIXME
          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

#metadata_filesObject

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 
  extensions = %w[yml yaml json toml]
   = {}

  # First, scan for new structure: collection-level metadata files (e.g., posts/ara.yml)
  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)$/, '')
      [node_id] ||= {id: node_id, path: file, type: :new}
    end
  end

  # Second, scan for old structure: subdirectory attributes files (e.g., posts/ara/attributes.yml)
  subdirectories.each do |subdir|
    node_id = subdir.basename.to_s

    # Check if this subdirectory has an attributes file
    extensions.each do |ext|
      attributes_file = subdir.join("attributes.#{ext}")
      if attributes_file.exist? && attributes_file.file?
        # Only use old structure if new structure doesn't exist
        # This allows gradual migration - new structure takes precedence
        [node_id] ||= {id: node_id, path: attributes_file, type: :old}
        break
      end
    end
  end

  # Return sorted array of metadata info
  .values.sort_by { |m| m[:id] }
end

#nameObject



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

#sizeObject



234
235
236
# File 'lib/ro/collection.rb', line 234

def size
  subdirectories.size
end

#sliceObject



277
278
279
# File 'lib/ro/collection.rb', line 277

def slice(...)
  subdirectories.slice(...).map{|subdirectory| node_for(subdirectory)}
end

#subdirectoriesObject



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

#typeObject



21
22
23
# File 'lib/ro/collection.rb', line 21

def type
  name
end