Class: Packwerk::Cache::CacheContents

Inherits:
Object
  • Object
show all
Defined in:
lib/packwerk/cache.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_contents_digest:, unresolved_references:) ⇒ CacheContents

: (file_contents_digest: String, unresolved_references: Array) -> void



16
17
18
19
# File 'lib/packwerk/cache.rb', line 16

def initialize(file_contents_digest:, unresolved_references:)
  @file_contents_digest = file_contents_digest
  @unresolved_references = unresolved_references
end

Instance Attribute Details

#file_contents_digestObject (readonly)

: String



10
11
12
# File 'lib/packwerk/cache.rb', line 10

def file_contents_digest
  @file_contents_digest
end

#unresolved_referencesObject (readonly)

: Array



13
14
15
# File 'lib/packwerk/cache.rb', line 13

def unresolved_references
  @unresolved_references
end

Class Method Details

.deserialize(serialized_cache_contents) ⇒ Object

: (String serialized_cache_contents) -> CacheContents



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/packwerk/cache.rb', line 23

def deserialize(serialized_cache_contents)
  cache_contents_json = JSON.parse(serialized_cache_contents)
  unresolved_references = cache_contents_json["unresolved_references"].map do |json|
    UnresolvedReference.new(
      constant_name: json["constant_name"],
      namespace_path: json["namespace_path"],
      relative_path: json["relative_path"],
      source_location: Node::Location.new(json["source_location"]["line"], json["source_location"]["column"],)
    )
  end

  CacheContents.new(
    file_contents_digest: cache_contents_json["file_contents_digest"],
    unresolved_references: unresolved_references,
  )
end

Instance Method Details

#serializeObject

: -> String



59
60
61
# File 'lib/packwerk/cache.rb', line 59

def serialize
  to_json
end

#to_json(*_args) ⇒ Object

: (*untyped _args) -> String



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/packwerk/cache.rb', line 42

def to_json(*_args)
  JSON.generate({
    file_contents_digest: @file_contents_digest,
    unresolved_references: @unresolved_references.map do |ref|
      source_location = ref.source_location #: as !nil

      {
        constant_name: ref.constant_name,
        namespace_path: ref.namespace_path,
        relative_path: ref.relative_path,
        source_location: { line: source_location.line, column: source_location.column },
      }
    end,
  })
end