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



245
246
247
# File 'lib/ro/collection.rb', line 245

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



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

  # Use metadata files for new structure instead of subdirectories
  files = 

  if offset
    i = -1
    n = 0
    files.each do ||
      i += 1
      next if i < offset
      node = Node.new(self, )
      block.call(node)
      n += 1
      break if limit && n >= limit
    end
  else
    files.each do ||
      node = Node.new(self, )
      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)
  # 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



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 # 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 new structure format



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ro/collection.rb', line 38

def 
  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

#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



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

#sizeObject



198
199
200
# File 'lib/ro/collection.rb', line 198

def size
  subdirectories.size
end

#sliceObject



241
242
243
# File 'lib/ro/collection.rb', line 241

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

#subdirectoriesObject



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

#typeObject



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

def type
  name
end