Class: StubFile

Inherits:
FileLike show all
Defined in:
app/models/stub_file.rb

Overview

A simple object we can use in place of a GithubFile as a test mock or for debugging

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from FileLike

#basename, #extname, #first_line_of_patch

Constructor Details

#initialize(path:, blob:, patch: nil) ⇒ StubFile

Returns a new instance of StubFile.



17
18
19
20
21
# File 'app/models/stub_file.rb', line 17

def initialize(path:, blob:, patch: nil)
  @path = path
  @blob = blob
  @patch = patch
end

Instance Attribute Details

#blobObject

Returns the value of attribute blob.



6
7
8
# File 'app/models/stub_file.rb', line 6

def blob
  @blob
end

#pathObject

Returns the value of attribute path.



6
7
8
# File 'app/models/stub_file.rb', line 6

def path
  @path
end

Class Method Details

.from_json(json) ⇒ Object



8
9
10
11
12
13
14
15
# File 'app/models/stub_file.rb', line 8

def self.from_json(json)
  json = json.symbolize_keys
  StubFile.new(
    path: json[:path],
    blob: json[:blob],
    patch: Patch.new(json[:patch]),
  )
end

Instance Method Details

#as_json(_opts = {}) ⇒ Object



33
34
35
36
37
38
39
# File 'app/models/stub_file.rb', line 33

def as_json(_opts = {})
  {
    path: @path,
    blob: @blob,
    patch: patch.body,
  }
end

#patchObject



29
30
31
# File 'app/models/stub_file.rb', line 29

def patch
  @patch || Patch.new(patch_from_blob)
end

#patch_from_blobObject



23
24
25
26
27
# File 'app/models/stub_file.rb', line 23

def patch_from_blob
  lines = @blob.lines
  "@@ -0,0 +1,#{lines.length}\n" +
  lines.map { |l| "+#{l}" }.join('')
end