Module: Arango::Document::InstanceMethods

Defined in:
lib/arango/document/instance_methods.rb

Overview

Arango Document InstanceMethods

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/arango/document/instance_methods.rb', line 75

def method_missing(name, *args, &block)
  name_s = name.to_s
  set_attr = false
  have_attr = false
  attribute_name_s = name_s.end_with?('=') ? (set_attr = true; name_s.chop) : name_s
  attribute_name_y = attribute_name_s.start_with?('attribute_') ? (have_attr = true; attribute_name_s[9..-1].to_sym) : attribute_name_s.to_sym
  if set_attr
    return @changed_attributes[attribute_name_y] = args[0]
  elsif @changed_attributes.key?(attribute_name_y)
    return @changed_attributes[attribute_name_y]
  elsif @attributes.key?(attribute_name_y)
    return @attributes[attribute_name_y]
  elsif have_attr
    return nil
  end
  super(name, *args, &block)
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



8
9
10
# File 'lib/arango/document/instance_methods.rb', line 8

def attributes
  @attributes
end

#collectionObject

Returns the value of attribute collection.



8
9
10
# File 'lib/arango/document/instance_methods.rb', line 8

def collection
  @collection
end

#databaseObject (readonly)

Returns the value of attribute database.



8
9
10
# File 'lib/arango/document/instance_methods.rb', line 8

def database
  @database
end

#graphObject

Returns the value of attribute graph.



8
9
10
# File 'lib/arango/document/instance_methods.rb', line 8

def graph
  @graph
end

#ignore_revsObject

Returns the value of attribute ignore_revs.



7
8
9
# File 'lib/arango/document/instance_methods.rb', line 7

def ignore_revs
  @ignore_revs
end

#serverObject (readonly)

Returns the value of attribute server.



8
9
10
# File 'lib/arango/document/instance_methods.rb', line 8

def server
  @server
end

#wait_for_syncObject

Returns the value of attribute wait_for_sync.



7
8
9
# File 'lib/arango/document/instance_methods.rb', line 7

def wait_for_sync
  @wait_for_sync
end

Instance Method Details

#createObject



93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/arango/document/instance_methods.rb', line 93

def create
  params = { returnNew: true }
  params[:waitForSync] = @wait_for_sync unless @wait_for_sync.nil?
  @attributes.merge!(@changed_attributes)
  @changed_attributes = {}
  result = if @graph
    Arango::Requests::Graph::CreateVertex.execute(server: @server, args: @attributes, params: params)
  else
    args = { collection: @collection.name }
    Arango::Requests::Document::Create.execute(server: @server, args: args, body: @attributes, params: params)
  end
  @attributes.merge!(result.new)
  self
end

#deleteObject



180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/arango/document/instance_methods.rb', line 180

def delete
  headers = { }
  if @graph
    headers[:"If-Match"] = @attributes[:_rev] if if_match
    args = { graph: @graph.name, collection: @collection.name, vertex: @attributes[:_key] }
    Arango::Requests::Graph::Delete.execute(server: @server, args: args, headers: headers)
  else
    query = { waitForSync: @wait_for_sync }
    headers[:"If-Match"] = @attributes[:_rev] if !@ignore_revs && @attributes.key?(:_rev)
    args = { collection: @collection.name, key: @attributes[:_key] }
    Arango::Requests::Document::Delete.execute(server: @server, args: args, headers: headers)
  end
  nil
end

#idObject



41
42
43
44
45
# File 'lib/arango/document/instance_methods.rb', line 41

def id
  i = @changed_attributes[:_id] || @attributes[:_id]
  return i if i
  "#{collection.name}/#{key}"
end

#id=(i) ⇒ Object



47
48
49
# File 'lib/arango/document/instance_methods.rb', line 47

def id=(i)
  @changed_attributes[:_id] = i
end

#initialize(key: nil, attributes: {}, collection:, ignore_revs: false, wait_for_sync: nil) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/arango/document/instance_methods.rb', line 10

def initialize(key: nil, attributes: {}, collection:, ignore_revs: false, wait_for_sync: nil)
  @attributes = _attributes_from_arg(attributes)
  @attributes[:_key] = key if key
  @changed_attributes = {}
  @ignore_revs = ignore_revs
  @wait_for_sync = wait_for_sync
  send(:collection=, collection)
  send(:graph=, collection.graph) if collection.graph
end

#keyObject



