snapbot 0.1.0 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9ff9b478fc49144d2edf97c515e52f946b3f73a0d939e0de6d9789e6ab207a1b
4
- data.tar.gz: 3d10ece40e5e1e2dfc67f8552746f6c2f326b54d0977a2a39764f1ba008a9a97
3
+ metadata.gz: bb58059025b35d29e5d762be3719f21461f225a2950c51efe54161009b1412ca
4
+ data.tar.gz: a08b6fe2a72289b2946ac7461920863a7226a9d24b12a24d3b23dbe1f2ca00dd
5
5
  SHA512:
6
- metadata.gz: dff17039b7af000c3d9a6fb17ba2fc21af8da0a0ab3a72a9b2af098f8b0e06b7c5b3a3230b547d78886f5e9bd320c9c30f2095fdb9e55ee5c892488be5c04e6b
7
- data.tar.gz: 578806b5e7c84b72691ebad4069ac39a51c0fcd04b79b62282fbb57fbf096dc88e29e8c23d801e1ed230a8925dc4b004d1c80b80520d7eb3ece9caf612e208e3
6
+ metadata.gz: 31970a0125ab31512d051b4a8dac26e324fa556dd6ccfdffa1a83dc8bc9b6a962f5423150bcc634c45420cc59e12258b3d034b1210765b7c63cbba033b8a3277
7
+ data.tar.gz: 530cebe2837a43ea3a9bddcdb63ed3bdda8666d3a2dd05cb79926e3d6f9c5b3c41da750e8d5dfc6c67fa3784c4f1c672b4620c0e2d135a1825bf87b1182b76fa
data/.rubocop.yml CHANGED
@@ -7,6 +7,10 @@ Metrics/BlockLength:
7
7
  - spec/**/*_spec.rb
8
8
  - snapbot.gemspec
9
9
 
10
+ Style/StderrPuts:
11
+ Exclude:
12
+ - lib/snapbot/diagram/renderer.rb
13
+
10
14
  Style/StringLiterals:
11
15
  Enabled: true
12
16
  EnforcedStyle: double_quotes
@@ -40,7 +40,7 @@ module Snapbot
40
40
  def collect_lets(example)
41
41
  @lets_by_value = RSpec::Lets.new(example).collect.each_with_object({}) do |sym, lets_by_value|
42
42
  value = example.send(sym) unless @ignore_lets.include?(sym)
43
- lets_by_value[value] = sym if value.is_a?(Reflector::BASE_ACTIVERECORD_CLASS)
43
+ lets_by_value[value] = sym if value.is_a?(reflector.base_activerecord_class)
44
44
  end
45
45
  end
46
46
 
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "open3"
4
+
5
+ module Snapbot
6
+ module Diagram
7
+ # Render some DOT via Graphviz dot command line
8
+ class Renderer
9
+ INSTALL_GRAPHVIZ_URL = "https://graphviz.org/download/#executable-packages"
10
+ OUTPUT_FILENAME = "tmp/models.svg"
11
+
12
+ def initialize(dot)
13
+ @dot = dot
14
+ end
15
+
16
+ def save
17
+ ensure_graphviz
18
+ FileUtils.rm(OUTPUT_FILENAME, force: true)
19
+ FileUtils.mkdir_p(File.dirname(OUTPUT_FILENAME))
20
+
21
+ IO.popen("dot -Tsvg -o #{OUTPUT_FILENAME}", "w") do |pipe|
22
+ pipe.puts(@dot)
23
+ end
24
+
25
+ warn "Written to #{OUTPUT_FILENAME}"
26
+ OUTPUT_FILENAME
27
+ end
28
+
29
+ private
30
+
31
+ def graphviz_executable
32
+ `which dot`
33
+ end
34
+
35
+ def ensure_graphviz
36
+ return unless graphviz_executable.empty?
37
+
38
+ warn <<~GRAPHVIZ
39
+ The graphviz executable `dot` was not found.
40
+ Install from #{INSTALL_GRAPHVIZ_URL}
41
+ GRAPHVIZ
42
+ end
43
+ end
44
+ end
45
+ end
@@ -1,11 +1,28 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "snapbot/diagram/dot_generator"
4
+ require "snapbot/diagram/renderer"
5
+ require "open3"
6
+
3
7
  module Snapbot
4
8
  # Print the small constellation of objects in your integration test and how they relate.
5
9
  # Requires Graphviz. Optimised for Mac. YMMV.
6
10
  module Diagram
7
11
  def save_and_open_diagram(**args)
8
- DotGenerator.new(**args).open
12
+ dot = DotGenerator.new(**args).dot
13
+ filename = Renderer.new(dot).save
14
+
15
+ unless open_command.present?
16
+ warn "No `open` command available. File saved to #{filename}"
17
+ return
18
+ end
19
+
20
+ _stdout, stderr, status = Open3.capture3("#{open_command} #{filename}")
21
+ raise stderr unless status.exitstatus.zero?
22
+ end
23
+
24
+ def open_command
25
+ `which open`.chomp
9
26
  end
10
27
  end
11
28
  end
@@ -5,12 +5,14 @@ require "active_record"
5
5
  module Snapbot
6
6
  # Reflect models and instances in a way that's useful for generating a diagram
7
7
  class Reflector
8
- BASE_ACTIVERECORD_CLASS = defined?(ApplicationRecord) ? ApplicationRecord : ActiveRecord::Base
8
+ def base_activerecord_class
9
+ defined?(ApplicationRecord) ? ApplicationRecord : ActiveRecord::Base
10
+ end
9
11
 
10
12
  def models(only_with_records: true)
11
13
  @models ||= begin
12
14
  Rails.application.eager_load! if defined?(Rails)
13
- BASE_ACTIVERECORD_CLASS.descendants.reject do |c|
15
+ base_activerecord_class.descendants.reject do |c|
14
16
  c.to_s == "Schema" || (only_with_records && c.count.zero?)
15
17
  end
16
18
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Snapbot
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.3"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: snapbot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Russell Garner
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-08-05 00:00:00.000000000 Z
11
+ date: 2022-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: binding_of_caller
@@ -99,6 +99,7 @@ files:
99
99
  - lib/snapbot/diagram.rb
100
100
  - lib/snapbot/diagram/dot_generator.rb
101
101
  - lib/snapbot/diagram/dot_generator/relationship.rb
102
+ - lib/snapbot/diagram/renderer.rb
102
103
  - lib/snapbot/reflector.rb
103
104
  - lib/snapbot/rspec/lets.rb
104
105
  - lib/snapbot/version.rb