Class: Tarantool::LightRecord

Inherits:
BaseRecord show all
Defined in:
lib/tarantool/light_record.rb

Instance Attribute Summary

Attributes included from BaseRecord::InstanceMethods

#__new_record, #attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClassAttribute

#t_class_attribute

Methods included from BaseRecord::ClassMethods

#_tail, #all, #auto_space, #by_pk, #by_pks, #call, #create, #delete, #field, #find, #first, #from_fetched, #index, #insert, #invoke, #primary_index, #replace, #reset_space!, #select, #shard_fields, #shard_proc, #space, #space_no, #space_no=, #store, #tarantool, #tarantool=, #update

Methods included from BaseRecord::InstanceMethods

#==, #_raise_doesnt_exists, #_tail, #_tail=, #auto_space, #id, #increment, #new_record!, #old_record!, #reload, #set_attributes, #space, #update, #update_attributes

Constructor Details

#initialize(attributes = nil) ⇒ LightRecord

Returns a new instance of LightRecord.



5
6
7
8
9
10
# File 'lib/tarantool/light_record.rb', line 5

def initialize(attributes = nil)
  @__new_record = true
  @attributes = self.class.default_values.dup
  set_attributes(attributes)  if attributes
  after_init
end

Class Method Details

.create(attrs) ⇒ Object



76
77
78
# File 'lib/tarantool/light_record.rb', line 76

def create(attrs)
  new(attrs).save
end

.define_field_accessor(name, type) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/tarantool/light_record.rb', line 64

def define_field_accessor(name, type)
  generated_attribute_methods.class_eval <<-"EOF", __FILE__, __LINE__ - 1
    def #{name}
      @attributes[:"#{name}"]
    end

    def #{name}=(v)
      @attributes[:"#{name}"] = v
    end
  EOF
end

.from_fetched(attributes) ⇒ Object



80
81
82
# File 'lib/tarantool/light_record.rb', line 80

def from_fetched(attributes)
  attributes && allocate.__fetched(attributes)
end

.generated_attribute_methodsObject



57
58
59
60
61
62
# File 'lib/tarantool/light_record.rb', line 57

def generated_attribute_methods
  @generated_attribute_methods ||= begin
      include (mod = Module.new)
      mod
    end
end

Instance Method Details

#__fetched(attributes) ⇒ Object



12
13
14
15
16
17
# File 'lib/tarantool/light_record.rb', line 12

def __fetched(attributes)
  @__new_record = false
  @attributes = attributes
  after_init
  self
end

#after_createObject



50
# File 'lib/tarantool/light_record.rb', line 50

def after_create; end

#after_destroyObject



54
# File 'lib/tarantool/light_record.rb', line 54

def after_destroy; end

#after_initObject

callback which runs both after initialization and after fetching from database



21
22
# File 'lib/tarantool/light_record.rb', line 21

def after_init
end

#after_saveObject



52
# File 'lib/tarantool/light_record.rb', line 52

def after_save; end

#after_updateObject



51
# File 'lib/tarantool/light_record.rb', line 51

def after_update; end

#before_createObject



48
# File 'lib/tarantool/light_record.rb', line 48

def before_create; true end

#before_destroyObject



53
# File 'lib/tarantool/light_record.rb', line 53

def before_destroy; true end

#before_saveObject



47
# File 'lib/tarantool/light_record.rb', line 47

def before_save; true end

#before_updateObject



49
# File 'lib/tarantool/light_record.rb', line 49

def before_update; true end

#destroyObject



40
41
42
43
44
45
# File 'lib/tarantool/light_record.rb', line 40

def destroy
  return false  unless before_destroy
  self.class.delete id
  after_destroy
  true
end

#saveObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/tarantool/light_record.rb', line 24

def save
  return false  unless before_save
  if @__new_record
    return false  unless before_create
    self.class.insert(@attributes)
    @__new_record = false
    after_create
  else
    return false  unless before_update
    self.class.replace(@attributes)
    after_update
  end
  after_save
  self
end