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

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

Overview

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.



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

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)


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

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



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

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, blank_node = false) ⇒ 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

  • blank_node (false, RDF::Node) (defaults to: false)

Returns:

  • (Array)

    location, value



160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/rof/translators/jsonld_to_rof/accumulator.rb', line 160

def add_predicate_location_and_value(location, value, blank_node = false)
  # 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)
  else
    add_predicate_location_and_value_direct_for_non_blank_node(location, value)
  end
  [location, value]
end

#add_predicate_location_and_value_direct_for_blank_node(location, value, blank_node) ⇒ Object



174
175
176
177
178
179
# File 'lib/rof/translators/jsonld_to_rof/accumulator.rb', line 174

def add_predicate_location_and_value_direct_for_blank_node(location, value, blank_node)
  fetch_blank_node(blank_node) # Ensure the node exists
  @blank_node_locations[blank_node] ||= {}
  @blank_node_locations[blank_node][location[0..-2]] ||= []
  @blank_node_locations[blank_node][location[0..-2]] << { location[-1] => Array.wrap(coerce_object_to_string(value)) }
end

#add_predicate_location_and_value_direct_for_non_blank_node(location, value) ⇒ Object



181
182
183
184
185
186
187
188
189
190
# File 'lib/rof/translators/jsonld_to_rof/accumulator.rb', line 181

def add_predicate_location_and_value_direct_for_non_blank_node(location, value)
  data = @rof
  location[0..-2].each do |slug|
    data[slug] ||= {}
    data = data[slug]
  end
  slug = location[-1]
  data[slug] ||= []
  data[slug] << coerce_object_to_string(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:



135
136
137
# File 'lib/rof/translators/jsonld_to_rof/accumulator.rb', line 135

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



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

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)


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

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