Class: Solis::Model
- Inherits:
-
Object
- Object
- Solis::Model
- Defined in:
- lib/solis/model.rb
Class Method Summary collapse
- .construct(level = 0) ⇒ Object
- .graph ⇒ Object
- .graph=(graph) ⇒ Object
- .graph_name ⇒ Object
- .graph_name=(graph_name) ⇒ Object
- .graph_prefix ⇒ Object
- .graph_prefix=(graph_prefix) ⇒ Object
- .language ⇒ Object
- .language=(language) ⇒ Object
- .make_id_for(model) ⇒ Object
- .metadata ⇒ Object
- .metadata=(m) ⇒ Object
- .model(level = 0) ⇒ Object
- .model_after_create(&blk) ⇒ Object
- .model_after_delete(&blk) ⇒ Object
- .model_after_read(&blk) ⇒ Object
- .model_after_update(&blk) ⇒ Object
- .model_before_create(&blk) ⇒ Object
- .model_before_delete(&blk) ⇒ Object
- .model_before_read(&blk) ⇒ Object
- .model_before_update(&blk) ⇒ Object
- .model_template(level = 0) ⇒ Object
- .shapes ⇒ Object
- .shapes=(s) ⇒ Object
- .sparql_endpoint ⇒ Object
- .sparql_endpoint=(sparql_endpoint) ⇒ Object
Instance Method Summary collapse
- #destroy ⇒ Object
- #dump(format = :ttl, resolve_all = true) ⇒ Object
- #exists?(sparql) ⇒ Boolean
- #graph_id ⇒ Object
-
#initialize(attributes = {}) ⇒ Model
constructor
A new instance of Model.
- #is_referenced?(sparql) ⇒ Boolean
-
#model_class_name(plural = false) ⇒ Object
Removed the ‘name’ instance method to avoid conflicts with ‘name’ attributes Use model_class_name instead, or access @model_name directly.
- #query ⇒ Object
- #save(validate_dependencies = true, top_level = true) ⇒ Object
- #to_graph(resolve_all = true) ⇒ Object
- #to_ttl(resolve_all = true) ⇒ Object
- #update(data, validate_dependencies = true, top_level = true) ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(attributes = {}) ⇒ Model
Returns a new instance of Model.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/solis/model.rb', line 11 def initialize(attributes = {}) @model_name = self.class.name @model_plural_name = @model_name.pluralize @language = Graphiti.context[:object]&.language || Solis::Options.instance.get[:language] || 'en' raise "Please look at /#{@model_name.tableize}/model for structure to supply" if attributes.nil? attributes.each do |attribute, value| if self.class.[:attributes].keys.include?(attribute.to_s) if !self.class.[:attributes][attribute.to_s][:node_kind].nil? && !(value.is_a?(Hash) || value.is_a?(Array) || value.class.ancestors.include?(Solis::Model)) raise Solis::Error::InvalidAttributeError, "'#{@model_name}.#{attribute}' must be an object" end if self.class.[:attributes][attribute.to_s][:node_kind].is_a?(RDF::URI) && value.is_a?(Hash) inner_model = self.class.graph.shape_as_model(self.class.[:attributes][attribute.to_s][:datatype].to_s) value = inner_model.new(value) elsif self.class.[:attributes][attribute.to_s][:node_kind].is_a?(RDF::URI) && value.is_a?(Array) new_value = [] value.each do |v| if v.is_a?(Hash) inner_model = self.class.graph.shape_as_model(self.class.[:attributes][attribute.to_s][:datatype].to_s) new_value << inner_model.new(v) else new_value << v end end value = new_value end # switched off. currently language query parameters returns the value # value = { # "@language" => @language, # "@value" => value # } if self.class.metadata[:attributes][attribute.to_s][:datatype_rdf].eql?('http://www.w3.org/1999/02/22-rdf-syntax-ns#langString') value = value.first if value.is_a?(Array) && (attribute.eql?('id') || attribute.eql?(:id)) instance_variable_set("@#{attribute}", value) else raise Solis::Error::InvalidAttributeError, "'#{attribute}' is not part of the definition of #{@model_name}" end end self.class.make_id_for(self) rescue StandardError => e Solis::LOGGER.error(e.) raise Solis::Error::GeneralError, "Unable to create entity #{@model_name}" end |
Class Method Details
.construct(level = 0) ⇒ Object
427 428 429 |
# File 'lib/solis/model.rb', line 427 def self.construct(level = 0) raise 'to be implemented' end |
.graph ⇒ Object
377 378 379 |
# File 'lib/solis/model.rb', line 377 def self.graph @graph end |
.graph=(graph) ⇒ Object
381 382 383 |
# File 'lib/solis/model.rb', line 381 def self.graph=(graph) @graph = graph end |
.graph_name ⇒ Object
353 354 355 |
# File 'lib/solis/model.rb', line 353 def self.graph_name @graph_name end |
.graph_name=(graph_name) ⇒ Object
357 358 359 |
# File 'lib/solis/model.rb', line 357 def self.graph_name=(graph_name) @graph_name = graph_name end |
.graph_prefix ⇒ Object
365 366 367 |
# File 'lib/solis/model.rb', line 365 def self.graph_prefix @graph_prefix end |
.graph_prefix=(graph_prefix) ⇒ Object
361 362 363 |
# File 'lib/solis/model.rb', line 361 def self.graph_prefix=(graph_prefix) @graph_prefix = graph_prefix end |
.language ⇒ Object
385 386 387 |
# File 'lib/solis/model.rb', line 385 def self.language Graphiti.context[:object]&.language || Solis::Options.instance.get[:language] || @language || 'en' end |
.language=(language) ⇒ Object
389 390 391 |
# File 'lib/solis/model.rb', line 389 def self.language=(language) @language = language end |
.make_id_for(model) ⇒ Object
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 |
# File 'lib/solis/model.rb', line 313 def self.make_id_for(model) raise "I need a SPARQL endpoint" if self.sparql_endpoint.nil? sparql = Solis::Store::Sparql::Client.new(self.sparql_endpoint) id = model.instance_variable_get("@id") if id.nil? || (id.is_a?(String) && id&.empty?) id_retries = 0 while id.nil? || sparql.query("ASK WHERE { ?s <#{self.graph_name}id> \"#{id}\" }") id = SecureRandom.uuid id_retries += 1 end LOGGER.info("ID(#{id}) generated for #{self.name} in #{id_retries} retries") if ConfigFile[:debug] model.instance_variable_set("@id", id) elsif id.to_s =~ /^https?:\/\// id = id.to_s.split('/').last LOGGER.info("ID(#{id}) normalised for #{self.name}") if ConfigFile[:debug] model.instance_variable_set("@id", id) end model rescue StandardError => e Solis::LOGGER.error(e.) raise Solis::Error::GeneralError, "Error generating id for #{self.name}" end |
.metadata ⇒ Object
337 338 339 |
# File 'lib/solis/model.rb', line 337 def self. end |
.metadata=(m) ⇒ Object
341 342 343 |
# File 'lib/solis/model.rb', line 341 def self.=(m) = m end |
.model(level = 0) ⇒ Object
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
# File 'lib/solis/model.rb', line 393 def self.model(level = 0) m = { type: self.name.tableize, attributes: {} } self.[:attributes].each do |attribute, | is_array = (([:maxcount].nil? || ([:maxcount].to_i > 1)) && ![:datatype].eql?(:lang_string)) attribute_name = is_array ? "#{attribute}[]" : attribute if .key?(:class) && ![:class].nil? && [:class].value =~ /#{self.graph_name}/ && level == 0 cm = self.graph.shape_as_model(self.[:attributes][attribute][:datatype].to_s).model(level + 1) m[:attributes][attribute_name.to_sym] = cm[:attributes] else m[:attributes][attribute_name.to_sym] = { description: [:comment]&.value, mandatory: ([:mincount].to_i > 0), data_type: [:datatype] } m[:attributes][attribute_name.to_sym][:order] = [:order]&.value.to_i if .key?(:order) && ![:order].nil? end end m end |
.model_after_create(&blk) ⇒ Object
443 444 445 |
# File 'lib/solis/model.rb', line 443 def self.model_after_create(&blk) self.after_create_proc = blk end |
.model_after_delete(&blk) ⇒ Object
459 460 461 |
# File 'lib/solis/model.rb', line 459 def self.model_after_delete(&blk) self.after_delete_proc = blk end |
.model_after_read(&blk) ⇒ Object
435 436 437 |
# File 'lib/solis/model.rb', line 435 def self.model_after_read(&blk) self.after_read_proc = blk end |
.model_after_update(&blk) ⇒ Object
451 452 453 |
# File 'lib/solis/model.rb', line 451 def self.model_after_update(&blk) self.after_update_proc = blk end |
.model_before_create(&blk) ⇒ Object
439 440 441 |
# File 'lib/solis/model.rb', line 439 def self.model_before_create(&blk) self.before_create_proc = blk end |
.model_before_delete(&blk) ⇒ Object
455 456 457 |
# File 'lib/solis/model.rb', line 455 def self.model_before_delete(&blk) self.before_delete_proc = blk end |
.model_before_read(&blk) ⇒ Object
431 432 433 |
# File 'lib/solis/model.rb', line 431 def self.model_before_read(&blk) self.before_read_proc = blk end |
.model_before_update(&blk) ⇒ Object
447 448 449 |
# File 'lib/solis/model.rb', line 447 def self.model_before_update(&blk) self.before_update_proc = blk end |
.model_template(level = 0) ⇒ Object
412 413 414 415 416 417 418 419 420 421 422 423 424 425 |
# File 'lib/solis/model.rb', line 412 def self.model_template(level = 0) m = { type: self.name.tableize, attributes: {} } self.[:attributes].each do |attribute, | if .key?(:class) && ![:class].nil? && [:class].value =~ /#{self.graph_name}/ && level == 0 cm = self.graph.shape_as_model(self.[:attributes][attribute][:datatype].to_s).model_template(level + 1) m[:attributes][attribute.to_sym] = cm[:attributes] else m[:attributes][attribute.to_sym] = '' end end m end |
.shapes ⇒ Object
349 350 351 |
# File 'lib/solis/model.rb', line 349 def self.shapes @shapes end |
.shapes=(s) ⇒ Object
345 346 347 |
# File 'lib/solis/model.rb', line 345 def self.shapes=(s) @shapes = s end |
.sparql_endpoint ⇒ Object
369 370 371 |
# File 'lib/solis/model.rb', line 369 def self.sparql_endpoint @sparql_endpoint end |
.sparql_endpoint=(sparql_endpoint) ⇒ Object
373 374 375 |
# File 'lib/solis/model.rb', line 373 def self.sparql_endpoint=(sparql_endpoint) @sparql_endpoint = sparql_endpoint end |
Instance Method Details
#destroy ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/solis/model.rb', line 108 def destroy raise "I need a SPARQL endpoint" if self.class.sparql_endpoint.nil? sparql = Solis::Store::Sparql::Client.new(self.class.sparql_endpoint) raise Solis::Error::QueryError, "#{self.id} is still referenced, refusing to delete" if is_referenced?(sparql) before_delete_proc&.call(self) query = %( with <#{self.class.graph_name}> delete {?s ?p ?o} where { values ?s {<#{self.graph_id}>} ?s ?p ?o } ) result = sparql.query(query) if result.count > 0 if result.first.bound?(result.variable_names.first) && result.first[result.variable_names.first].value =~ /done$/ after_delete_proc&.call(self) else after_delete_proc&.call(result) end end result end |
#dump(format = :ttl, resolve_all = true) ⇒ Object
84 85 86 87 |
# File 'lib/solis/model.rb', line 84 def dump(format = :ttl, resolve_all = true) graph = as_graph(self, resolve_all) graph.dump(format) end |
#exists?(sparql) ⇒ Boolean
309 310 311 |
# File 'lib/solis/model.rb', line 309 def exists?(sparql) sparql.query("ASK WHERE { <#{self.graph_id}> ?p ?o }") end |
#graph_id ⇒ Object
301 302 303 |
# File 'lib/solis/model.rb', line 301 def graph_id "#{self.class.graph_name}#{tableized_class_name(self)}/#{self.id}" end |
#is_referenced?(sparql) ⇒ Boolean
305 306 307 |
# File 'lib/solis/model.rb', line 305 def is_referenced?(sparql) sparql.query("ASK WHERE { ?s ?p <#{self.graph_id}>. filter (!contains(str(?s), 'audit') && !contains(str(?p), 'audit'))}") end |
#model_class_name(plural = false) ⇒ Object
Removed the ‘name’ instance method to avoid conflicts with ‘name’ attributes Use model_class_name instead, or access @model_name directly
62 63 64 65 66 67 68 |
# File 'lib/solis/model.rb', line 62 def model_class_name(plural = false) if plural @model_plural_name else @model_name end end |
#query ⇒ Object
70 71 72 73 74 75 76 77 |
# File 'lib/solis/model.rb', line 70 def query raise "I need a SPARQL endpoint" if self.class.sparql_endpoint.nil? # before_read_proc&.call(self) result = Solis::Query.new(self) # after_read_proc&.call(result) result end |
#save(validate_dependencies = true, top_level = true) ⇒ Object
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/solis/model.rb', line 136 def save(validate_dependencies = true, top_level = true) raise "I need a SPARQL endpoint" if self.class.sparql_endpoint.nil? sparql = SPARQL::Client.new(self.class.sparql_endpoint) before_create_proc&.call(self) if self.exists?(sparql) data = properties_to_hash(self) result = update(data) else data = properties_to_hash(self) attributes = data.include?('attributes') ? data['attributes'] : data attributes.each_pair do |key, value| unless self.class.[:attributes][key][:node].nil? value = [value] unless value.is_a?(Array) value.each do |sub_value| = self.class.graph.shape_as_model(self.class.[:attributes][key][:datatype].to_s).new(sub_value) = Solis::Options.instance.get[:embedded_readonly].map{|s| s.to_s} || [] if (.class.ancestors.map{|s| s.to_s} & ).empty? || top_level if .exists?(sparql) = properties_to_hash() .update(, validate_dependencies, false) else .save(validate_dependencies, false) end else Solis::LOGGER.info("#{embedded.class.name} is embedded not allowed to change. Skipping") end end end end graph = as_graph(self, validate_dependencies) Solis::LOGGER.info SPARQL::Client::Update::InsertData.new(graph, graph: graph.name).to_s if ConfigFile[:debug] result = sparql.insert_data(graph, graph: graph.name) end after_create_proc&.call(self) self rescue StandardError => e Solis::LOGGER.error e. raise e end |
#to_graph(resolve_all = true) ⇒ Object
89 90 91 |
# File 'lib/solis/model.rb', line 89 def to_graph(resolve_all = true) as_graph(self, resolve_all) end |
#to_ttl(resolve_all = true) ⇒ Object
79 80 81 82 |
# File 'lib/solis/model.rb', line 79 def to_ttl(resolve_all = true) graph = as_graph(self, resolve_all) graph.dump(:ttl) end |
#update(data, validate_dependencies = true, top_level = true) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
# File 'lib/solis/model.rb', line 183 def update(data, validate_dependencies = true, top_level = true) raise Solis::Error::GeneralError, "I need a SPARQL endpoint" if self.class.sparql_endpoint.nil? attributes = data.include?('attributes') ? data['attributes'] : data raise "id is mandatory when updating" unless attributes.keys.include?('id') id = attributes.delete('id') sparql = SPARQL::Client.new(self.class.sparql_endpoint) original_klass = self.query.filter({ language: self.class.language, filters: { id: [id] } }).find_all.map { |m| m }&.first raise Solis::Error::NotFoundError if original_klass.nil? updated_klass = original_klass.deep_dup # Track entities to potentially delete entities_to_check_for_deletion = {} attributes.each_pair do |key, value| unless original_klass.class.[:attributes][key][:node].nil? value = [value] unless value.is_a?(Array) # Get original embedded entities for this attribute = original_klass.instance_variable_get("@#{key}") = [] unless .nil? || .is_a?(Array) ||= [] # Track original IDs original_ids = .map { |e| solis_model?(e) ? e.id : nil }.compact # Build new array of embedded entities = [] new_ids = [] value.each do |sub_value| = self.class.graph.shape_as_model(original_klass.class.[:attributes][key][:datatype].to_s).new(sub_value) new_ids << .id if .id = Solis::Options.instance.get[:embedded_readonly].map{|s| s.to_s} || [] if (.class.ancestors.map{|s| s.to_s} & ).empty? || top_level if .exists?(sparql) = properties_to_hash() .update(, validate_dependencies, false) << else = .save(validate_dependencies, false) << end else Solis::LOGGER.info("#{embedded.class.name} is embedded not allowed to change. Skipping") << end end # Identify orphaned entities (in original but not in new) orphaned_ids = original_ids - new_ids unless orphaned_ids.empty? orphaned_entities = .select { |e| solis_model?(e) && orphaned_ids.include?(e.id) } entities_to_check_for_deletion[key] = orphaned_entities end # Replace entire array with new values maxcount = original_klass.class.[:attributes][key][:maxcount] value = maxcount && maxcount == 1 ? .first : end updated_klass.instance_variable_set("@#{key}", value) end before_update_proc&.call(original_klass, updated_klass) properties_original_klass = properties_to_hash(original_klass) properties_updated_klass = properties_to_hash(updated_klass) if Hashdiff.best_diff(properties_original_klass, properties_updated_klass).empty? Solis::LOGGER.info("#{original_klass.class.name} unchanged, skipping") data = self.query.filter({ filters: { id: [id] } }).find_all.map { |m| m }&.first else delete_graph = as_graph(original_klass, false) where_graph = RDF::Graph.new(graph_name: RDF::URI("#{self.class.graph_name}#{tableized_class_name(self)}/#{id}"), data: RDF::Repository.new) if id.is_a?(Array) id.each do |i| where_graph << [RDF::URI("#{self.class.graph_name}#{tableized_class_name(self)}/#{i}"), :p, :o] end else where_graph << [RDF::URI("#{self.class.graph_name}#{tableized_class_name(self)}/#{id}"), :p, :o] end insert_graph = as_graph(updated_klass, true) delete_insert_query = SPARQL::Client::Update::DeleteInsert.new(delete_graph, insert_graph, where_graph, graph: insert_graph.name).to_s delete_insert_query.gsub!('_:p', '?p') data = sparql.query(delete_insert_query) data = self.query.filter({ filters: { id: [id] } }).find_all.map { |m| m }&.first if data.nil? sparql.insert_data(insert_graph, graph: insert_graph.name) data = self.query.filter({ filters: { id: [id] } }).find_all.map { |m| m }&.first end # Delete orphaned entities after successful update delete_orphaned_entities(entities_to_check_for_deletion, sparql) end after_update_proc&.call(updated_klass, data) data rescue StandardError => e original_graph = as_graph(original_klass, false) Solis::LOGGER.error(e.) Solis::LOGGER.error original_graph.dump(:ttl) Solis::LOGGER.error delete_insert_query if defined?(delete_insert_query) sparql.insert_data(original_graph, graph: original_graph.name) raise e end |
#valid? ⇒ Boolean
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/solis/model.rb', line 93 def valid? begin graph = as_graph(self, false) rescue Solis::Error::InvalidAttributeError => e Solis::LOGGER.error(e.) end shacl = SHACL.get_shapes(self.class.graph.instance_variable_get(:"@graph")) report = shacl.execute(graph) report.conform? rescue StandardError => e false end |