Class: Ldp::Resource::RdfSource

Inherits:
Ldp::Resource show all
Defined in:
lib/ldp/resource/rdf_source.rb

Direct Known Subclasses

Container

Instance Attribute Summary

Attributes inherited from Ldp::Resource

#client, #subject

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Ldp::Resource

#current?, #delete, #get, #head, #new?, #reload, #retrieved_content?, #save, #subject_uri, #update

Constructor Details

#initialize(client, subject, graph_or_response = nil) ⇒ RdfSource



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/ldp/resource/rdf_source.rb', line 4

def initialize client, subject, graph_or_response = nil
  super client, subject, graph_or_response

  case graph_or_response
    when RDF::Graph
      @graph = graph_or_response
    when Ldp::Response
    when NilClass
      #nop
    else
      raise ArgumentError, "Third argument to #{self.class}.new should be a RDF::Graph or a Ldp::Response. You provided #{graph_or_response.class}"
  end
end

Class Method Details

.check_for_differences_and_reload_resource(old_object) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/ldp/resource/rdf_source.rb', line 56

def self.check_for_differences_and_reload_resource old_object
  new_object = old_object.reload

  bijection = new_object.graph.bijection_to(old_object.graph)
  diff = RDF::Graph.new

  old_object.graph.each do |statement|
    if statement.has_blank_nodes?
      subject = bijection.fetch(statement.subject, false) if statement.subject.node?
      object = bijection.fetch(statement.object, false) if statement.object.node?
      bijection_statement = RDF::Statement.new :subject => subject || statemnet.subject, :predicate => statement.predicate, :object => object || statement.object

      diff << statement if subject === false or object === false or new_object.graph.has_statement?(bijection_statement)
    elsif !new_object.graph.has_statement? statement
      diff << statement
    end
  end

  diff
end

Instance Method Details

#check_for_differences_and_reloadObject



52
53
54
# File 'lib/ldp/resource/rdf_source.rb', line 52

def check_for_differences_and_reload
  self.class.check_for_differences_and_reload_resource self
end

#contentObject



24
25
26
# File 'lib/ldp/resource/rdf_source.rb', line 24

def content
  graph.dump(:ttl) if graph
end

#createObject



18
19
20
21
22
# File 'lib/ldp/resource/rdf_source.rb', line 18

def create
  super do |req|
    req.headers = { "Content-Type" => "text/turtle" }
  end
end

#graphObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ldp/resource/rdf_source.rb', line 28

def graph
  @graph ||= RDF::Graph.new if new?
  @graph ||= begin
    original_graph = get.graph

    inlinedResources = get.graph.query(:predicate => Ldp.contains).map { |x| x.object }

    # we want to scope this graph to just statements about this model, not contained relations
    unless inlinedResources.empty?
      new_graph = RDF::Graph.new

      original_graph.each_statement do |s|
        unless inlinedResources.include? s.subject
          new_graph << s
        end
      end

      new_graph
    else
      original_graph
    end
  end
end