Class: ActiveFedora::RdfNode::TermProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/active_fedora/rdf_node/term_proxy.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, subject, options) ⇒ TermProxy

Returns a new instance of TermProxy.

Parameters:

  • parent

    RdfNode

  • subject

    RDF::URI

  • options

    Hash



12
13
14
15
16
# File 'lib/active_fedora/rdf_node/term_proxy.rb', line 12

def initialize(parent, subject, options)
  @parent = parent
  @subject = subject
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/active_fedora/rdf_node/term_proxy.rb', line 5

def options
  @options
end

#parentObject (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

#subjectObject (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, options.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(options.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

#clearObject

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, options.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, options.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_valuesObject

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, options.predicate).each do |solution|
    v = solution.value
    v = v.to_s if v.is_a? RDF::Literal
    if options.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 options.class_name
      klass =  class_from_rdf_type(v)
      values << v if klass == ActiveFedora.class_from_string(options.class_name, parent.class)
    else
      values << v
    end
  end

  if options.class_name
    values = values.map{ |found_subject| class_from_rdf_type(found_subject).new(parent.graph, found_subject)}
  end
  
  options.multivalue ? values : values.first
end

#mint_node(attributes) ⇒ Object

Parameters:

  • attributes (Hash)

Options Hash (attributes):

  • id (Object)

    the rdf subject to use for the node, if omitted the new node will be a b-node



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(options).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

#targetObject



72
73
74
# File 'lib/active_fedora/rdf_node/term_proxy.rb', line 72

def target
  @target ||= load_values
end