Module: ActiveFedora::RdfNode

Extended by:
ActiveSupport::Autoload, ActiveSupport::Concern
Includes:
ActiveFedora::Rdf::NestedAttributes
Included in:
RDFDatastream, RdfList
Defined in:
lib/active_fedora/rdf_node.rb,
lib/active_fedora/rdf_node/term_proxy.rb

Defined Under Namespace

Modules: ClassMethods Classes: Builder, TermProxy

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.rdf_registryObject

Mapping from URI to ruby class



10
11
12
# File 'lib/active_fedora/rdf_node.rb', line 10

def self.rdf_registry
  @@rdf_registry ||= {}
end

Instance Method Details

#==(other_object) ⇒ Object

Comparison Operator Checks that

* RDF subject id (URI) is same
* Class is the same
* Both objects reference the same RDF graph in memory


19
20
21
22
23
# File 'lib/active_fedora/rdf_node.rb', line 19

def ==(other_object)
  self.class == other_object.class &&
  self.rdf_subject.id == other_object.rdf_subject.id &&
  self.graph.object_id == other_object.graph.object_id
end

#append(subject, predicate, args) ⇒ Object

append a value

Parameters:

  • predicate (Symbol, RDF::URI)

    the predicate to insert into the graph



154
155
156
157
# File 'lib/active_fedora/rdf_node.rb', line 154

def append(subject, predicate, args)
  options = config_for_term_or_uri(predicate)
  graph.insert([subject, predicate, args])
end

#attributes=(values) ⇒ Object

Parameters:

  • [Hash] (Hash)

    a customizable set of options

Raises:

  • (ArgumentError)


122
123
124
125
126
127
128
129
130
131
# File 'lib/active_fedora/rdf_node.rb', line 122

def attributes=(values)
  raise ArgumentError, "values must be a Hash, you provided #{values.class}" unless values.kind_of? Hash
  values.with_indifferent_access.each do |key, value|
    if self.class.config.keys.include?(key)
      set_value(rdf_subject, key, value)
    elsif nested_attributes_options.keys.map{ |k| "#{k}_attributes"}.include?(key)
      send("#{key}=".to_sym, value)
    end
  end
end

#config_for_term_or_uri(term) ⇒ Object



164
165
166
167
168
169
170
171
# File 'lib/active_fedora/rdf_node.rb', line 164

def config_for_term_or_uri(term)
  case term
  when RDF::URI
    self.class.config.each { |k, v| return v if v.predicate == term}
  else
    self.class.config[term.to_sym]
  end
end

#delete_predicate(subject, predicate, values = nil) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/active_fedora/rdf_node.rb', line 133

def delete_predicate(subject, predicate, values = nil)
  predicate = find_predicate(predicate) unless predicate.kind_of? RDF::URI

  if values.nil?
    query = RDF::Query.new do
      pattern [subject, predicate, :value]
    end

    query.execute(graph).each do |solution|
      graph.delete [subject, predicate, solution.value]
    end
  else
    Array(values).each do |v|
      graph.delete [subject, predicate, v]
    end
  end
  reset_child_cache!
end

#destroyObject

Be careful with destroy. It will still be in the cache untill you call reset()



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/active_fedora/rdf_node.rb', line 100

def destroy
  # delete any statements about this rdf_subject
  subject = rdf_subject
  query = RDF::Query.new do
    pattern [subject, :predicate, :value]
  end

  query.execute(graph).each do |solution|
    graph.delete [subject, solution.predicate, solution.value]
  end

  # delete any statements that reference this rdf_subject
  query = RDF::Query.new do
    pattern [:subject, :predicate, subject]
  end

  query.execute(graph).each do |solution|
    graph.delete [solution.subject, solution.predicate, subject]
  end
end

#find_predicate(term) ⇒ Object

Parameters:

  • term (Symbol, RDF::URI)

    predicate the predicate to insert into the graph



174
175
176
177
# File 'lib/active_fedora/rdf_node.rb', line 174

def find_predicate(term)
  conf = config_for_term_or_uri(term)
  conf ? conf.predicate : nil
end

#get_values(subject, term) ⇒ Object

Parameters:

  • subject (RDF::URI)

    the base node to start the search from

  • term (Symbol)

    the term to get the values for



45
46
47
48
49
# File 'lib/active_fedora/rdf_node.rb', line 45

def get_values(subject, term)
  options = config_for_term_or_uri(term)
  @target ||= {}
  @target[term.to_s] ||= TermProxy.new(self, subject, options)
end

#insert_child(predicate, node) ⇒ Object



159
160
161
# File 'lib/active_fedora/rdf_node.rb', line 159

def insert_child(predicate, node)
  graph.insert([rdf_subject, predicate, node.rdf_subject])
end

#mark_for_destructionObject



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

def mark_for_destruction
  @marked_for_destruction = true
end

#marked_for_destruction?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/active_fedora/rdf_node.rb', line 59

def marked_for_destruction?
  @marked_for_destruction
end

#new_record=(val) ⇒ Object



63
64
65
# File 'lib/active_fedora/rdf_node.rb', line 63

def new_record= val
  @new_record = val
end

#new_record?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/active_fedora/rdf_node.rb', line 67

def new_record?
  @new_record
end

#query(subject, predicate, &block) ⇒ Object



179
180
181
182
183
184
185
186
187
# File 'lib/active_fedora/rdf_node.rb', line 179

def query subject, predicate, &block
  predicate = find_predicate(predicate) unless predicate.kind_of? RDF::URI
  
  q = RDF::Query.new do
    pattern [subject, predicate, :value]
  end

  q.execute(graph, &block)
end

#rdf_subjectObject

Get the subject for this rdf object



27
28
29
30
31
32
33
# File 'lib/active_fedora/rdf_node.rb', line 27

def rdf_subject
  @subject ||= begin
    s = self.class.rdf_subject.call(self)
    s &&= RDF::URI.new(s) if s.is_a? String
    s
  end
end

#reset_child_cache!Object



39
40
41
# File 'lib/active_fedora/rdf_node.rb', line 39

def reset_child_cache!
  @target = {}
end

#reset_rdf_subject!Object



35
36
37
# File 'lib/active_fedora/rdf_node.rb', line 35

def reset_rdf_subject!
  @subject = nil
end

#set_value(subject, term, values) ⇒ Object

if there are any existing statements with this predicate, replace them

Parameters:

  • subject (RDF::URI)

    the subject to insert into the graph

  • term (Symbol, RDF::URI)

    the term to insert into the graph

  • values (Array, #to_s)

    the value/values to insert into the graph



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

def set_value(subject, term, values)
  options = config_for_term_or_uri(term)
  predicate = options.predicate
  values = Array(values)

  remove_existing_values(subject, predicate, values)

  values.each do |arg|
    if arg.respond_to?(:rdf_subject) # an RdfObject
      graph.insert([subject, predicate, arg.rdf_subject ])
    else
      arg = arg.to_s if arg.kind_of? RDF::Literal
      graph.insert([subject, predicate, arg])
    end
  end

  @target ||= {}
  proxy = @target[term.to_s]
  proxy ||= TermProxy.new(self, subject, options)
  proxy.reset!
  proxy

end

#target_class(conf) ⇒ Object



51
52
53
# File 'lib/active_fedora/rdf_node.rb', line 51

def target_class(conf)
  ActiveFedora.class_from_string(conf.class_name, self.class) if conf.class_name
end