Module: RDF::RDFObjects::Resource

Included in:
Node, URI
Defined in:
lib/rdf/rdfobjects/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



206
207
208
209
210
# File 'lib/rdf/rdfobjects/resource.rb', line 206

def method_missing(meth, *args)
  if RDF.const_defined?(meth) && RDF.const_get(meth).ancestors.index(RDF::Vocabulary)
    send(:"[]", meth)
  end
end

Instance Attribute Details

#graphObject

Returns the value of attribute graph.



3
4
5
# File 'lib/rdf/rdfobjects/resource.rb', line 3

def graph
  @graph
end

Instance Method Details

#[](p) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rdf/rdfobjects/resource.rb', line 21

def [](p)
  p = RDF::Resource.new(p) if p.is_a?(String)
  results = {}
  if p.is_a?(RDF::Resource)
    if p == RDF.to_rdf
      regex = Regexp.new(p.to_s)
      if assertions
        assertions.each_pair do |pred, objects|
          results[pred.to_s] = objects if pred.to_s =~ /^#{regex}/
        end 
      end
      results.extend(AssertionSet)
      results.vocabulary = p
      results.subject = self
    else          
      @graph.query(:subject=>self, :predicate=>p).each do |r|
        if r.object.is_a?(RDF::Resource)
          unless r.object.frozen?
            r.object.extend(RDF::RDFObjects::Resource)
            r.object.graph = self.graph
          end
        end
        results[r.predicate] ||=[]
        results[r.predicate] << r.object
      end   
    end               
  elsif p.is_a?(Class) && p.inspect =~ /^RDF::Vocabulary\(/
    regex = Regexp.new(p.to_s)
    if assertions
      assertions.each_pair do |pred, objects|
        results[pred.to_s] = objects if pred.to_s =~ /^#{regex}/
      end 
    end
    results.extend(AssertionSet)
    results.vocabulary = p
    results.subject = self
    
  elsif (RDF.const_defined?(p) && RDF.const_get(p).ancestors.index(RDF::Vocabulary))
    regex = Regexp.new(RDF.const_get(p).to_s)
    if assertions
      assertions.each_pair do |pred, objects|
        results[pred.to_s] = objects if pred.to_s =~ /^#{regex}/
      end 
    end
    results.extend(AssertionSet)
    results.vocabulary = RDF.const_get(p)         
    results.subject = self
  end
  
  if results.is_a?(AssertionSet)
    results.each_pair do |pred, objs|
      objs.extend(ObjectSet)
      objs.set_statement(RDF::Statement.new(self, pred, ""))
    end        
    obj_set = results
  else
    obj_set = case results.keys.length
    when 0
      set = []
      set.extend(ObjectSet)
      set.set_statement(RDF::Statement.new(self,p,""))
      set
    when 1
      set = []
      results.values.first.each do |v|
        set << v
      end
      set.extend(ObjectSet)
      set.set_statement(RDF::Statement.new(self,p,""))
      set
    end
  end
  obj_set
end

#[]=(p, o) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/rdf/rdfobjects/resource.rb', line 96

def []=(p,o)      
  p = RDF::URI.intern(p) unless p.is_a?(RDF::URI)
  @graph.query(:subject=>self, :predicate=>p).each {|stmt| @graph.delete(stmt)}    
  [*o].each do |obj|
    next unless obj
    obj = cast_as_typed_object(obj)        
    if obj.is_a?(RDF::Resource) && !obj.frozen?
      obj.graph = @graph
    elsif obj.is_a?(RDF::Resource) && obj.frozen? && obj.graph != @graph
      obj = RDF::URI.new(obj)
      obj.graph = @graph
      obj.freeze
    end
    @graph << [self, p, obj] unless obj.nil?
  end
  self[p]
end

#assert(p, o) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/rdf/rdfobjects/resource.rb', line 8

def assert(p, o)
  p = RDF::URI.intern(p) unless p.is_a?(RDF::URI)
  unless o.is_a?(RDF::Resource) || o.is_a?(RDF::Literal)
    o = RDF::Literal.new(o)
  end
  @graph << [self, p, o]
end

#cast_as_typed_object(o) ⇒ Object



140
141
142
143
# File 'lib/rdf/rdfobjects/resource.rb', line 140

def cast_as_typed_object(o)
  return o if o.is_a?(RDF::Literal) || o.is_a?(RDF::Resource)
  return Literal.new(o)
end

#clearObject



202
203
204
# File 'lib/rdf/rdfobjects/resource.rb', line 202

def clear
  @graph.query(:subject=>self).each {|stmt| @graph.delete(stmt)}
end

#object_ofObject



157
158
159
160
161
162
163
164
165
166
# File 'lib/rdf/rdfobjects/resource.rb', line 157

def object_of
  subjects = []
  @graph.query(:object=>self).each do |stmt|
    s = stmt.subject
    s.extend(RDF::RDFObjects::Resource)
    s.graph = @graph
    subjects << s
  end
  subjects
end

#predicatesObject



145
146
147
148
149
# File 'lib/rdf/rdfobjects/resource.rb', line 145

def predicates
  p = []
  @graph.query(:subject=>self).each {|stmt| p << stmt.predicate unless p.include?(stmt.predicate)}
  p
end

#propertiesObject Also known as: assertions



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/rdf/rdfobjects/resource.rb', line 168

def properties
  results = {}
  @graph.query(:subject=>self).each do |r| 
    if r.object.is_a?(RDF::Resource)
      r.object.graph = self.graph unless r.object.frozen?
    end
    results[r.predicate] ||=[]
    results[r.predicate] << r.object
  end
  
  obj_set = case results.keys.length
  when 0 then nil
  else
    results.each_pair do |pred, objs|
      #objs.each do |o|
        objs.extend(ObjectSet)
        objs.set_statement(RDF::Statement.new(self, pred, ""))
      #end
      #results[pred] = objs.first if objs.length == 1
      results
    end
  end
  obj_set      
end

#push(p, o) ⇒ Object



134
135
136
137
138
# File 'lib/rdf/rdfobjects/resource.rb', line 134

def push(p, o)
  p = RDF::URI.intern(p) unless p.is_a?(RDF::URI)
  o = cast_as_typed_object(o)
  @graph << [self, p, o]
end

#relate(p, o) ⇒ Object



16
17
18
19
# File 'lib/rdf/rdfobjects/resource.rb', line 16

def relate(p, o)
  o = RDF::Resource.new(o) unless o.is_a?(RDF::Resource)
  assert(p, o)
end

#remove(p, o = nil) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/rdf/rdfobjects/resource.rb', line 114

def remove(p, o=nil)
  p = RDF::URI.intern(p) unless p.is_a?(RDF::URI)
  removals = []
  if o
    o = cast_as_typed_object(o)
    query = {:subject=>self, :predicate=>p, :object=>o}
  else
    query = {:subject=>self, :predicate=>p}
  end
  @graph.query(query).each do |stmt| 
    removals << stmt
    @graph.delete(stmt)
  end      
  if o
    removals.first
  else
    removals
  end
end

#statementsObject



151
152
153
154
155
# File 'lib/rdf/rdfobjects/resource.rb', line 151

def statements
  stmts = []
  @graph.query(:subject=>self, :predicate=>p).each {|stmt| stmts << stmt}   
  stmts
end

#to_ntriplesObject



212
213
214
215
216
217
218
# File 'lib/rdf/rdfobjects/resource.rb', line 212

def to_ntriples
  RDF::Writer.for(:ntriples).buffer do |writer|
    @graph.query(:subject=>self).each_statement do |statement|
      writer << statement
    end
  end      
end

#typeObject



198
199
200
# File 'lib/rdf/rdfobjects/resource.rb', line 198

def type
  self[RDF.type]
end

#type=(rdf_type) ⇒ Object



194
195
196
# File 'lib/rdf/rdfobjects/resource.rb', line 194

def type=(rdf_type)
  self[RDF.type] = rdf_type
end