Module: ActiveFedora::SemanticNode
- Extended by:
- ActiveSupport::Concern, Deprecation
- Included in:
- Base
- Defined in:
- lib/active_fedora/semantic_node.rb
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#load_from_solr ⇒ Object
Returns the value of attribute load_from_solr.
-
#relationships_loaded ⇒ Object
Returns the value of attribute relationships_loaded.
-
#subject ⇒ Object
Returns the value of attribute subject.
Instance Method Summary collapse
-
#add_relationship(predicate, target, literal = false) ⇒ Object
Add a relationship to the Object.
- #assert_kind_of(n, o, t) ⇒ Object
-
#clear_relationship(predicate) ⇒ Object
Clears all relationships with the specified predicate.
- #clear_relationships ⇒ Object
-
#conforms_to?(model_class) ⇒ Boolean
Checks that this object is matches the model class passed in.
- #ids_for_outbound(predicate) ⇒ Object
-
#internal_uri ⇒ String
The internal fedora URI.
- #load_relationships ⇒ Object
- #object_relations ⇒ Object
-
#relationships(*args) ⇒ Object
If no arguments are supplied, return the whole RDF::Graph.
- #relationships=(xml) ⇒ Object
- #relationships_are_dirty? ⇒ Boolean (also: #relationships_are_dirty)
- #relationships_are_not_dirty! ⇒ Object
-
#remove_relationship(predicate, obj, literal = false) ⇒ Object
Remove a Rels-Ext relationship from the Object.
Instance Attribute Details
#load_from_solr ⇒ Object
Returns the value of attribute load_from_solr.
7 8 9 |
# File 'lib/active_fedora/semantic_node.rb', line 7 def load_from_solr @load_from_solr end |
#relationships_loaded ⇒ Object
Returns the value of attribute relationships_loaded.
6 7 8 |
# File 'lib/active_fedora/semantic_node.rb', line 6 def relationships_loaded @relationships_loaded end |
#subject ⇒ Object
Returns the value of attribute subject.
7 8 9 |
# File 'lib/active_fedora/semantic_node.rb', line 7 def subject @subject end |
Instance Method Details
#add_relationship(predicate, target, literal = false) ⇒ Object
Add a relationship to the Object. TODO is target ever a AF::Base anymore?
49 50 51 52 53 |
# File 'lib/active_fedora/semantic_node.rb', line 49 def add_relationship(predicate, target, literal=false) #raise ArgumentError, "predicate must be a symbol. You provided `#{predicate.inspect}'" unless predicate.class.in?([Symbol, String]) object_relations.add(predicate, target, literal) rels_ext.content_will_change! if object_relations.dirty end |
#assert_kind_of(n, o, t) ⇒ Object
9 10 11 |
# File 'lib/active_fedora/semantic_node.rb', line 9 def assert_kind_of(n, o,t) raise "Assertion failure: #{n}: #{o} is not of type #{t}" unless o.kind_of?(t) end |
#clear_relationship(predicate) ⇒ Object
Clears all relationships with the specified predicate
57 58 59 60 61 62 |
# File 'lib/active_fedora/semantic_node.rb', line 57 def clear_relationship(predicate) relationships(predicate).each do |target| object_relations.delete(predicate, target) end rels_ext.content_will_change! if object_relations.dirty end |
#clear_relationships ⇒ Object
13 14 15 16 |
# File 'lib/active_fedora/semantic_node.rb', line 13 def clear_relationships @relationships_loaded = false @object_relations = nil end |
#conforms_to?(model_class) ⇒ Boolean
Checks that this object is matches the model class passed in. It requires two steps to pass to return true
1. It has a hasModel relationship of the same model
2. kind_of? returns true for the model passed in
This method can most often be used to detect if an object from Fedora that was created with a different model was then used to populate this object.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/active_fedora/semantic_node.rb', line 72 def conforms_to?(model_class) if self.kind_of?(model_class) #check has model and class match mod = relationships.first(:predicate=>Predicates.find_graph_predicate(:has_model)) if mod expected = self.class.to_class_uri if mod.object.to_s == expected return true else raise "has_model relationship check failed for model #{model_class} raising exception, expected: '#{expected}' actual: '#{mod.object.to_s}'" end else raise "has_model relationship does not exist for model #{model_class} check raising exception" end else raise "kind_of? check failed for model #{model_class}, actual #{self.class} raising exception" end return false end |
#ids_for_outbound(predicate) ⇒ Object
122 123 124 125 126 127 |
# File 'lib/active_fedora/semantic_node.rb', line 122 def ids_for_outbound(predicate) (object_relations[predicate] || []).map do |o| o = o.to_s if o.kind_of? RDF::Literal o.kind_of?(String) ? self.class.pid_from_uri(o) : o.pid end end |
#internal_uri ⇒ String
Returns the internal fedora URI.
130 131 132 |
# File 'lib/active_fedora/semantic_node.rb', line 130 def internal_uri self.class.internal_uri(pid) end |
#load_relationships ⇒ Object
115 116 117 118 119 120 |
# File 'lib/active_fedora/semantic_node.rb', line 115 def load_relationships @relationships_loaded = true content = rels_ext.content return unless content.present? RelsExtDatastream.from_xml content, rels_ext end |
#object_relations ⇒ Object
18 19 20 21 |
# File 'lib/active_fedora/semantic_node.rb', line 18 def object_relations load_relationships if !relationships_loaded @object_relations ||= RelationshipGraph.new end |
#relationships(*args) ⇒ Object
If no arguments are supplied, return the whole RDF::Graph. if a predicate is supplied as a parameter, then it returns the result of quering the graph with that predicate
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/active_fedora/semantic_node.rb', line 104 def relationships(*args) load_relationships unless relationships_loaded if args.empty? raise "Must have internal_uri" unless internal_uri return object_relations.to_graph(internal_uri) end rels = object_relations[args.first] || [] rels.map {|o| o.respond_to?(:internal_uri) ? o.internal_uri : o }.compact #TODO, could just return the object end |
#relationships=(xml) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/active_fedora/semantic_node.rb', line 32 def relationships=(xml) RDF::RDFXML::Reader.new(xml) do |reader| reader.each_statement do |statement| literal = statement.object.kind_of?(RDF::Literal) object = literal ? statement.object.value : statement.object.to_str object_relations.add(statement.predicate, object, literal) end end # Adding the relationships to the graph causes the graph to be marked as dirty, # so now we assert that the graph is in sync relationships_are_not_dirty! end |
#relationships_are_dirty? ⇒ Boolean Also known as: relationships_are_dirty
23 24 25 |
# File 'lib/active_fedora/semantic_node.rb', line 23 def relationships_are_dirty? object_relations.dirty end |
#relationships_are_not_dirty! ⇒ Object
28 29 30 |
# File 'lib/active_fedora/semantic_node.rb', line 28 def relationships_are_not_dirty! object_relations.dirty = false end |
#remove_relationship(predicate, obj, literal = false) ⇒ Object
Remove a Rels-Ext relationship from the Object.
96 97 98 99 100 |
# File 'lib/active_fedora/semantic_node.rb', line 96 def remove_relationship(predicate, obj, literal=false) object_relations.delete(predicate, obj) object_relations.dirty = true rels_ext.content_will_change! end |