Class: Ldp::Orm

Inherits:
Object
  • Object
show all
Defined in:
lib/ldp/orm.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource) ⇒ Orm

Returns a new instance of Orm.



6
7
8
# File 'lib/ldp/orm.rb', line 6

def initialize resource
  @resource = resource
end

Instance Attribute Details

#last_responseObject (readonly)

Returns the value of attribute last_response.



4
5
6
# File 'lib/ldp/orm.rb', line 4

def last_response
  @last_response
end

#resourceObject (readonly)

Returns the value of attribute resource.



3
4
5
# File 'lib/ldp/orm.rb', line 3

def resource
  @resource
end

Instance Method Details

#createObject



46
47
48
49
50
51
52
# File 'lib/ldp/orm.rb', line 46

def create
  Ldp.instrument 'create.orm.ldp', subject: subject_uri do
    # resource.create returns a reloaded resource which causes any default URIs (e.g. "<>")
    # in the graph to be transformed to routable URIs
    Ldp::Orm.new resource.create
  end
end

#deleteObject



74
75
76
77
78
# File 'lib/ldp/orm.rb', line 74

def delete
  Ldp.instrument 'delete.orm.ldp', subject: subject_uri do
    resource.delete
  end
end

#graphObject



22
23
24
25
26
# File 'lib/ldp/orm.rb', line 22

def graph
  Ldp.instrument 'graph.orm.ldp', subject: subject_uri do
    resource.graph
  end
end

#new?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/ldp/orm.rb', line 14

def new?
  resource.new?
end

#persisted?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/ldp/orm.rb', line 18

def persisted?
  !new?
end

#query(*args, &block) ⇒ Object



34
35
36
37
38
# File 'lib/ldp/orm.rb', line 34

def query *args, &block
  Ldp.instrument 'query.orm.ldp', subject: subject_uri do
    graph.query *args, &block
  end
end

#reloadObject



40
41
42
43
44
# File 'lib/ldp/orm.rb', line 40

def reload
  Ldp.instrument 'reload.orm.ldp', subject: subject_uri do
    Ldp::Orm.new resource.reload
  end
end

#saveObject



54
55
56
57
58
59
60
61
62
# File 'lib/ldp/orm.rb', line 54

def save
  Ldp.instrument 'save.orm.ldp', subject: subject_uri do
    response = create_or_update

    response.success?
  end
rescue Ldp::HttpError
  false
end

#save!Object



64
65
66
67
68
69
70
71
72
# File 'lib/ldp/orm.rb', line 64

def save!
  result = create_or_update

  if result.is_a? RDF::Enumerable
    raise Ldp::GraphDifferenceException, 'Graph failed to persist', result
  end

  result
end

#subject_uriObject



10
11
12
# File 'lib/ldp/orm.rb', line 10

def subject_uri
  resource.subject_uri
end

#value(predicate) ⇒ Object



28
29
30
31
32
# File 'lib/ldp/orm.rb', line 28

def value predicate
  graph.query([subject_uri, predicate, nil]).map do |stmt|
    stmt.object
  end
end