Class: ROCrate::Directory

Inherits:
DataEntity show all
Defined in:
lib/ro_crate/model/directory.rb

Overview

A data entity that represents a directory of potentially many files and subdirectories (or none).

Direct Known Subclasses

Crate

Instance Attribute Summary

Attributes inherited from Entity

#crate, #properties

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from DataEntity

#filepath, specialize

Methods inherited from Entity

#==, #[], #[]=, #auto_dereference, #auto_reference, #canonical_id, #dereference, #eql?, #external?, #has_type?, #hash, #id, #id=, #inspect, properties, #raw_properties, #reference, #to_json, #type, #type=

Constructor Details

#initialize(crate, source_directory = nil, crate_path = nil, properties = {}) ⇒ Directory

Create a new Directory. PLEASE NOTE, the new directory will not be added to the crate. To do this, call Crate#add_data_entity, or just use Crate#add_directory.

Parameters:

  • crate (Crate)

    The RO crate that owns this directory.

  • source_directory (String, Pathname, ::File, nil) (defaults to: nil)

    The source directory that will be included in the crate.

  • crate_path (String) (defaults to: nil)

    The relative path within the RO crate where this directory will be written.

  • properties (Hash{String => Object}) (defaults to: {})

    A hash of JSON-LD properties to associate with this directory.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ro_crate/model/directory.rb', line 19

def initialize(crate, source_directory = nil, crate_path = nil, properties = {})
  @directory_entries = {}

  if source_directory
    raise 'Not a directory' unless ::File.directory?(source_directory)
    source_directory = Pathname.new(::File.expand_path(source_directory))
    crate_path = source_directory.basename.to_s if crate_path.nil?

    Dir.chdir(source_directory) { Dir.glob('**/*') }.each do |rel_path|
      source_path = Pathname.new(::File.join(source_directory, rel_path)).expand_path
      @directory_entries[rel_path] = Entry.new(source_path)
    end
  end

  super(crate, crate_path, properties)
end

Class Method Details

.format_id(id) ⇒ Object



7
8
9
# File 'lib/ro_crate/model/directory.rb', line 7

def self.format_id(id)
  super + '/'
end

Instance Method Details

#entriesHash{String => Entry}

A map of all the files/directories under this directory, where the key is the destination path within the crate and the value is an Entry where the source data can be read.

Returns:

  • (Hash{String => Entry})

    ]



41
42
43
44
45
46
47
48
49
# File 'lib/ro_crate/model/directory.rb', line 41

def entries
  entries = {}

  @directory_entries.each do |rel_path, entry|
    entries[::File.join(filepath, rel_path)] = entry
  end

  entries
end