Class: Gexp::Mongoid::Transaction::Base

Inherits:
Object
  • Object
show all
Includes:
Mongoid::Document
Defined in:
lib/gexp/mongoid/transaction.rb

Overview

extend ActiveSupport::Concern

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.inherited(sub) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/gexp/mongoid/transaction.rb', line 105

def inherited(sub)
  sub.instance_eval do

    def field(name, *args)
      super # вызов Mongoid::Document#field

      return if SYSTEM_FIELDS.include? name.to_sym

      field_setter = "#{name}=".to_sym
      field_getter = "#{name}"

      # переопределяем сеттер
      define_method(field_setter) do |val|

        if Observer.started?
          self.transaction ||= Observer.runned
        end

        if self.new_record?
          super(val)
        else
          if Observer.started?
            # Создание грязной версии если еще нет
            self.create_update
            self._updated[name.to_s] = val
          else
            super(val)
          end
        end
      end

      define_method(field_getter) do
        # Если транзакция началась и текущий объект не _updated
        if Observer.started? && self._updated
          self._updated[name.to_s]
        else
          super()
        end
      end

    end
  end
end

Instance Method Details

#can_save?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/gexp/mongoid/transaction.rb', line 159

def can_save?
  self.new_record? || (not Observer.started?)
end

#check_dirtyObject

Проверка объекта на незаконченную транзакцию если объект “грязный”, то чистим его



198
199
200
# File 'lib/gexp/mongoid/transaction.rb', line 198

def check_dirty
  self.clear! if self.dirty?
end

#clear!Object



215
216
217
218
219
220
221
222
223
# File 'lib/gexp/mongoid/transaction.rb', line 215

def clear!
  if self.dirty_commited?
    self.clear_commited! 
  elsif self.dirty_uncommited?
    self.clear_uncommited!
  end

  self.save
end

#clear?Boolean

Чистый

Returns:

  • (Boolean)


231
232
233
# File 'lib/gexp/mongoid/transaction.rb', line 231

def clear?
  self._updated.nil? && self._transaction_id.nil?
end

#clear_commited!Object



202
203
204
205
206
207
# File 'lib/gexp/mongoid/transaction.rb', line 202

def clear_commited!
  self.updated_apply!
  self.clear_updated!
  self._version += 1
  self._transaction_id = nil
end

#clear_uncommited!Object



209
210
211
212
213
# File 'lib/gexp/mongoid/transaction.rb', line 209

def clear_uncommited!
  _transaction_id = Observer.started? ? Observer.runned.id : nil
  self._transaction_id = _transaction_id
  self.clear_updated!
end

#clear_updated!Object



188
189
190
# File 'lib/gexp/mongoid/transaction.rb', line 188

def clear_updated!
  self._updated = nil
end

#create_updateObject

def save!

return super if can_save?
true

end



173
174
175
# File 'lib/gexp/mongoid/transaction.rb', line 173

def create_update
  self._updated ||= self.attributes.except('_id', '_type')
end

#dirty?Boolean

Returns:

  • (Boolean)


225
226
227
228
# File 'lib/gexp/mongoid/transaction.rb', line 225

def dirty?
  self.dirty_commited? ||
    self.dirty_uncommited?
end

#dirty_commited?Boolean

Грязный закоммиченный

Returns:

  • (Boolean)


245
246
247
248
249
# File 'lib/gexp/mongoid/transaction.rb', line 245

def dirty_commited?
  self.update_exist? && 
    self._transaction_id.present? &&
    (not self.exist_transaction?)
end

#dirty_uncommited?Boolean

Грязный незакоммиченный

Returns:

  • (Boolean)


252
253
254
255
256
# File 'lib/gexp/mongoid/transaction.rb', line 252

def dirty_uncommited?
  self.update_exist? && 
    self._transaction_id.present? &&
    self.exist_transaction?
end

#exist_transaction?Boolean

Существование у объекта транзакции

Returns:

  • (Boolean)


236
237
238
239
240
241
242
# File 'lib/gexp/mongoid/transaction.rb', line 236

def exist_transaction?
  return false unless self._transaction_id
  Instance.find(self._transaction_id)
  true
rescue ::Mongoid::Errors::DocumentNotFound
  false
end

#saveObject



163
164
165
166
# File 'lib/gexp/mongoid/transaction.rb', line 163

def save
  return super if can_save?
  true
end

#transaction_commitObject

Вызывается только тогда, когда все объекты сохраненны



178
179
180
181
182
183
184
185
186
# File 'lib/gexp/mongoid/transaction.rb', line 178

def transaction_commit
  if self._updated
    self.updated_apply!
    self.clear_updated!
    self._version += 1
    self._transaction_id = nil
    self.save
  end
end

#update_exist?Boolean

Сущетвование не закоммиченной версии

Returns:

  • (Boolean)


259
260
261
# File 'lib/gexp/mongoid/transaction.rb', line 259

def update_exist?
  self._updated.present?
end

#updated_apply!Object



192
193
194
# File 'lib/gexp/mongoid/transaction.rb', line 192

def updated_apply!
  self.attributes = self.attributes.merge(self._updated)
end