Module: Arango::Edge::InstanceMethods

Defined in:
lib/arango/edge/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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/arango/edge/instance_methods.rb', line 70

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.



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

def attributes
  @attributes
end

#databaseObject (readonly)

Returns the value of attribute database.



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

def database
  @database
end

#edge_collectionObject

Returns the value of attribute edge_collection.



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

def edge_collection
  @edge_collection
end

#graphObject

Returns the value of attribute graph.



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

def graph
  @graph
end

#ignore_revsObject

Returns the value of attribute ignore_revs.



48
49
50
# File 'lib/arango/edge/instance_methods.rb', line 48

def ignore_revs
  @ignore_revs
end

#serverObject (readonly)

Returns the value of attribute server.



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

def server
  @server
end

#wait_for_syncObject

Returns the value of attribute wait_for_sync.



48
49
50
# File 'lib/arango/edge/instance_methods.rb', line 48

def wait_for_sync
  @wait_for_sync
end

Instance Method Details

#createObject



109
110
111
112
113
114
115
116
117
118
# File 'lib/arango/edge/instance_methods.rb', line 109

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

#deleteObject



153
154
155
156
157
158
159
160
# File 'lib/arango/edge/instance_methods.rb', line 153

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

#fromObject



162
163
164
# File 'lib/arango/edge/instance_methods.rb', line 162

def from
  @from_instance ||= _get_instance(from_id)
end

#from=(f) ⇒ Object



170
171
172
173
# File 'lib/arango/edge/instance_methods.rb', line 170

def from=(f)
  _set_from(f)
  from_id
end

#from_idObject



166
167
168
# File 'lib/arango/edge/instance_methods.rb', line 166

def from_id
  @changed_attributes[:_from] || @attributes[:_from]
end

#idObject



18
19
20
21
22
# File 'lib/arango/edge/instance_methods.rb', line 18

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

#id=(i) ⇒ Object



24
25
26
# File 'lib/arango/edge/instance_methods.rb', line 24

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

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



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

def initialize(key: nil, attributes: {}, from: nil, to: nil, edge_collection:, ignore_revs: false, wait_for_sync: false)
  @attributes = _attributes_from_arg(attributes)
  @attributes[:_key] = key if key
  #STDERR.puts ("Edge.new key #{key.inspect}, attributes #{attributes.inspect}, from #{from.inspect}, to #{to.inspect}")
  @changed_attributes = {}
  @ignore_revs = ignore_revs
  @wait_for_sync = wait_for_sync
  _set_from(from || from_id)
  _set_to(to || to_id)
  send(:edge_collection=, edge_collection)
  send(:graph=, edge_collection.graph) if edge_collection.graph
end

#keyObject



28
29
30
# File 'lib/arango/edge/instance_methods.rb', line 28

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

#key=(k) ⇒ Object



32
33
34
# File 'lib/arango/edge/instance_methods.rb', line 32

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

#reloadObject



88
89
90
91
92
93
94
95
96
# File 'lib/arango/edge/instance_methods.rb', line 88

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

#replaceObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/arango/edge/instance_methods.rb', line 120

def replace
  params = { returnNew: true, ignoreRevs: @ignore_revs }
  params[: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[:_from] = from_id
  attributes[:_to] = to_id
  @attributes = attributes
  @changed_attributes = {}
  headers = { "If-Match": @attributes[:_rev] } if !@ignore_revs && @attributes.key?(:_rev)
  args = { collection: @edge_collection.name, key: @attributes[:_key] }
  result = Arango::Requests::Document::Replace.execute(server: @server, args: args, params: params, headers: headers, body: @attributes)
  @attributes.merge!(result[:new])
  self
end

#revisionObject



36
37
38
# File 'lib/arango/edge/instance_methods.rb', line 36

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

#revision=(r) ⇒ Object



40
41
42
# File 'lib/arango/edge/instance_methods.rb', line 40

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

#same_revision?Boolean

Returns:

  • (Boolean)


98
99
100
101
102
103
104
105
106
107
# File 'lib/arango/edge/instance_methods.rb', line 98

def same_revision?
  headers = { "If-Match": @attributes[:_rev] }
  args = { collection: @edge_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



139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/arango/edge/instance_methods.rb', line 139

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

#toObject



175
176
177
# File 'lib/arango/edge/instance_methods.rb', line 175

def to
  @to_instance ||= _get_instance(to_id)
end

#to=(t) ⇒ Object



183
184
185
186
# File 'lib/arango/edge/instance_methods.rb', line 183

def to=(t)
  _set_to(t)
  to_id
end

#to_hObject



44
45
46
# File 'lib/arango/edge/instance_methods.rb', line 44

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

#to_idObject



179
180
181
# File 'lib/arango/edge/instance_methods.rb', line 179

def to_id
  @changed_attributes[:_to] || @attributes[:_to]
end