Class: ActiveFedora::RdfNode::TermProxy
- Inherits:
-
Object
- Object
- ActiveFedora::RdfNode::TermProxy
- Defined in:
- lib/active_fedora/rdf_node/term_proxy.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#subject ⇒ Object
readonly
Returns the value of attribute subject.
Instance Method Summary collapse
- #<<(*values) ⇒ Object
- #build(attributes = {}) ⇒ Object
-
#clear ⇒ Object
Remove all matching nodes from the graph.
- #delete(*values) ⇒ Object
- #first_or_create(attributes = {}) ⇒ Object
-
#initialize(parent, subject, options) ⇒ TermProxy
constructor
A new instance of TermProxy.
-
#load_values ⇒ Object
Get the values off of the rdf nodes this proxy targets.
- #mint_node(attributes) ⇒ Object
- #reset! ⇒ Object
- #target ⇒ Object
Constructor Details
#initialize(parent, subject, options) ⇒ TermProxy
Returns a new instance of TermProxy.
12 13 14 15 16 |
# File 'lib/active_fedora/rdf_node/term_proxy.rb', line 12 def initialize(parent, subject, ) @parent = parent @subject = subject @options = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/active_fedora/rdf_node/term_proxy.rb', line 5 def @options end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
5 6 7 |
# File 'lib/active_fedora/rdf_node/term_proxy.rb', line 5 def parent @parent end |
#subject ⇒ Object (readonly)
Returns the value of attribute subject.
5 6 7 |
# File 'lib/active_fedora/rdf_node/term_proxy.rb', line 5 def subject @subject end |
Instance Method Details
#<<(*values) ⇒ Object
58 59 60 61 62 |
# File 'lib/active_fedora/rdf_node/term_proxy.rb', line 58 def <<(*values) values.each { |value| parent.append(subject, .predicate, value) } reset! values end |
#build(attributes = {}) ⇒ Object
19 20 21 22 23 24 25 26 27 |
# File 'lib/active_fedora/rdf_node/term_proxy.rb', line 19 def build(attributes={}) node = mint_node(attributes) parent.insert_child(.predicate, node) reset! new_node = target.find { |n| n.rdf_subject == node.rdf_subject} new_node = node unless new_node #if it's a list, the find doesn't work, just use the new node new_node.new_record = true new_node end |
#clear ⇒ Object
Remove all matching nodes from the graph
34 35 36 37 38 39 40 41 42 43 |
# File 'lib/active_fedora/rdf_node/term_proxy.rb', line 34 def clear parent.query(subject, .predicate).each do |solution| # TODO - Recursive delete # Delete everythign we're pointing at. parent.graph.delete([solution.value, nil, nil]) if solution.value.uri? end # Delete all the assertions about this object parent.graph.delete([subject, nil, nil]) reset! end |
#delete(*values) ⇒ Object
64 65 66 67 68 69 70 |
# File 'lib/active_fedora/rdf_node/term_proxy.rb', line 64 def delete(*values) values.each do |value| parent.delete_predicate(subject, .predicate, value) end values end |
#first_or_create(attributes = {}) ⇒ Object
29 30 31 |
# File 'lib/active_fedora/rdf_node/term_proxy.rb', line 29 def first_or_create(attributes={}) first || build(attributes) end |
#load_values ⇒ Object
Get the values off of the rdf nodes this proxy targets
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/active_fedora/rdf_node/term_proxy.rb', line 77 def load_values values = [] parent.query(subject, .predicate).each do |solution| v = solution.value v = v.to_s if v.is_a? RDF::Literal if .type == :date v = Date.parse(v) end # If the user provided options[:class_name], we should query to make sure this # potential solution is of the right RDF.type if .class_name klass = class_from_rdf_type(v) values << v if klass == ActiveFedora.class_from_string(.class_name, parent.class) else values << v end end if .class_name values = values.map{ |found_subject| class_from_rdf_type(found_subject).new(parent.graph, found_subject)} end .multivalue ? values : values.first end |
#mint_node(attributes) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/active_fedora/rdf_node/term_proxy.rb', line 51 def mint_node(attributes) new_subject = attributes.key?('id') ? RDF::URI.new(attributes.delete('id')) : RDF::Node.new return parent.target_class().new(parent.graph, new_subject).tap do |node| node.attributes = attributes if attributes end end |
#reset! ⇒ Object
45 46 47 |
# File 'lib/active_fedora/rdf_node/term_proxy.rb', line 45 def reset! @target = nil end |
#target ⇒ Object
72 73 74 |
# File 'lib/active_fedora/rdf_node/term_proxy.rb', line 72 def target @target ||= load_values end |