Class: Stagehand::Staging::Commit

Inherits:
Object
  • Object
show all
Defined in:
lib/stagehand/staging/commit.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_id) ⇒ Commit

Returns a new instance of Commit.

Raises:



97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/stagehand/staging/commit.rb', line 97

def initialize(start_id)
  @start_id, @end_id = CommitEntry.control_operations
    .limit(2)
    .where(:commit_id => start_id)
    .where('id >= ?', start_id)
    .reorder(:id => :asc)
    .pluck(:id)

  return if @start_id && @end_id

  missing = []
  missing << CommitEntry::START_OPERATION unless @start_id == start_id
  missing << CommitEntry::END_OPERATION if @start_id == start_id

  raise CommitNotFound, "Couldn't find #{missing.join(', ')} entry for Commit #{start_id}"
end

Class Method Details

.allObject



4
5
6
# File 'lib/stagehand/staging/commit.rb', line 4

def self.all
  CommitEntry.end_operations.pluck(:commit_id).collect {|id| find(id) }.compact
end

.capture(subject_record = nil, except: [], &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/stagehand/staging/commit.rb', line 16

def self.capture(subject_record = nil, except: [], &block)
  @capturing = true
  start_operation = start_commit(subject_record)
  init_session!(start_operation)

  begin
    block.call(start_operation)
  rescue => e
    end_commit(start_operation, except) unless e.is_a?(CommitError) || e.is_a?(ActiveRecord::Rollback)
    raise(e)
  else
    return end_commit(start_operation, except)
  end
ensure
  @capturing = false
end

.capturing?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/stagehand/staging/commit.rb', line 12

def self.capturing?
  !!@capturing
end

.containing(record) ⇒ Object



33
34
35
# File 'lib/stagehand/staging/commit.rb', line 33

def self.containing(record)
  find(CommitEntry.contained.matching(record).pluck(:commit_id))
end

.emptyObject



8
9
10
# File 'lib/stagehand/staging/commit.rb', line 8

def self.empty
  all.select(&:empty?)
end

.find(start_ids) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/stagehand/staging/commit.rb', line 37

def self.find(start_ids)
  if start_ids.respond_to?(:each)
    start_ids.to_a.uniq.collect {|id| find(id) }.compact
  else
    new(start_ids)
  end
rescue CommitNotFound
end

Instance Method Details

#==(other) ⇒ Object



130
131
132
# File 'lib/stagehand/staging/commit.rb', line 130

def ==(other)
  id == other.id
end

#destroyObject



146
147
148
# File 'lib/stagehand/staging/commit.rb', line 146

def destroy
  entries.delete_all
end

#empty?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/stagehand/staging/commit.rb', line 142

def empty?
  entries.content_operations.empty?
end

#entriesObject



134
135
136
# File 'lib/stagehand/staging/commit.rb', line 134

def entries
  CommitEntry.where(:id => @start_id..@end_id).where(:commit_id => id)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/stagehand/staging/commit.rb', line 126

def eql?(other)
  self == other
end

#hashObject



122
123
124
# File 'lib/stagehand/staging/commit.rb', line 122

def hash
  id
end

#idObject



114
115
116
# File 'lib/stagehand/staging/commit.rb', line 114

def id
  @start_id
end

#include?(record) ⇒ Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/stagehand/staging/commit.rb', line 118

def include?(record)
  entries.where(:record_id => record.id, :table_name => record.class.table_name).exists?
end

#subjectObject



138
139
140
# File 'lib/stagehand/staging/commit.rb', line 138

def subject
  entries.sort_by(&:id).first.record
end