Module: ActiveFedora::SemanticNode
- Extended by:
- ActiveSupport::Concern
- 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.
-
#conforms_to?(model_class) ⇒ Boolean
Checks that this object is matches the model class passed in.
- #ids_for_outbound(predicate) ⇒ Object
-
#inbound_relationship_predicates ⇒ Hash
Return hash of inbound relationship names and predicate pairs.
- #inbound_relationships(response_format = :uri) ⇒ Object
- #load_relationships ⇒ Object
- #object_relations ⇒ Object
-
#outbound_relationship_predicates ⇒ Hash
Return hash of outbound relationship names and predicate pairs.
- #outbound_relationships ⇒ Object
-
#relationship_predicates ⇒ Hash
Return hash of relationship names and predicate pairs (inbound and outbound).
-
#relationships(*args) ⇒ Object
If no arguments are supplied, return the whole RDF::Graph.
- #relationships_are_dirty ⇒ Object
- #relationships_are_dirty=(val) ⇒ Object
-
#relationships_desc ⇒ Hash
Return hash that persists relationship metadata defined by has_relationship calls.
-
#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.
7 8 9 |
# File 'lib/active_fedora/semantic_node.rb', line 7 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.
28 29 30 31 |
# File 'lib/active_fedora/semantic_node.rb', line 28 def add_relationship(predicate, target, literal=false) 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
35 36 37 38 39 40 |
# File 'lib/active_fedora/semantic_node.rb', line 35 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 |
#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.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/active_fedora/semantic_node.rb', line 50 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
124 125 126 127 128 129 |
# File 'lib/active_fedora/semantic_node.rb', line 124 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) ? o.gsub("info:fedora/", "") : o.pid end end |
#inbound_relationship_predicates ⇒ Hash
Return hash of inbound relationship names and predicate pairs
166 167 168 |
# File 'lib/active_fedora/semantic_node.rb', line 166 def inbound_relationship_predicates relationship_predicates.has_key?(:inbound) ? relationship_predicates[:inbound] : {} end |
#inbound_relationships(response_format = :uri) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/active_fedora/semantic_node.rb', line 80 def inbound_relationships(response_format=:uri) rel_values = {} inbound_relationship_predicates.each_pair do |name,predicate| objects = self.send("#{name}",{:response_format=>response_format}) items = [] objects.each do |object| if (response_format == :uri) #inbound relationships are always object properties items.push(object.internal_uri) else items.push(object) end end unless items.empty? rel_values.merge!({predicate=>items}) end end return rel_values end |
#load_relationships ⇒ Object
117 118 119 120 121 122 |
# File 'lib/active_fedora/semantic_node.rb', line 117 def load_relationships self.relationships_loaded = true content = rels_ext.content return unless content.present? RelsExtDatastream.from_xml content, rels_ext end |
#object_relations ⇒ Object
13 14 15 16 |
# File 'lib/active_fedora/semantic_node.rb', line 13 def object_relations load_relationships if !relationships_loaded @object_relations ||= RelationshipGraph.new end |
#outbound_relationship_predicates ⇒ Hash
Return hash of outbound relationship names and predicate pairs
160 161 162 |
# File 'lib/active_fedora/semantic_node.rb', line 160 def outbound_relationship_predicates relationship_predicates.has_key?(:self) ? relationship_predicates[:self] : {} end |
#outbound_relationships ⇒ Object
100 101 102 |
# File 'lib/active_fedora/semantic_node.rb', line 100 def outbound_relationships() relationships.statements end |
#relationship_predicates ⇒ Hash
Return hash of relationship names and predicate pairs (inbound and outbound). It retrieves this information via the relationships_desc hash in the class.
146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/active_fedora/semantic_node.rb', line 146 def relationship_predicates return @relationship_predicates if @relationship_predicates @relationship_predicates = {} relationships_desc.each_pair do |subj, names| @relationship_predicates[subj] = {} names.each_pair do |name, args| @relationship_predicates[subj][name] = args[:predicate] end end @relationship_predicates 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
106 107 108 109 110 111 112 113 114 115 |
# File 'lib/active_fedora/semantic_node.rb', line 106 def relationships(*args) load_relationships if !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 } #TODO, could just return the object end |
#relationships_are_dirty ⇒ Object
18 19 20 |
# File 'lib/active_fedora/semantic_node.rb', line 18 def relationships_are_dirty object_relations.dirty end |
#relationships_are_dirty=(val) ⇒ Object
21 22 23 |
# File 'lib/active_fedora/semantic_node.rb', line 21 def relationships_are_dirty=(val) object_relations.dirty = val end |
#relationships_desc ⇒ Hash
Return hash that persists relationship metadata defined by has_relationship calls
139 140 141 |
# File 'lib/active_fedora/semantic_node.rb', line 139 def relationships_desc @relationships_desc ||= self.class.relationships_desc end |
#remove_relationship(predicate, obj, literal = false) ⇒ Object
Remove a Rels-Ext relationship from the Object.
74 75 76 77 78 |
# File 'lib/active_fedora/semantic_node.rb', line 74 def remove_relationship(predicate, obj, literal=false) object_relations.delete(predicate, obj) self.relationships_are_dirty = true rels_ext.content_will_change! end |