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:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/stagehand/staging/commit.rb', line 87

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
  missing << CommitEntry::END_OPERATION unless @end_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)
    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



120
121
122
# File 'lib/stagehand/staging/commit.rb', line 120

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

#destroyObject



136
137
138
# File 'lib/stagehand/staging/commit.rb', line 136

def destroy
  entries.delete_all
end

#empty?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/stagehand/staging/commit.rb', line 132

def empty?
  entries.content_operations.empty?
end

#entriesObject



124
125
126
# File 'lib/stagehand/staging/commit.rb', line 124

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/stagehand/staging/commit.rb', line 116

def eql?(other)
  self == other
end

#hashObject



112
113
114
# File 'lib/stagehand/staging/commit.rb', line 112

def hash
  id
end

#idObject



104
105
106
# File 'lib/stagehand/staging/commit.rb', line 104

def id
  @start_id
end

#include?(record) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/stagehand/staging/commit.rb', line 108

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

#subjectObject



128
129
130
# File 'lib/stagehand/staging/commit.rb', line 128

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