Class: RDFObject::ChangeSet

Inherits:
BlankNode
  • Object
show all
Defined in:
lib/rdf_objects/changeset/changeset.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource, versioned = false) ⇒ ChangeSet

Returns a new instance of ChangeSet.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/rdf_objects/changeset/changeset.rb', line 5

def initialize(resource, versioned=false)
  @resource = resource
  @versioned = versioned
  @additions = []
  @removals = []
  super(UUID.generate)

  assert("[cs:createdDate]", Time.now)
  relate("[rdf:type]", "[cs:ChangeSet]")
  relate("[cs:subjectOfChange]", @resource.uri)      
end

Instance Attribute Details

#additionsObject (readonly)

Returns the value of attribute additions.



4
5
6
# File 'lib/rdf_objects/changeset/changeset.rb', line 4

def additions
  @additions
end

#removalsObject (readonly)

Returns the value of attribute removals.



4
5
6
# File 'lib/rdf_objects/changeset/changeset.rb', line 4

def removals
  @removals
end

#resourceObject (readonly)

Returns the value of attribute resource.



4
5
6
# File 'lib/rdf_objects/changeset/changeset.rb', line 4

def resource
  @resource
end

Instance Method Details

#add_statement(predicate, object) ⇒ Object



36
37
38
# File 'lib/rdf_objects/changeset/changeset.rb', line 36

def add_statement(predicate, object)
  @additions << create_statement(predicate, object)
end

#clear_predicate(uri) ⇒ Object



61
62
63
64
65
66
# File 'lib/rdf_objects/changeset/changeset.rb', line 61

def clear_predicate(uri) 
  [*@resource[uri]].each do |object|
    next unless object
    @removals << create_statement(uri, object)
  end
end

#create_statement(predicate, object) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/rdf_objects/changeset/changeset.rb', line 68

def create_statement(predicate, object)
  stmt = BlankNode.new(UUID.generate)
  stmt.relate("[rdf:type]", "[rdf:Statement]")
  stmt.relate("[rdf:subject]", @resource.uri)
  stmt.relate("[rdf:predicate]", predicate)      
  stmt.assert("[rdf:object]", object)
  stmt      
end

#creator_name=(name) ⇒ Object



22
23
24
25
# File 'lib/rdf_objects/changeset/changeset.rb', line 22

def creator_name=(name)
  self.cs['creatorName'] = nil if self.cs['creatorName']      
  assert("[cs:creatorName]", name)
end

#delete_allObject



27
28
29
30
31
32
33
34
# File 'lib/rdf_objects/changeset/changeset.rb', line 27

def delete_all()
  @resource.assertions.each_pair do |predicate, objects|
    [*objects].each do |object|
      next unless object
      @removals <<  create_statement(predicate, object)
    end
  end 
end

#rdf_description_block(depth = 0) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/rdf_objects/changeset/changeset.rb', line 77

def rdf_description_block(depth=0)
  rdf = "<rdf:Description #{xml_subject_attribute}>"
  namespaces = {}
  Curie.get_mappings.each_pair do |key, value|
    if self.respond_to?(key.to_sym)
      self.send(key.to_sym).each_pair do | predicate, objects |
        [*objects].each do | object |
          rdf << "<#{key}:#{predicate}"
          namespaces["xmlns:#{key}"] = "#{Curie.parse("[#{key}:]")}"
          if object.is_a?(RDFObject::ResourceReference) || object.is_a?(RDFObject::BlankNode)
            if depth == 0
              rdf << " #{object.xml_object_attribute} />"
            else
              rdf << ">"                  
              ns, rdf_data = object.rdf_description_block(depth-1)
              namespaces.merge!(ns)
              rdf << rdf_data
              rdf << "</#{key}:#{predicate}>"
            end
          else
            object = RDF::Literal.new(object) unless object.is_a?(RDF::Literal)
            if object.language
              rdf << " xml:lang=\"#{object.language}\""
            end
            if object.datatype
              rdf << " rdf:datatype=\"#{object.datatype}\""
            end
            rdf << ">#{CGI.escapeHTML(object.value)}</#{key}:#{predicate}>"
          end
        end
      end
    end
    if value == "http://purl.org/vocab/changeset/schema#"
      @removals.each do |removal|
        rdf << "<#{key}:removal>"
        ns, rdf_data = removal.rdf_description_block
        namespaces.merge!(ns)
        rdf << rdf_data            
        rdf << "</#{key}:removal>"
      end
      @additions.each do |addition|
        rdf << "<#{key}:addition>"
        ns, rdf_data = addition.rdf_description_block
        namespaces.merge!(ns)
        rdf << rdf_data
        rdf << "</#{key}:addition>"
      end                      
    end
  end
  
  rdf << "</rdf:Description>"
  [namespaces, rdf]
end

#reason=(reason_note) ⇒ Object



17
18
19
20
# File 'lib/rdf_objects/changeset/changeset.rb', line 17

def reason=(reason_note)
  self.cs['changeReason'] = nil if self.cs['changeReason']
  assert("[cs:changeReason]", reason_note)
end

#remove_statement(predicate, object) ⇒ Object



40
41
42
# File 'lib/rdf_objects/changeset/changeset.rb', line 40

def remove_statement(predicate, object)
  @removals << create_statement(predicate, object)
end

#replace_statements(from, to = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rdf_objects/changeset/changeset.rb', line 44

def replace_statements(from, to=nil)
  from.each_pair do |pred, objects|
    [*objects].each do |o|
      next unless o
      @removals << create_statement(pred, o)
    end
  end
  if to and to.is_a?(Hash) and !to.empty?
    to.each_pair do |pred, objects|
      [*objects].each do |o|
        next unless o
        @additions << create_statement(pred, o)
      end
    end
  end
end