Class: ROCrate::DataEntity

Inherits:
Entity
  • Object
show all
Defined in:
lib/ro_crate/model/data_entity.rb

Overview

A class to represent a “Data Entity” within an RO-Crate. Data Entities are the actual physical files and directories within the Crate.

Direct Known Subclasses

Directory, File

Instance Attribute Summary

Attributes inherited from Entity

#crate, #properties

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Entity

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

Constructor Details

#initialize(crate, source = nil, crate_path = nil, properties = {}) ⇒ DataEntity

Create a new ROCrate::DataEntity. This entity represents something that is neither a file or directory, but still constitutes part of the crate. PLEASE NOTE, the new data entity will not be added to the crate. To do this, call Crate#add_data_entity.

where the content of this DataEntity can be found. Also used as the ID of the DataEntity. Will be taken from ‘properties` or generated if `crate_path` is nil.

Parameters:

  • crate (Crate)

    The RO-Crate that owns this directory.

  • source (String, Pathname, ::File, URI, nil, #read) (defaults to: nil)

    The source on the disk (or on the internet if a URI)

  • crate_path (String, nil) (defaults to: nil)

    The relative path within the RO-Crate where this data entity should be written.

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

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



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ro_crate/model/data_entity.rb', line 35

def initialize(crate, source = nil, crate_path = nil, properties = {})
  if crate_path.is_a?(Hash) && properties.empty?
    properties = crate_path
    crate_path = nil
  end

  @source = normalize_source(source)

  if crate_path.nil?
    crate_path = @source.basename.to_s if @source.respond_to?(:basename)
    crate_path = @source.to_s if @source.is_a?(URI) && @source.absolute?
  end

  super(crate, crate_path, properties)
end

Class Method Details

.specialize(props) ⇒ Class

Return an appropriate specialization of DataEntity for the given properties.

Parameters:

  • props (Hash)

    Set of properties to try and infer the type from.

Returns:

  • (Class)


12
13
14
15
16
17
18
19
20
21
22
# File 'lib/ro_crate/model/data_entity.rb', line 12

def self.specialize(props)
  type = props['@type']
  type = [type] unless type.is_a?(Array)
  if type.include?('Dataset')
    ROCrate::Directory
  elsif type.include?('File')
    ROCrate::File
  else
    self
  end
end

Instance Method Details

#filepathString

A disk-safe filepath based on the ID of this DataEntity.

Returns:

  • (String)

    The relative file path of this DataEntity within the Crate.



64
65
66
# File 'lib/ro_crate/model/data_entity.rb', line 64

def filepath
  Addressable::URI.unescape(id.sub(/\A\//, '')).to_s # Remove initial / and decode %20 etc.
end

#payloadHash{String => Entry} Also known as: entries

The payload of all the files/directories associated with this DataEntity, mapped by their relative file path.

Returns:

  • (Hash{String => Entry})

    ] The key is the location within the crate, and the value is an Entry.



55
56
57
# File 'lib/ro_crate/model/data_entity.rb', line 55

def payload
  {}
end