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.



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

def initialize resource
  @resource = resource
end

Instance Attribute Details

#last_responseObject (readonly)

Returns the value of attribute last_response.



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

def last_response
  @last_response
end

#resourceObject (readonly)

Returns the value of attribute resource.



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

def resource
  @resource
end

Instance Method Details

#createObject



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

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



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

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

#graphObject



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

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

#new?Boolean

Returns:

  • (Boolean)


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

def new?
  resource.new?
end

#persisted?Boolean

Returns:

  • (Boolean)


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

def persisted?
  !new?
end

#query(*args, &block) ⇒ Object



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

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

#reloadObject



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

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

#saveObject



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

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



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

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



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

def subject_uri
  resource.subject_uri
end

#value(predicate) ⇒ Object



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

def value predicate
  graph.query(:subject => subject_uri, :predicate => predicate).map do |stmt|
    stmt.object
  end
end