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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
# File 'lib/active_fedora/rdf_node.rb', line 188

def method_missing(name, *args)
  if md = /^([^=]+)=$/.match(name.to_s)
      set_value(rdf_subject, md[1], *args)  
  elsif find_predicate(name)
      get_values(rdf_subject, name)
  else 
    super
  end
rescue ActiveFedora::UnregisteredPredicateError
  super
end

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



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

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)


126
127
128
129
130
131
132
133
134
135
# File 'lib/active_fedora/rdf_node.rb', line 126

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



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

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



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/active_fedora/rdf_node.rb', line 137

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



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/active_fedora/rdf_node.rb', line 104

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



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

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
50
# File 'lib/active_fedora/rdf_node.rb', line 45

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

#mark_for_destructionObject



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

def mark_for_destruction
  @marked_for_destruction = true
end

#marked_for_destruction?Boolean

Returns:

  • (Boolean)


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

def marked_for_destruction?
  @marked_for_destruction
end

#new_record=(val) ⇒ Object



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

def new_record= val
  @new_record = val
end

#new_record?Boolean

Returns:

  • (Boolean)


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

def new_record?
  @new_record
end

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



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

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

  • predicate (Symbol, RDF::URI)

    the predicate to insert into the graph

  • values (Array, #to_s)

    the value/values to insert into the graph



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.rb', line 79

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, predicate, options)
  proxy.reset!
  proxy

end

#target_class(predicate) ⇒ Object



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

def target_class(predicate)
  _, conf = self.class.config_for_predicate(predicate)
  class_name = conf[:class_name]
  return nil unless class_name
  ActiveFedora.class_from_string(class_name, self.class)
end