class ROCrate::Crate

A Ruby abstraction of an RO-Crate.

Constants

IDENTIFIER

Attributes

contextual_entities[R]
data_entities[R]

Public Class Methods

format_id(id) click to toggle source
Calls superclass method
# File lib/ro_crate/model/crate.rb, line 10
def self.format_id(id)
  i = super(id)
  i.end_with?('/') ? i : "#{i}/"
end
format_local_id(id) click to toggle source
Calls superclass method
# File lib/ro_crate/model/crate.rb, line 15
def self.format_local_id(id)
  return id if id == IDENTIFIER
  super
end
new(id = IDENTIFIER, properties = {}) click to toggle source

Initialize an empty RO-Crate.

Calls superclass method
# File lib/ro_crate/model/crate.rb, line 22
def initialize(id = IDENTIFIER, properties = {})
  @data_entities = []
  @contextual_entities = []
  super(self, nil, id, properties)
end

Public Instance Methods

add_all(source_directory, create_entities = true, include_hidden: false) click to toggle source

Recursively add the contents of the given source directory at the root of the crate. Useful for quickly RO-Crate-ifying a directory. Creates data entities for each file/directory discovered (excluding the top level directory itself) if `create_entities` is true.

@param source_directory [String, Pathname, ::File,] The source directory that will be included in the crate. @param create_entities [Boolean] Whether to create data entities for the added content, or just include them anonymously. @param include_hidden [Boolean] Whether to include hidden files, i.e. those prefixed by a `.` (period).

@return [Array<DataEntity>] Any entities that were created from the directory contents. Will be empty if `create_entities` was false.

# File lib/ro_crate/model/crate.rb, line 79
def add_all(source_directory, create_entities = true, include_hidden: false)
  added = []

  if create_entities
    list_all_files(source_directory, include_hidden: include_hidden).each do |rel_path|
      source_path = Pathname.new(::File.join(source_directory, rel_path)).expand_path
      if source_path.directory?
        added << add_directory(source_path, rel_path)
      else
        added << add_file(source_path, rel_path)
      end
    end
  else
    populate_entries(Pathname.new(::File.expand_path(source_directory)), include_hidden: include_hidden)
  end

  added
end
add_contact_point(id, properties = {}) click to toggle source

Create a new ROCrate::ContactPoint and add it to the crate

@param id [String, nil] An ID to identify this contact point, or blank to auto-generate an appropriate one,

(or determine via the properties param)

@param properties [Hash{String => Object}] A hash of JSON-LD properties to associate with this contact point. @return [ContactPoint]

# File lib/ro_crate/model/crate.rb, line 116
def add_contact_point(id, properties = {})
  add_contextual_entity(ROCrate::ContactPoint.new(self, id, properties))
end
add_contextual_entity(entity) click to toggle source

Add a contextual entity to the crate

@param entity [Entity] the entity to add to the crate. @return [Entity] the entity itself, or a clone of the entity “owned” by this crate.

# File lib/ro_crate/model/crate.rb, line 136
def add_contextual_entity(entity)
  entity = claim(entity)
  contextual_entities.delete(entity) # Remove (then re-add) the entity if it exists
  contextual_entities.push(entity)
  entity
end
add_data_entity(entity) click to toggle source

Add a data entity to the crate

@param entity [Entity] the entity to add to the crate. @return [Entity] the entity itself, or a clone of the entity “owned” by this crate.

# File lib/ro_crate/model/crate.rb, line 148
def add_data_entity(entity)
  entity = claim(entity)
  data_entities.delete(entity) # Remove (then re-add) the entity if it exists
  data_entities.push(entity)
  entity
end
add_directory(source_directory, crate_path = nil, entity_class: ROCrate::Directory, **properties) click to toggle source

Create a new directory and add it to the crate.

@param source_directory [String, Pathname, ::File, read, nil] The source directory that will be included in the crate. @param crate_path [String] The relative path within the RO-Crate where this directory will be written. @param entity_class [Class] The class to use to instantiate the Entity,

useful if you have created a subclass of ROCrate::Directory that you want to use. (defaults to ROCrate::Directory).

@param properties [Hash{String => Object}] A hash of JSON-LD properties to associate with this directory.

@return [Entity]

# File lib/ro_crate/model/crate.rb, line 65
def add_directory(source_directory, crate_path = nil, entity_class: ROCrate::Directory, **properties)
  entity_class.new(self, source_directory, crate_path, properties).tap { |e| add_data_entity(e) }
end
add_external_file(source, entity_class: ROCrate::File, **properties) click to toggle source

Create a new file that references a remote URI and add it to the crate.

@param source [String, URI] The URI to add. @param entity_class [Class] The class to use to instantiate the Entity,

useful if you have created a subclass of ROCrate::File that you want to use. (defaults to ROCrate::File).

@param properties [Hash{String => Object}] A hash of JSON-LD properties to associate with this file.

@return [Entity]

# File lib/ro_crate/model/crate.rb, line 51
def add_external_file(source, entity_class: ROCrate::File, **properties)
  entity_class.new(self, source, nil, properties).tap { |e| add_data_entity(e) }
end
add_file(source, crate_path = nil, entity_class: ROCrate::File, **properties) click to toggle source

Create a new file and add it to the crate.

