Class: ActiveFedora::RelationshipGraph
- Inherits:
-
Object
- Object
- ActiveFedora::RelationshipGraph
- Defined in:
- lib/active_fedora/relationship_graph.rb
Instance Attribute Summary collapse
-
#dirty ⇒ Object
Returns the value of attribute dirty.
-
#relationships ⇒ Object
Returns the value of attribute relationships.
Instance Method Summary collapse
- #[](predicate) ⇒ Object
- #add(predicate, object, literal = false) ⇒ Object
-
#build_statement(uri, predicate, target) ⇒ Object
Create an RDF statement.
-
#delete(predicate, object = nil) ⇒ Object
Remove the statement matching the predicate and object [predicate] the predicate to delete [object] the object to delete, if nil, all statements with this predicate are deleted.
- #has_predicate?(predicate) ⇒ Boolean
-
#initialize ⇒ RelationshipGraph
constructor
A new instance of RelationshipGraph.
- #to_graph(subject_uri) ⇒ Object
Constructor Details
#initialize ⇒ RelationshipGraph
Returns a new instance of RelationshipGraph.
7 8 9 10 |
# File 'lib/active_fedora/relationship_graph.rb', line 7 def initialize self.dirty = false self.relationships = Hash.new { |h, k| h[k] = [] } end |
Instance Attribute Details
#dirty ⇒ Object
Returns the value of attribute dirty.
4 5 6 |
# File 'lib/active_fedora/relationship_graph.rb', line 4 def dirty @dirty end |
#relationships ⇒ Object
Returns the value of attribute relationships.
4 5 6 |
# File 'lib/active_fedora/relationship_graph.rb', line 4 def relationships @relationships end |
Instance Method Details
#[](predicate) ⇒ Object
49 50 51 |
# File 'lib/active_fedora/relationship_graph.rb', line 49 def [](predicate) relationships[predicate] end |
#add(predicate, object, literal = false) ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/active_fedora/relationship_graph.rb', line 16 def add(predicate, object, literal=false) unless relationships.has_key? predicate relationships[predicate] = [] end object = RDF::Literal.new(object) if literal unless relationships[predicate].include?(object) @dirty = true relationships[predicate] << object end end |
#build_statement(uri, predicate, target) ⇒ Object
Create an RDF statement
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/active_fedora/relationship_graph.rb', line 70 def build_statement(uri, predicate, target) raise "Not allowed anymore" if uri == :self target = target.internal_uri if target.respond_to? :internal_uri subject = RDF::URI.new(uri) #TODO cache if target.is_a? RDF::Literal or target.is_a? RDF::Resource object = target else begin target_uri = (target.is_a? URI) ? target : URI.parse(target) if target_uri.scheme.nil? raise ArgumentError, "Invalid target \"#{target}\". Must have namespace." end if target_uri.to_s =~ /\A[\w\-]+:[\w\-]+\Z/ raise ArgumentError, "Invalid target \"#{target}\". Target should be a complete URI, and not a pid." end rescue URI::InvalidURIError raise ArgumentError, "Invalid target \"#{target}\". Target must be specified as a literal, or be a valid URI." end object = RDF::URI.new(target) end predicate = ActiveFedora::Predicates.find_graph_predicate(predicate) unless predicate.kind_of? RDF::URI RDF::Statement.new(subject, predicate, object) end |
#delete(predicate, object = nil) ⇒ Object
Remove the statement matching the predicate and object
- predicate
-
the predicate to delete
- object
-
the object to delete, if nil, all statements with this predicate are deleted.
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/active_fedora/relationship_graph.rb', line 30 def delete(predicate, object = nil) return unless relationships.has_key? predicate if object.nil? @dirty = true relationships.delete(predicate) return end if relationships[predicate].include?(object) @dirty = true relationships[predicate].delete(object) end if object.respond_to?(:internal_uri) && relationships[predicate].include?(object.internal_uri) @dirty = true relationships[predicate].delete(object.internal_uri) elsif object.is_a? String relationships[predicate].delete_if{|obj| obj.respond_to?(:internal_uri) && obj.internal_uri == object} end end |
#has_predicate?(predicate) ⇒ Boolean
12 13 14 |
# File 'lib/active_fedora/relationship_graph.rb', line 12 def has_predicate?(predicate) relationships.has_key? predicate end |
#to_graph(subject_uri) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/active_fedora/relationship_graph.rb', line 53 def to_graph(subject_uri) # need to destroy relationships and rewrite it. subject = RDF::URI.new(subject_uri) graph = RDF::Graph.new relationships.each do |predicate, values| values.each do |object| graph.insert build_statement(subject, predicate, object) end end graph end |