Module: MassiveRecord::ORM::Persistence

Extended by:
ActiveSupport::Concern
Defined in:
lib/massive_record/orm/persistence.rb,
lib/massive_record/orm/persistence/operations.rb,
lib/massive_record/orm/persistence/operations/insert.rb,
lib/massive_record/orm/persistence/operations/reload.rb,
lib/massive_record/orm/persistence/operations/update.rb,
lib/massive_record/orm/persistence/operations/destroy.rb,
lib/massive_record/orm/persistence/operations/suppress.rb,
lib/massive_record/orm/persistence/operations/embedded/insert.rb,
lib/massive_record/orm/persistence/operations/embedded/reload.rb,
lib/massive_record/orm/persistence/operations/embedded/update.rb,
lib/massive_record/orm/persistence/operations/atomic_operation.rb,
lib/massive_record/orm/persistence/operations/embedded/destroy.rb,
lib/massive_record/orm/persistence/operations/table_operation_helpers.rb,
lib/massive_record/orm/persistence/operations/embedded/operation_helpers.rb

Defined Under Namespace

Modules: ClassMethods, Operations

Instance Method Summary collapse

Instance Method Details

#atomic_decrement!(attr_name, by = 1) ⇒ Object



124
125
126
# File 'lib/massive_record/orm/persistence.rb', line 124

def atomic_decrement!(attr_name, by = 1)
  atomic_operation(:decrement, attr_name, by)
end

#atomic_increment!(attr_name, by = 1) ⇒ Object



108
109
110
# File 'lib/massive_record/orm/persistence.rb', line 108

def atomic_increment!(attr_name, by = 1)
  atomic_operation(:increment, attr_name, by)
end

#change_id!(new_id) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/massive_record/orm/persistence.rb', line 75

def change_id!(new_id)
  old_id, self.id = id, new_id

  @new_record = true
  unless save
    raise <<-TXT
      Unable to save #{self.class} with updated id '#{new_id}'.
      Old id '#{old_id}' was not deleted so in theory nothing should be changed in the database.
    TXT
  end

  unless self.class.find(old_id).destroy
    raise <<-TXT
      Unable to destroy #{self.class} with id '#{old_id}'.
      You now how duplicate records in the database. New id is: '#{new_id}.'
    TXT
  end

  reload
end

#decrement(attr_name, by = 1) ⇒ Object



113
114
115
116
117
118
# File 'lib/massive_record/orm/persistence.rb', line 113

def decrement(attr_name, by = 1)
  raise NotNumericalFieldError unless attributes_schema[attr_name.to_s].type == :integer
  self[attr_name] ||= 0
  self[attr_name] -= by
  self
end

#decrement!(attr_name, by = 1) ⇒ Object



120
121
122
# File 'lib/massive_record/orm/persistence.rb', line 120

def decrement!(attr_name, by = 1)
  decrement(attr_name, by).update_attribute(attr_name, self[attr_name])
end

#destroyObject Also known as: delete



69
70
71
# File 'lib/massive_record/orm/persistence.rb', line 69

def destroy
  @destroyed = (persisted? ? do_destroy : true) and freeze
end

#destroyed?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/massive_record/orm/persistence.rb', line 30

def destroyed?
  @destroyed
end

#increment(attr_name, by = 1) ⇒ Object



97
98
99
100
101
102
# File 'lib/massive_record/orm/persistence.rb', line 97

def increment(attr_name, by = 1)
  raise NotNumericalFieldError unless attributes_schema[attr_name.to_s].type == :integer
  self[attr_name] ||= 0
  self[attr_name] += by
  self
end

#increment!(attr_name, by = 1) ⇒ Object



104
105
106
# File 'lib/massive_record/orm/persistence.rb', line 104

def increment!(attr_name, by = 1)
  increment(attr_name, by).update_attribute(attr_name, self[attr_name])
end

#new_record?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/massive_record/orm/persistence.rb', line 22

def new_record?
  @new_record
end

#persisted?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/massive_record/orm/persistence.rb', line 26

def persisted?
  !(new_record? || destroyed?)
end

#reloadObject



35
36
37
38
# File 'lib/massive_record/orm/persistence.rb', line 35

def reload
  Operations.reload(self).execute
  self
end

#saveObject



40
41
42
# File 'lib/massive_record/orm/persistence.rb', line 40

def save(*)
  create_or_update
end

#save!Object



44
45
46
# File 'lib/massive_record/orm/persistence.rb', line 44

def save!(*)
  create_or_update or raise RecordNotSaved
end

#touchObject

TODO This actually does nothing atm, but it’s here and callbacks on it

is working.


65
66
67
# File 'lib/massive_record/orm/persistence.rb', line 65

def touch
  true
end

#update_attribute(attr_name, value) ⇒ Object



48
49
50
51
# File 'lib/massive_record/orm/persistence.rb', line 48

def update_attribute(attr_name, value)
  self[attr_name] = value
  save(:validate => false)
end

#update_attributes(attributes) ⇒ Object



53
54
55
56
# File 'lib/massive_record/orm/persistence.rb', line 53

def update_attributes(attributes)
  self.attributes = attributes
  save
end

#update_attributes!(attributes) ⇒ Object



58
59
60
61
# File 'lib/massive_record/orm/persistence.rb', line 58

def update_attributes!(attributes)
  self.attributes = attributes
  save!
end