Class: BibframeRuby::Graph

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

Overview

Container returned by BibframeRuby.parse that holds all parsed resources with typed collection accessors.

Constant Summary collapse

WRITER_MAP =
{
  jsonld: JSON::LD::Writer,
  turtle: RDF::Turtle::Writer
}.freeze
PREFIXES =
{
  bf: "http://id.loc.gov/ontologies/bibframe/",
  bflc: "http://id.loc.gov/ontologies/bflc/",
  rdfs: "http://www.w3.org/2000/01/rdf-schema#",
  rdf: "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
  xsd: "http://www.w3.org/2001/XMLSchema#"
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(works:, instances:, items:, resources:, hubs: [], rdf_graph: nil) ⇒ Graph

Returns a new instance of Graph.



25
26
27
28
29
30
31
32
# File 'lib/bibframe_ruby/graph.rb', line 25

def initialize(works:, instances:, items:, resources:, hubs: [], rdf_graph: nil)
  @works = works
  @instances = instances
  @items = items
  @hubs = hubs
  @resources = resources
  @rdf_graph = rdf_graph
end

Instance Attribute Details

#hubs ⇒ Object (readonly)

Returns the value of attribute hubs.



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

def hubs
  @hubs
end

#instances ⇒ Object (readonly)

Returns the value of attribute instances.



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

def instances
  @instances
end

#items ⇒ Object (readonly)

Returns the value of attribute items.



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

def items
  @items
end

#rdf_graph ⇒ Object (readonly)

Returns the value of attribute rdf_graph.



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

def rdf_graph
  @rdf_graph
end

#resources ⇒ Object (readonly)

Returns the value of attribute resources.



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

def resources
  @resources
end

#works ⇒ Object (readonly)

Returns the value of attribute works.



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

def works
  @works
end

Class Method Details

.from_rdf(rdf_graph) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/bibframe_ruby/graph.rb', line 46

def self.from_rdf(rdf_graph)
  result = GraphBuilder.new(rdf_graph).build

  graph = new(
    works: result[:works],
    instances: result[:instances],
    items: result[:items],
    hubs: result[:hubs],
    resources: result[:resources].values,
    rdf_graph: rdf_graph
  )

  # Set back-references so resources can serialize themselves
  result[:resources].each_value { |r| r.rdf_graph = rdf_graph }

  graph
end

Instance Method Details

#==(other) ⇒ Object



34
35
36
# File 'lib/bibframe_ruby/graph.rb', line 34

def ==(other)
  other.is_a?(Graph) && @rdf_graph.isomorphic_with?(other.rdf_graph)
end

#to_rdf(format: :jsonld) ⇒ Object



38
39
40
41
42
43
44
# File 'lib/bibframe_ruby/graph.rb', line 38

def to_rdf(format: :jsonld)
  writer_class = WRITER_MAP.fetch(format) do
    raise BibframeRuby::Error, "Unsupported serialization format: #{format}"
  end

  writer_class.buffer(prefixes: PREFIXES.dup) { |w| @rdf_graph.each_statement { |s| w << s } }
end