51
52
53
# File 'lib/arango/document/instance_methods.rb', line 51

def key
  @changed_attributes[:_key] || @attributes[:_key]
end

#key=(k) ⇒ Object



55
56
57
# File 'lib/arango/document/instance_methods.rb', line 55

def key=(k)
  @changed_attributes[:_key] = k
end

#reloadObject



108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/arango/document/instance_methods.rb', line 108

def reload
  headers = {}
  result = if @graph
    headers[:"If-Match"] = @attributes[:_rev] if if_match
    args = { collection: @collection.name, vertex: @attributes[:_key] }
    Arango::Requests::Graph::GetVertex.execute(server: @server, headers: headers, args: args)
  else
    headers = { "If-Match": @attributes[:_rev] } if !@ignore_revs && @attributes.key?(:_rev)
    args = { collection: @collection.name, key: @attributes[:_key] }
    Arango::Requests::Document::Get.execute(server: @server, headers: headers, args: args)
  end
  @attributes = _attributes_from_arg(result)
  @changed_attributes = {}
end

#replaceObject



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/arango/document/instance_methods.rb', line 135

def replace
  headers = {}
  result = if @graph
    headers[:"If-Match"] = @attributes[:_rev] if if_match
    args = { graph: @graph.name, collection: @collection.name, vertex: @attributes[:_key] }
    Arango::Requests::Graph::UpdateVertex.execute(server: @server, args: args, headers: headers)
  else
    query = { returnNew: true, ignoreRevs: @ignore_revs }
    query[:waitForSync] = @wait_for_sync unless @wait_for_sync.nil?
    headers = nil
    attributes = @changed_attributes
    attributes[:_id] = @attributes[:_id]
    attributes[:_key] = @attributes[:_key]
    attributes[:_rev] = @attributes[:_rev]
    @attributes = attributes
    @changed_attributes = {}
    headers = { "If-Match": @attributes[:_rev] } if !@ignore_revs && @attributes.key?(:_rev)
    args = { collection: @collection.name, key: @attributes[:_key] }
    Arango::Requests::Document::Update.execute(server: @server, args: args, headers: headers, body: @attributes)
  end
  @attributes.merge!(@attributes)
  self
end

#revisionObject



59
60
61
# File 'lib/arango/document/instance_methods.rb', line 59

def revision
  @changed_attributes[:_rev] || @attributes[:_rev]
end

#revision=(r) ⇒ Object



63
64
65
# File 'lib/arango/document/instance_methods.rb', line 63

def revision=(r)
  @changed_attributes[:_rev] = r
end

#same_revision?Boolean

is the same revision stored in the DB ?

Returns:

  • (Boolean)


124
125
126
127
128
129
130
131
132
133
# File 'lib/arango/document/instance_methods.rb', line 124

def same_revision?
  headers = { "If-Match": @attributes[:_rev] }
  args = { collection: @collection.name, key: @attributes[:_key] }
  begin
    Arango::Requests::Document::Head.execute(server: @server, headers: headers, args: args)
  rescue Error => e
    return false
  end
  true
end

#saveObject Also known as: update



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/arango/document/instance_methods.rb', line 159

def save
  headers = { }
  result = if @graph
    headers[:"If-Match"] = @attributes[:_rev] if if_match
    args = { graph: @graph.name, collection: @collection.name, vertex: @attributes[:_key] }
    Arango::Requests::Graph::UpdateVertex.execute(server: @server, args: args, headers: headers)
  else
    query = { returnNew: true, ignoreRevs: @ignore_revs }
    query[:waitForSync] = @wait_for_sync unless @wait_for_sync.nil?
    headers[:"If-Match"] =  @attributes[:_rev] if !@ignore_revs && @attributes.key?(:_rev)
    changed_attributes = @changed_attributes
    @changed_attributes = {}
    args = { collection: @collection.name, key: @attributes[:_key] }
    rev = Arango::Requests::Document::Update.execute(server: @server, args: args, headers: headers, body: changed_attributes)[:_rev]
    changed_attributes.merge({ _rev: rev })
  end
  @attributes.merge!(result)
  self
end

#to_hObject



67
68
69
# File 'lib/arango/document/instance_methods.rb', line 67

def to_h
  @attributes.delete_if{|_,v| v.nil?}
end

#vertex?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/arango/document/instance_methods.rb', line 71

def vertex?
  !!@graph
end