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.
- #has_predicate?(predicate) ⇒ Boolean
-
#initialize ⇒ RelationshipGraph
constructor
A new instance of RelationshipGraph.
- #to_graph(subject_uri) ⇒ Object
- #uri_predicate(predicate) ⇒ Object
Constructor Details
#initialize ⇒ RelationshipGraph
Returns a new instance of RelationshipGraph.
6 7 8 9 |
# File 'lib/active_fedora/relationship_graph.rb', line 6 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
50 51 52 53 |
# File 'lib/active_fedora/relationship_graph.rb', line 50 def [](predicate) uri = uri_predicate(predicate) relationships[uri] end |
#add(predicate, object, literal = false) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/active_fedora/relationship_graph.rb', line 15 def add(predicate, object, literal=false) uri = uri_predicate(predicate) unless has_predicate? uri relationships[uri] = [] end object = RDF::Literal.new(object) if literal unless relationships[uri].include?(object) @dirty = true relationships[uri] << object end end |
#build_statement(uri, predicate, target) ⇒ Object
Create an RDF statement
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/active_fedora/relationship_graph.rb', line 72 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 RDF::Statement.new(subject, uri_predicate(predicate), object) end |
#delete(predicate, object = nil) ⇒ Object
Remove the statement matching the predicate and object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/active_fedora/relationship_graph.rb', line 30 def delete(predicate, object = nil) uri = uri_predicate(predicate) return unless has_predicate? uri if object.nil? @dirty = true relationships.delete(uri) return end if relationships[uri].include?(object) @dirty = true relationships[uri].delete(object) end if object.respond_to?(:internal_uri) && relationships[uri].include?(object.internal_uri) @dirty = true relationships[uri].delete(object.internal_uri) elsif object.is_a? String relationships[uri].delete_if{|obj| obj.respond_to?(:internal_uri) && obj.internal_uri == object} end end |
#has_predicate?(predicate) ⇒ Boolean
11 12 13 |
# File 'lib/active_fedora/relationship_graph.rb', line 11 def has_predicate?(predicate) relationships.has_key? uri_predicate(predicate) end |
#to_graph(subject_uri) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/active_fedora/relationship_graph.rb', line 55 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 |
#uri_predicate(predicate) ⇒ Object
96 97 98 99 |
# File 'lib/active_fedora/relationship_graph.rb', line 96 def uri_predicate(predicate) return predicate if predicate.kind_of? RDF::URI ActiveFedora::Predicates.find_graph_predicate(predicate) end |