Class: BibframeRuby::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/bibframe_ruby/resource.rb

Overview

Base class for all BIBFRAME resources. Provides URI identity, type tracking, and hash-style property access.

Direct Known Subclasses

Agent, Contribution, Hub, Instance, Item, Subject, Title, Work

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, types: [], properties: {}) ⇒ Resource

Returns a new instance of Resource.



12
13
14
15
16
17
# File 'lib/bibframe_ruby/resource.rb', line 12

def initialize(id: nil, types: [], properties: {})
  @id = id
  @types = types
  @properties = properties
  @rdf_graph = nil
end

Instance Attribute Details

#id ⇒ Object (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/bibframe_ruby/resource.rb', line 9

def id
  @id
end

#properties ⇒ Object

Returns the value of attribute properties.



10
11
12
# File 'lib/bibframe_ruby/resource.rb', line 10

def properties
  @properties
end

#rdf_graph ⇒ Object

Returns the value of attribute rdf_graph.



10
11
12
# File 'lib/bibframe_ruby/resource.rb', line 10

def rdf_graph
  @rdf_graph
end

#types ⇒ Object (readonly)

Returns the value of attribute types.



9
10
11
# File 'lib/bibframe_ruby/resource.rb', line 9

def types
  @types
end

Instance Method Details

#[](key) ⇒ Object



19
20
21
# File 'lib/bibframe_ruby/resource.rb', line 19

def [](key)
  @properties[key.to_s]
end

#[]=(key, value) ⇒ Object



23
24
25
# File 'lib/bibframe_ruby/resource.rb', line 23

def []=(key, value)
  @properties[key.to_s] = value
end

#to_rdf(format: :jsonld) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/bibframe_ruby/resource.rb', line 27

def to_rdf(format: :jsonld)
  raise BibframeRuby::Error, "Unsupported serialization format: #{format}" unless Graph::WRITER_MAP.key?(format)
  raise BibframeRuby::Error, "No RDF graph available for serialization" unless @rdf_graph && @id

  sub_graph = extract_sub_graph
  Graph::WRITER_MAP[format].buffer(prefixes: Graph::PREFIXES.dup) { |w| sub_graph.each_statement { |s| w << s } }
end