Class: SnapshotUI::Snapshot

Inherits:
Object
  • Object
show all
Defined in:
lib/snapshot_ui/snapshot.rb,
lib/snapshot_ui/snapshot/context.rb,
lib/snapshot_ui/snapshot/storage.rb

Defined Under Namespace

Classes: Context, NotFound, Storage

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
end

#slugObject (readonly)

Returns the value of attribute slug.



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

def slug
  @slug
end

Class Method Details

.clear_snapshotsObject



39
40
41
# File 'lib/snapshot_ui/snapshot.rb', line 39

def self.clear_snapshots
  Storage.clear
end

.clear_snapshots_in_progressObject



35
36
37
# File 'lib/snapshot_ui/snapshot.rb', line 35

def self.clear_snapshots_in_progress
  Storage.clear(:in_progress)
end

.find(slug) ⇒ Object



17
18
19
20
21
22
# File 'lib/snapshot_ui/snapshot.rb', line 17

def self.find(slug)
  json = JSON.parse(Storage.read(slug), symbolize_names: true)
  new.from_json(json)
rescue Errno::ENOENT
  raise NotFound.new("Snapshot with a slug `#{slug}` can't be found.")
end

.grouped_by_test_caseObject



24
25
26
27
28
# File 'lib/snapshot_ui/snapshot.rb', line 24

def self.grouped_by_test_case
  all.group_by do |snapshot|
    snapshot.context.test_group
  end
end

.persist(snapshotee:, context:) ⇒ Object



13
14
15
# File 'lib/snapshot_ui/snapshot.rb', line 13

def self.persist(snapshotee:, context:)
  new.extract(snapshotee: snapshotee, context: context).persist
end

.publish_snapshots_in_progressObject



30
31
32
33
# File 'lib/snapshot_ui/snapshot.rb', line 30

def self.publish_snapshots_in_progress
  return unless SnapshotUI::Snapshot::Storage.in_progress_directory.exist?
  SnapshotUI::Snapshot::Storage.publish_snapshots_in_progress
end

Instance Method Details

#as_jsonObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/snapshot_ui/snapshot.rb', line 66

def as_json
  {
    type_data: {
      snapshotee_class: @snapshotee_class,
      body: body
    },
    context: {
      test_framework: context.test_framework,
      test_case_name: context.test_case_name,
      method_name: context.method_name,
      source_location: context.source_location,
      take_snapshot_index: context.take_snapshot_index
    },
    slug: context.to_slug
  }
end

#extract(snapshotee:, context:) ⇒ Object



55
56
57
58
59
60
# File 'lib/snapshot_ui/snapshot.rb', line 55

def extract(snapshotee:, context:)
  @body = snapshotee.body
  @snapshotee_class = snapshotee.class.to_s
  @context = Context.new(context)
  self
end

#from_json(json) ⇒ Object



83
84
85
86
87
88
# File 'lib/snapshot_ui/snapshot.rb', line 83

def from_json(json)
  @body = json[:type_data][:body]
  @context = Context.new(json[:context])
  @slug = json[:slug]
  self
end

#persistObject



62
63
64
# File 'lib/snapshot_ui/snapshot.rb', line 62

def persist
  Storage.write(context.to_slug, JSON.pretty_generate(as_json))
end