Module: Insta::Inline::PendingStore

Defined in:
lib/insta/inline/pending_store.rb,
sig/insta/inline/pending_store.rbs

Overview

@rbs! type pending_store_entry = { file: String, line: Integer, content: String, old_content: String, type: String }

Constant Summary collapse

FILENAME =

Returns:

  • (::String)
".insta-pending-inline"

Class Method Summary collapse

Class Method Details

.add(file:, line:, content:, old_content:, type:) ⇒ void

This method returns an undefined value.

: (file: String, line: Integer, content: String, old_content: String, type: Symbol) -> void

Parameters:

  • file: (String)
  • line: (Integer)
  • content: (String)
  • old_content: (String)
  • type: (Symbol)


18
19
20
21
22
23
24
25
26
27
28
# File 'lib/insta/inline/pending_store.rb', line 18

def self.add(file:, line:, content:, old_content:, type:)
  @mutex.synchronize do
    @pending << {
      file: file,
      line: line,
      content: content,
      old_content: old_content,
      type: type.to_s,
    }
  end
end

.any?Boolean

: () -> bool

Returns:

  • (Boolean)


109
110
111
# File 'lib/insta/inline/pending_store.rb', line 109

def self.any?
  @mutex.synchronize { !@pending.empty? }
end

.apply!(entries) ⇒ void

This method returns an undefined value.

: (Array) -> void

Parameters:

  • (Array[Inline::pending_store_entry])


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/insta/inline/pending_store.rb', line 72

def self.apply!(entries)
  grouped = entries.group_by { |entry| entry[:file] }

  grouped.each do |file, file_entries|
    next unless File.exist?(file)

    pending = file_entries.map { |entry|
      { line: entry[:line], content: entry[:content], type: entry[:type].to_sym }
    } #: Array[Inline::pending_entry]

    patched = FilePatcher.patch(file, pending)

    FilePatcher.atomic_write(file, patched)
  end
end

.clean!void

This method returns an undefined value.

: () -> void



65
66
67
68
69
# File 'lib/insta/inline/pending_store.rb', line 65

def self.clean!
  manifest_path = File.join(Insta.configuration.snapshot_path, FILENAME)

  FileUtils.rm_f(manifest_path)
end

.clear!void

This method returns an undefined value.

: () -> void



119
120
121
# File 'lib/insta/inline/pending_store.rb', line 119

def self.clear!
  @mutex.synchronize { @pending.clear }
end

.flush!void

This method returns an undefined value.

: () -> void



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/insta/inline/pending_store.rb', line 31

def self.flush!
  entries = @mutex.synchronize do
    result = @pending.dup
    @pending.clear

    result
  end

  return if entries.empty?

  manifest_path = File.join(Insta.configuration.snapshot_path, FILENAME)
  existing = if File.exist?(manifest_path)
               JSON.parse(File.read(manifest_path), symbolize_names: true)
             else
               [] #: Array[Inline::pending_store_entry]
             end

  merged = merge_entries(existing, entries)

  FileUtils.mkdir_p(File.dirname(manifest_path))
  File.write(manifest_path, JSON.pretty_generate(merged))
end

.loadArray[Inline::pending_store_entry]

: () -> Array

Returns:

  • (Array[Inline::pending_store_entry])


55
56
57
58
59
60
61
62
# File 'lib/insta/inline/pending_store.rb', line 55

def self.load
  manifest_path = File.join(Insta.configuration.snapshot_path, FILENAME)
  return [] unless File.exist?(manifest_path)

  JSON.parse(File.read(manifest_path), symbolize_names: true)
rescue JSON::ParserError
  []
end

.remove!(entries) ⇒ void

This method returns an undefined value.

: (Array) -> void

Parameters:

  • (Array[Inline::pending_store_entry])


89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/insta/inline/pending_store.rb', line 89

def self.remove!(entries)
  current = self.load
  return if current.empty?

  remaining = current.reject { |existing|
    entries.any? { |entry|
      existing[:file] == entry[:file] && existing[:line] == entry[:line]
    }
  }

  manifest_path = File.join(Insta.configuration.snapshot_path, FILENAME)

  if remaining.empty?
    FileUtils.rm_f(manifest_path)
  else
    File.write(manifest_path, JSON.pretty_generate(remaining))
  end
end

.sizeInteger

: () -> Integer

Returns:

  • (Integer)


114
115
116
# File 'lib/insta/inline/pending_store.rb', line 114

def self.size
  @mutex.synchronize { @pending.length }
end