Module: Kettle::Drift::Serializer

Defined in:
lib/kettle/drift/serializer.rb

Class Method Summary collapse

Class Method Details

.deserialize(data) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/kettle/drift/serializer.rb', line 17

def deserialize(data)
  parsed = JSON.parse(data)
  raise Kettle::Drift::Error, "Wrong format of the lock file: expected a JSON object" unless parsed.is_a?(Hash)

  parsed.keys.sort.each_with_object({}) do |chunk, normalized|
    entries = parsed.fetch(chunk)
    unless entries.is_a?(Array)
      raise Kettle::Drift::Error, "Wrong format of the lock file: `#{chunk}` must map to an array"
    end

    normalized[chunk] = entries.map { |entry| deserialize_entry(chunk, entry) }
  end
rescue JSON::ParserError => e
  raise Kettle::Drift::Error, "Wrong format of the lock file: #{e.message}"
end

.normalize(results, project_root: nil) ⇒ Object



9
10
11
# File 'lib/kettle/drift/serializer.rb', line 9

def normalize(results, project_root: nil)
  deserialize(serialize(results, project_root: project_root))
end

.serialize(results, project_root: nil) ⇒ Object



13
14
15
# File 'lib/kettle/drift/serializer.rb', line 13

def serialize(results, project_root: nil)
  "#{JSON.pretty_generate(normalize_results(results, project_root: project_root))}\n"
end