Class: ROF::Translators::JsonldToRof::Accumulator
- Inherits:
-
Object
- Object
- ROF::Translators::JsonldToRof::Accumulator
- 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
- #add_blank_node(statement) ⇒ RDF::Statement
-
#add_pid(pid) ⇒ String
Pid.
-
#add_predicate_location_and_value(location, value, options = {}) ⇒ Array
Location, value.
- #fetch_blank_node(subject) ⇒ RDF::Statement
-
#initialize(initial_rof = {}) ⇒ Accumulator
constructor
A new instance of Accumulator.
-
#register_properties(node_name, node_value) ⇒ Array
Of given node_name and node_value.
- #to_rof ⇒ Hash
Constructor Details
#initialize(initial_rof = {}) ⇒ Accumulator
Returns a new instance of Accumulator.
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
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.
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.
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, = {}) blank_node = .fetch(:blank_node, false) multiple = .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
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.
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_rof ⇒ 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 = (rof) rof = normalize_contributor!(rof) rof = append_properties_to(rof) rof end |