Class: MonkeyLens::Snapshot
- Inherits:
-
Object
- Object
- MonkeyLens::Snapshot
- Defined in:
- lib/monkey_lens/snapshot.rb,
sig/monkey_lens.rbs
Constant Summary collapse
- SCHEMA =
1
Instance Attribute Summary collapse
-
#engine ⇒ String
readonly
Returns the value of attribute engine.
-
#ruby ⇒ String
readonly
Returns the value of attribute ruby.
-
#schema ⇒ Integer
readonly
Returns the value of attribute schema.
-
#targets ⇒ Hash[String, untyped]
readonly
Returns the value of attribute targets.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(schema: SCHEMA, ruby:, engine:, targets:) ⇒ Snapshot
constructor
A new instance of Snapshot.
- #to_h ⇒ Hash[String, untyped]
- #write(path) ⇒ Snapshot
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
#engine ⇒ String (readonly)
Returns the value of attribute engine.
9 10 11 |
# File 'lib/monkey_lens/snapshot.rb', line 9 def engine @engine end |
#ruby ⇒ String (readonly)
Returns the value of attribute ruby.
9 10 11 |
# File 'lib/monkey_lens/snapshot.rb', line 9 def ruby @ruby end |
#schema ⇒ Integer (readonly)
Returns the value of attribute schema.
9 10 11 |
# File 'lib/monkey_lens/snapshot.rb', line 9 def schema @schema end |
#targets ⇒ Hash[String, untyped] (readonly)
Returns the value of attribute targets.
9 10 11 |
# File 'lib/monkey_lens/snapshot.rb', line 9 def targets @targets end |
Class Method Details
.read(path) ⇒ Snapshot
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.}" end |
Instance Method Details
#to_h ⇒ 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
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 |