Class: Mighost::Capturer

Inherits:
Object
  • Object
show all
Defined in:
lib/mighost/capturer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version, migration_path) ⇒ Capturer

Returns a new instance of Capturer.



11
12
13
14
# File 'lib/mighost/capturer.rb', line 11

def initialize(version, migration_path)
  @version = version.to_s
  @migration_path = migration_path
end

Instance Attribute Details

#migration_pathObject (readonly)

Returns the value of attribute migration_path.



9
10
11
# File 'lib/mighost/capturer.rb', line 9

def migration_path
  @migration_path
end

#versionObject (readonly)

Returns the value of attribute version.



9
10
11
# File 'lib/mighost/capturer.rb', line 9

def version
  @version
end

Instance Method Details

#captureObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mighost/capturer.rb', line 16

def capture
  return unless Mighost.enabled?
  return unless Mighost.configuration.auto_capture
  return unless File.exist?(migration_path)

  content = File.read(migration_path)
  filename = File.basename(migration_path)

  attributes = {
    version: version,
    content: content,
    filename: filename
  }

  if Mighost.configuration.
    attributes[:branch_name] = GitMetadata.current_branch
    attributes[:git_sha] = GitMetadata.current_sha

    author_name, author_email = git_author_of(migration_path)
    attributes[:author_name] = author_name
    attributes[:author_email] = author_email
  end

  Snapshot.store(**attributes)
rescue => e
  # Don't break migrations if snapshot capture fails
  warn "[Mighost] Failed to capture snapshot for #{version}: #{e.message}"
end