Module: Tarantool::BaseRecord::InstanceMethods

Included in:
Tarantool::BaseRecord
Defined in:
lib/tarantool/base_record.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#__new_recordObject Also known as: new_record

Returns the value of attribute __new_record.



288
289
290
# File 'lib/tarantool/base_record.rb', line 288

def __new_record
  @__new_record
end

#attributesObject (readonly)

Returns the value of attribute attributes.



289
290
291
# File 'lib/tarantool/base_record.rb', line 289

def attributes
  @attributes
end

Instance Method Details

#==(other) ⇒ Object



346
347
348
# File 'lib/tarantool/base_record.rb', line 346

def ==(other)
  equal?(other) || (other.class == self.class && id == other.id)
end

#_raise_doesnt_exists(action = "update") ⇒ Object

Raises:



368
369
370
# File 'lib/tarantool/base_record.rb', line 368

def _raise_doesnt_exists(action = "update")
  raise TupleDoesntExists.new(0x3102, "Record which you wish to #{action}, doesn't exists")
end

#_tailObject



309
310
311
# File 'lib/tarantool/base_record.rb', line 309

def _tail
  @attributes[:_tail]
end

#_tail=(v) ⇒ Object



313
314
315
# File 'lib/tarantool/base_record.rb', line 313

def _tail=(v)
  @attributes[:_tail] = v
end

#auto_spaceObject



332
333
334
# File 'lib/tarantool/base_record.rb', line 332

def auto_space
  self.class.space
end

#idObject



322
323
324
325
326
# File 'lib/tarantool/base_record.rb', line 322

def id
  (primary = self.class.primary_index).size == 1 ?
    @attributes[primary[0]] :
    @attributes.values_at(*primary)
end

#increment(field, by = 1) ⇒ Object



364
365
366
# File 'lib/tarantool/base_record.rb', line 364

def increment(field, by = 1)
  update([[field.to_sym, :+, by]])
end

#new_record!Object



293
294
295
296
# File 'lib/tarantool/base_record.rb', line 293

def new_record!
  @__new_record = true
  self
end

#old_record!Object



298
299
300
301
# File 'lib/tarantool/base_record.rb', line 298

def old_record!
  @__new_record = false
  self
end

#reloadObject



336
337
338
339
340
341
342
343
344
# File 'lib/tarantool/base_record.rb', line 336

def reload
  if hash = space.by_pk(id)
    @__new_record = false
    @attributes = hash
    self
  else
    _raise_doesnt_exists("reload")
  end
end

#set_attributes(attributes) ⇒ Object



303
304
305
306
307
# File 'lib/tarantool/base_record.rb', line 303

def set_attributes(attributes)
  attributes.each do |k, v|
    send("#{k}=", v)
  end      
end

#spaceObject



328
329
330
# File 'lib/tarantool/base_record.rb', line 328

def space
  self.class.space
end

#update(ops) ⇒ Object

update record in db first, reload it then

record.update({:state => 'sleep', :sleep_count => [:+, 1]})
record.update([[:state, 'sleep'], [:sleep_count, :+, 1]])

Raises:



354
355
356
357
358
359
360
361
362
# File 'lib/tarantool/base_record.rb', line 354

def update(ops)
  raise UpdateNewRecord, "Could not call update on new record"  if @__new_record
  unless new_attrs = space.update(id, ops, return_tuple: true)
    _raise_doesnt_exists
  end
  @attributes = new_attrs

  self
end

#update_attributes(attributes) ⇒ Object



317
318
319
320
# File 'lib/tarantool/base_record.rb', line 317

def update_attributes(attributes)
  set_attributes(attributes)
  save
end