Module: Arango::Document::InstanceMethods

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

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



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

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.



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

def attributes
  @attributes
end

#collectionObject

Returns the value of attribute collection.



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

def collection
  @collection
end

#databaseObject (readonly)

Returns the value of attribute database.



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

def database
  @database
end

#graphObject

Returns the value of attribute graph.



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

def graph
  @graph
end

#ignore_revsObject

Returns the value of attribute ignore_revs.



5
6
7
# File 'lib/arango/document/instance_methods.rb', line 5

def ignore_revs
  @ignore_revs
end

#serverObject (readonly)

Returns the value of attribute server.



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

def server
  @server
end

#wait_for_syncObject

Returns the value of attribute wait_for_sync.



5
6
7
# File 'lib/arango/document/instance_methods.rb', line 5

def wait_for_sync
  @wait_for_sync
end

Instance Method Details

#createObject



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

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



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

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



39
40
41
42
43
# File 'lib/arango/document/instance_methods.rb', line 39

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

#id=(i) ⇒ Object



45
46
47
# File 'lib/arango/document/instance_methods.rb', line 45

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

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



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

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



49
50
51
# File 'lib/arango/document/instance_methods.rb', line 49

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

#key=(k) ⇒ Object



53
54
55
# File 'lib/arango/document/instance_methods.rb', line 53

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

#reloadObject



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

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



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

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



57
58
59
# File 'lib/arango/document/instance_methods.rb', line 57

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

#revision=(r) ⇒ Object



61
62
63
# File 'lib/arango/document/instance_methods.rb', line 61

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

#same_revision?Boolean

is the same revision stored in the DB ?

Returns:

  • (Boolean)


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

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



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

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



65
66
67
# File 'lib/arango/document/instance_methods.rb', line 65

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

#vertex?Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/arango/document/instance_methods.rb', line 69

def vertex?
  !!@graph
end