@param source [String, Pathname, ::File, read, nil] The source on the disk where this file will be read. @param crate_path [String] The relative path within the RO-Crate where this file will be written. @param entity_class [Class] The class to use to instantiate the Entity,

useful if you have created a subclass of ROCrate::File that you want to use. (defaults to ROCrate::File).

@param properties [Hash{String => Object}] A hash of JSON-LD properties to associate with this file.

@return [Entity]

# File lib/ro_crate/model/crate.rb, line 38
def add_file(source, crate_path = nil, entity_class: ROCrate::File, **properties)
  entity_class.new(self, source, crate_path, properties).tap { |e| add_data_entity(e) }
end
add_organization(id, properties = {}) click to toggle source

Create a new ROCrate::Organization and add it to the crate

@param id [String, nil] An ID to identify this organization, or blank to auto-generate an appropriate one,

(or determine via the properties param)

@param properties [Hash{String => Object}] A hash of JSON-LD properties to associate with this organization. @return [Organization]

# File lib/ro_crate/model/crate.rb, line 127
def add_organization(id, properties = {})
  add_contextual_entity(ROCrate::Organization.new(self, id, properties))
end
add_person(id, properties = {}) click to toggle source

Create a new ROCrate::Person and add it to the crate

@param id [String, nil] An ID to identify this person, or blank to auto-generate an appropriate one,

(or determine via the properties param)

@param properties [Hash{String => Object}] A hash of JSON-LD properties to associate with this person. @return [Person]

# File lib/ro_crate/model/crate.rb, line 105
def add_person(id, properties = {})
  add_contextual_entity(ROCrate::Person.new(self, id, properties))
end
canonical_id() click to toggle source

The “canonical”, global ID of the crate. If the crate was not given an absolute URI as its ID, it will use an “Archive and Package” (ARCP) URI with the UUID of the crate, for example:

arcp://uuid,b3d6fa2b-4e49-43ba-bd89-464e948b7f0c/

@return [Addressable::URI]

# File lib/ro_crate/model/crate.rb, line 210
def canonical_id
  Addressable::URI.parse("arcp://uuid,#{uuid}").join(id)
end
claim(entity) click to toggle source

Copy the entity, but as if it was in this crate. (Or just return the entity if it was already included)

# File lib/ro_crate/model/crate.rb, line 227
def claim(entity)
  return entity if entity.crate == self
  entity.class.new(crate, entity.id, entity.raw_properties)
end
default_entities() click to toggle source

Entities for the metadata file and crate itself, which should be present in all RO-Crates.

@return [Array<Entity>]

# File lib/ro_crate/model/crate.rb, line 192
def default_entities
  [metadata, preview, self]
end
entities() click to toggle source

All the entities within the crate. Includes contextual entities, data entities, the crate itself and its metadata file.

@return [Array<Entity>]

# File lib/ro_crate/model/crate.rb, line 184
def entities
  default_entities | data_entities | contextual_entities
end
entries() click to toggle source

# The RO-Crate's “payload” of the crate - a map of all the files/directories contained in the RO-Crate, where the key is the destination path within the crate and the value is an Entry where the source data can be read.

@return [Hash{String => Entry}>]

# File lib/ro_crate/model/crate.rb, line 238
def entries
  # Gather a map of entries, starting from the crate itself, then any directory data entities, then finally any
  # file data entities. This ensures in the case of a conflict, the more "specific" data entities take priority.
  entries = own_entries
  non_self_entities = default_entities.reject { |e| e == self }
  sorted_entities = (non_self_entities | data_entities).sort_by { |e| e.is_a?(ROCrate::Directory) ? 0 : 1 }

  sorted_entities.each do |entity|
    entity.entries.each do |path, entry|
      entries[path] = entry
    end
  end

  entries
end
Also aliased as: own_entries
get_binding() click to toggle source
# File lib/ro_crate/model/crate.rb, line 254
def get_binding
  binding
end
metadata() click to toggle source

The RO-Crate metadata file

@return [Metadata]

# File lib/ro_crate/model/crate.rb, line 159
def metadata
  @metadata ||= ROCrate::Metadata.new(self)
end
own_entries()
Alias for: entries
preview() click to toggle source

The RO-Crate preview file

@return [Preview]

# File lib/ro_crate/model/crate.rb, line 167
def preview
  @preview ||= ROCrate::Preview.new(self)
end
preview=(preview) click to toggle source

Set the RO-Crate preview file @param preview [Preview] the preview to set.

@return [Preview]

# File lib/ro_crate/model/crate.rb, line 176
def preview=(preview)
  @preview = claim(preview)
end
properties() click to toggle source
Calls superclass method
# File lib/ro_crate/model/crate.rb, line 196
def properties
  super.merge('hasPart' => data_entities.map(&:reference))
end
resolve_id(id) click to toggle source

Return an absolute URI for the given string ID, relative to the crate's canonical ID.

@param id [String] The ID to “join” onto the crate's base URI.

@return [Addressable::URI]

# File lib/ro_crate/model/crate.rb, line 220
def resolve_id(id)
  canonical_id.join(id)
end
uuid() click to toggle source
# File lib/ro_crate/model/crate.rb, line 200
def uuid
  @uuid ||= SecureRandom.uuid
end

Private Instance Methods

full_entry_path(relative_path) click to toggle source
# File lib/ro_crate/model/crate.rb, line 260
def full_entry_path(relative_path)
  relative_path
end