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, predicate, options) ⇒ TermProxy



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

def initialize(parent, subject, predicate, options)
  @parent = parent
  @subject = subject
  @predicate = predicate
  @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

#predicateObject (readonly)

Returns the value of attribute predicate.



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

def predicate
  @predicate
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



55
56
57
58
59
# File 'lib/active_fedora/rdf_node/term_proxy.rb', line 55

def <<(*values)
  values.each { |value| parent.append(subject, predicate, value) }
  reset!
  values
end

#build(attributes = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/active_fedora/rdf_node/term_proxy.rb', line 20

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

#clearObject

Remove all matching nodes from the graph



31
32
33
34
35
36
37
38
39
40
# File 'lib/active_fedora/rdf_node/term_proxy.rb', line 31

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



61
62
63
64
65
66
67
# File 'lib/active_fedora/rdf_node/term_proxy.rb', line 61

def delete(*values)
  values.each do |value| 
    parent.delete_predicate(subject, predicate, value)
  end

  values
end

#load_valuesObject

Get the values off of the rdf nodes this proxy targets



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/active_fedora/rdf_node/term_proxy.rb', line 74

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 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

Options Hash (attributes):

  • id (Object)

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



48
49
50
51
52
53
# File 'lib/active_fedora/rdf_node/term_proxy.rb', line 48

def mint_node(attributes)
  new_subject = attributes.key?('id') ? RDF::URI.new(attributes.delete('id')) : RDF::Node.new
  return parent.target_class(predicate).new(parent.graph, new_subject).tap do |node|
    node.attributes = attributes if attributes
  end
end

#reset!Object



42
43
44
# File 'lib/active_fedora/rdf_node/term_proxy.rb', line 42

def reset!
  @target = nil
end

#targetObject



69
70
71
# File 'lib/active_fedora/rdf_node/term_proxy.rb', line 69

def target
  @target ||= load_values
end