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:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/stagehand/staging/commit.rb', line 72

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) }
end

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



12
13
14
15
16
17
18
19
20
# File 'lib/stagehand/staging/commit.rb', line 12

def self.capture(subject_record = nil, except: [], &block)
  start_operation = start_commit(subject_record)
  init_session!(start_operation)
  block.call(start_operation)
  return end_commit(start_operation, except)
rescue => e
  end_commit(start_operation, except)
  raise(e)
end

.containing(record) ⇒ Object



22
23
24
# File 'lib/stagehand/staging/commit.rb', line 22

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



26
27
28
29
30
31
32
33
# File 'lib/stagehand/staging/commit.rb', line 26

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



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

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

#destroyObject



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

def destroy
  entries.delete_all
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  entries.content_operations.empty?
end

#entriesObject



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

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/stagehand/staging/commit.rb', line 101

def eql?(other)
  self == other
end

#hashObject



97
98
99
# File 'lib/stagehand/staging/commit.rb', line 97

def hash
  id
end

#idObject



89
90
91
# File 'lib/stagehand/staging/commit.rb', line 89

def id
  @start_id
end

#include?(record) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/stagehand/staging/commit.rb', line 93

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

#subjectObject



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

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