Class: ROF::Translators::JsonldToRof::Accumulator

Inherits:
Object
  • Object
show all
Defined in:
lib/rof/translators/jsonld_to_rof/accumulator.rb

Overview

Note:

The #to_rof will convert blank nodes to arrays of strings for objects that don’t have blank node dc:contributor.

Note:

The accumulator is only for one PID. See [ROF::Translators::JsonldToRof::Accumulator#add_pid]

The accumulator is a “passive” object. Things happen to it. All in the name of building the hash that is ROF.

Defined Under Namespace

Classes: PidAlreadySetError, TooManyElementsError

Instance Method Summary collapse

Constructor Details

#initialize(initial_rof = {}) ⇒ Accumulator

Returns a new instance of Accumulator.

Parameters:

  • initial_rof (Hash) (defaults to: {})
    • The base ROF document to which we will be adding elements.



16
17
18
19
20
# File 'lib/rof/translators/jsonld_to_rof/accumulator.rb', line 16

def initialize(initial_rof = {})
  @rof = initial_rof
  @blank_nodes = {}
  @blank_node_locations = {}
end

Instance Method Details

#add_blank_node(statement) ⇒ RDF::Statement

Parameters:

  • statement (RDF::Statement)

Returns:

  • (RDF::Statement)


125
126
127
128
129
130
# File 'lib/rof/translators/jsonld_to_rof/accumulator.rb', line 125

def add_blank_node(statement)
  @blank_nodes[statement.subject] ||= {}
  @blank_nodes[statement.subject][statement.predicate] ||= []
  @blank_nodes[statement.subject][statement.predicate] << statement.object
  statement
end

#add_pid(pid) ⇒ String

Returns pid.

Parameters:

  • pid (String)
    • an identifier

Returns:

  • (String)

    pid

Raises:

  • PidAlreadySetError - if you attempted to a different PID



145
146
147
148
149
150
151
152
153
154
155
# File 'lib/rof/translators/jsonld_to_rof/accumulator.rb', line 145

def add_pid(pid)
  pid = coerce_object_to_string(pid)
  if @rof.key?('pid')
    if @rof['pid'] != pid
      raise PidAlreadySetError, "Attempted to set pid=#{pid}, but it is already set to #{@rof['pid']}"
    end
  else
    @rof['pid'] = pid
  end
  pid
end

#add_predicate_location_and_value(location, value, options = {}) ⇒ Array

Returns location, value.

Parameters:

  • location (Array<String>, String)
    • a list of nested hash keys (or a single string)

  • value (String)
    • a translated value for the original RDF Statement

  • options (Hash) (defaults to: {})

Options Hash (options):

  • blank_node (false, RDF::Node)
  • multiple (Boolean) — default: default true
    • if true will append values to an Array; if false will have a singular (non-Array) value

Returns:

  • (Array)

    location, value



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/rof/translators/jsonld_to_rof/accumulator.rb', line 164

def add_predicate_location_and_value(location, value, options = {})
  blank_node = options.fetch(:blank_node, false)
  multiple = options.fetch(:multiple, true)
  # Because I am making transformation on the location via #shift method, I need a duplication.
  location = Array.wrap(location)
  if location == ['pid']
    return add_pid(value)
  end
  if blank_node
    add_predicate_location_and_value_direct_for_blank_node(location, value, blank_node, multiple)
  else
    add_predicate_location_and_value_direct_for_non_blank_node(location, value, multiple)
  end
  [location, value]
end

#fetch_blank_node(subject) ⇒ RDF::Statement

Parameters:

  • subject (RDF::Subject)
    • Fetch the corresponding blank node that was added

Returns:

  • (RDF::Statement)

Raises:

  • (KeyError)

    when the subject has not previosly been added

See Also:



137
138
139
# File 'lib/rof/translators/jsonld_to_rof/accumulator.rb', line 137

def fetch_blank_node(subject)
  @blank_nodes.fetch(subject)
end

#register_properties(node_name, node_value) ⇒ Array

Returns of given node_name and node_value.

Parameters:

  • node_name (String)
    • the XML node’s name (e.g. <node_name>node_value</node_name>)

  • node_value (String)
    • the XML element’s value

Returns:

  • (Array)

    of given node_name and node_value



113
114
115
116
117
# File 'lib/rof/translators/jsonld_to_rof/accumulator.rb', line 113

def register_properties(node_name, node_value)
  @properties ||= []
  @properties << [node_name, coerce_object_to_string(node_value)]
  [node_name, node_value]
end

#to_rofHash

Returns:

  • (Hash)


24
25
26
27
28
29
30
# File 'lib/rof/translators/jsonld_to_rof/accumulator.rb', line 24

def to_rof
  rof = @rof.deep_dup
  rof = expand_blank_node_locations!(rof)
  rof = normalize_contributor!(rof)
  rof = append_properties_to(rof)
  rof
end