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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



84
85
86
# File 'lib/ldp/orm.rb', line 84

def method_missing meth, *args, &block
  super
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



78
79
80
81
82
# File 'lib/ldp/orm.rb', line 78

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

#respond_to?(meth) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/ldp/orm.rb', line 88

def respond_to?(meth)
  super
end

#saveObject



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

def save
  Ldp.instrument 'save.orm.ldp', subject: subject_uri do
    @last_response = resource.save
    @last_response.success?
  end
rescue Ldp::HttpError => e
  @last_response = e
  logger.debug e
  false
end

#save!Object



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

def save!
  result = save

  if result.is_a? RDF::Graph
    raise GraphDifferenceException.new "", result
  elsif !result
    raise SaveException.new @last_response
  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