Class: ROCrate::Metadata

Inherits:
File show all
Defined in:
lib/ro_crate/model/metadata.rb

Overview

A representation of the ‘ro-crate-metadata.jsonld` file.

Constant Summary collapse

IDENTIFIER =
'ro-crate-metadata.json'.freeze
IDENTIFIER_1_0 =

1.0 spec identifier

'ro-crate-metadata.jsonld'.freeze
RO_CRATE_BASE =
'https://w3id.org/ro/crate/'
SUPPORTED_VERSIONS =
%w[1.0 1.0-DRAFT 1.1 1.1-DRAFT 1.2 1.2-DRAFT].freeze
DEFAULT_VERSION =
'1.2'.freeze
CONTEXT =
"#{RO_CRATE_BASE}#{DEFAULT_VERSION}/context".freeze
SPEC =
"#{RO_CRATE_BASE}#{DEFAULT_VERSION}".freeze

Instance Attribute Summary collapse

Attributes inherited from Entity

#crate, #properties

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from File

format_local_id, #payload, #remote?

Methods inherited from DataEntity

#filepath, #payload, specialize

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, properties = {}, version: DEFAULT_VERSION) ⇒ Metadata

Returns a new instance of Metadata.



26
27
28
29
30
# File 'lib/ro_crate/model/metadata.rb', line 26

def initialize(crate, properties = {}, version: DEFAULT_VERSION)
  self.class.warn_unrecognized_version(version)
  @version = version
  super(crate, nil, IDENTIFIER, properties)
end

Instance Attribute Details

#versionObject

Returns the value of attribute version.



15
16
17
# File 'lib/ro_crate/model/metadata.rb', line 15

def version
  @version
end

Class Method Details

.warn_unrecognized_version(v) ⇒ Object

Emit a warning if the given version is not in SUPPORTED_VERSIONS. Does not raise — unrecognized versions are still accepted so the library stays forward-compatible with future spec versions that need no changes.



21
22
23
24
# File 'lib/ro_crate/model/metadata.rb', line 21

def self.warn_unrecognized_version(v)
  return if SUPPORTED_VERSIONS.include?(v)
  warn "Unrecognized RO-Crate version: #{v.inspect}. Known versions: #{SUPPORTED_VERSIONS.join(', ')}"
end

Instance Method Details

#contextObject



56
57
58
# File 'lib/ro_crate/model/metadata.rb', line 56

def context
  @context || context_url
end

#context=(c) ⇒ Object



60
61
62
# File 'lib/ro_crate/model/metadata.rb', line 60

def context= c
  @context = c
end

#context_urlObject



40
41
42
# File 'lib/ro_crate/model/metadata.rb', line 40

def context_url
  "#{RO_CRATE_BASE}#{@version}/context"
end

#generateString

Generate the crate’s ‘ro-crate-metadata.jsonld`.

Returns:

  • (String)

    The rendered JSON-LD as a “prettified” string.



51
52
53
54
# File 'lib/ro_crate/model/metadata.rb', line 51

def generate
  graph = crate.entities.map(&:properties).reject(&:empty?)
  JSON.pretty_generate('@context' => context, '@graph' => graph)
end

#spec_urlObject



44
45
46
# File 'lib/ro_crate/model/metadata.rb', line 44

def spec_url
  "#{RO_CRATE_BASE}#{@version}"
end