Class: MonkeyLens::Snapshot

Inherits:
Object
  • Object
show all
Defined in:
lib/monkey_lens/snapshot.rb,
sig/monkey_lens.rbs

Constant Summary collapse

SCHEMA =

Returns:

  • (Integer)
1

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema: SCHEMA, ruby:, engine:, targets:) ⇒ Snapshot

Returns a new instance of Snapshot.



11
12
13
14
15
16
17
# File 'lib/monkey_lens/snapshot.rb', line 11

def initialize(schema: SCHEMA, ruby:, engine:, targets:)
  @schema = Integer(schema)
  @ruby = ruby.to_s
  @engine = engine.to_s
  @targets = targets.transform_keys(&:to_s)
  validate!
end

Instance Attribute Details

#engineString (readonly)

Returns the value of attribute engine.

Returns:

  • (String)


9
10
11
# File 'lib/monkey_lens/snapshot.rb', line 9

def engine
  @engine
end

#rubyString (readonly)

Returns the value of attribute ruby.

Returns:

  • (String)


9
10
11
# File 'lib/monkey_lens/snapshot.rb', line 9

def ruby
  @ruby
end

#schemaInteger (readonly)

Returns the value of attribute schema.

Returns:

  • (Integer)


9
10
11
# File 'lib/monkey_lens/snapshot.rb', line 9

def schema
  @schema
end

#targetsHash[String, untyped] (readonly)

Returns the value of attribute targets.

Returns:

  • (Hash[String, untyped])


9
10
11
# File 'lib/monkey_lens/snapshot.rb', line 9

def targets
  @targets
end

Class Method Details

.read(path) ⇒ Snapshot

Parameters:

  • path (String)

Returns:



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/monkey_lens/snapshot.rb', line 19

def self.read(path)
  data = JSON.parse(File.read(path))
  new(
    schema: data.fetch("schema"),
    ruby: data.fetch("ruby"),
    engine: data.fetch("engine"),
    targets: data.fetch("targets")
  )
rescue JSON::ParserError, KeyError => error
  raise SnapshotError, "invalid snapshot #{path}: #{error.message}"
end

Instance Method Details

#to_hHash[String, untyped]

Returns:

  • (Hash[String, untyped])


36
37
38
39
40
41
42
43
# File 'lib/monkey_lens/snapshot.rb', line 36

def to_h
  {
    "schema" => schema,
    "ruby" => ruby,
    "engine" => engine,
    "targets" => targets.sort.to_h
  }
end

#write(path) ⇒ Snapshot

Parameters:

  • path (String)

Returns:



31
32
33
34
# File 'lib/monkey_lens/snapshot.rb', line 31

def write(path)
  File.write(path, "#{JSON.pretty_generate(to_h)}\n")
  self
end