Class: Slaw::DocumentCollection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable
Defined in:
lib/slaw/collection.rb

Overview

A collection of Act instances.

This is useful for looking up acts by their FRBR uri and for loading a collection of XML act documents.

This collection is enumerable and can be iterated over. Use #items to access the underlying array of objects.

Examples:

Load a collection of acts and then iterate over them.


acts = Slaw::DocumentCollection.new
acts.discover('/path/to/acts/')

for act in acts
  puts act.short_name
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items = nil) ⇒ DocumentCollection

Returns a new instance of DocumentCollection.



31
32
33
# File 'lib/slaw/collection.rb', line 31

def initialize(items=nil)
  @items = items || []
end

Instance Attribute Details

#itemsObject

Array<Act>

The underlying array of acts



27
28
29
# File 'lib/slaw/collection.rb', line 27

def items
  @items
end

Instance Method Details

#discover(path, cls = Slaw::Act) ⇒ DocumentCollection

Find all XML files in ‘path` and add them into this collection.

Parameters:

  • path (String)

    the path to glob for xml files

  • cls (Class) (defaults to: Slaw::Act)

    the class to instantiate for each file

Returns:



42
43
44
45
46
47
48
# File 'lib/slaw/collection.rb', line 42

def discover(path, cls=Slaw::Act)
  for fname in Dir.glob("#{path}/**/*.xml")
    @items << cls.new(fname)
  end

  self
end

#for_uri(uri) ⇒ Act?

Try to find an act who’s FRBRuri matches this one, returning nil on failure

Parameters:

  • uri (String)

    the uri to look for

Returns:

  • (Act, nil)

    the act, or nil



56
57
58
# File 'lib/slaw/collection.rb', line 56

def for_uri(uri)
  return @items.find { |doc| doc.id_uri == uri }
end