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 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, blank_node = false) ⇒ Array
Location, value.
- #add_predicate_location_and_value_direct_for_blank_node(location, value, blank_node) ⇒ Object
- #add_predicate_location_and_value_direct_for_non_blank_node(location, value) ⇒ Object
- #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.
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
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.
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.
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
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.
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_rof ⇒ 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 (rof) rof = append_properties_to(rof) rof = force_cardinality_for_backwards_compatability(rof) rof end |