class Visualizer
Constants
- FILE_PATH
Public Class Methods
new(models)
click to toggle source
# File lib/model-visualizer/visualizer.rb, line 8 def initialize(models) @models = models end
Public Instance Methods
create_visualization(title)
click to toggle source
# File lib/model-visualizer/visualizer.rb, line 12 def create_visualization(title) # Get file from gem directory g = Gem::Specification.find_by_name 'model-visualizer' template = File.join(g.full_gem_path, 'share/template.html') css = File.join(g.full_gem_path, 'share/main.css') d3 = File.join(g.full_gem_path, 'share/d3.min.js') tooltip = File.join(g.full_gem_path, 'share/d3.tip.js') # Insert data into file template_contents = File.read template output = template_contents.gsub(/<%= @models %>/, JSON.generate(@models)) .gsub(/<%= @css %>/, css) .gsub(/<%= @d3 %>/, d3) .gsub(/<%= @title %>/, title + ' Model Visualization') .gsub(/<%= @sidebar %>/, create_sidebar) .gsub(/<%= @tooltip %>/, tooltip) # Write and open file File.open(FILE_PATH, 'w') {|file| file.puts output} self.launch_browser FILE_PATH end
launch_browser(path)
click to toggle source
stackoverflow.com/questions/152699/open-the-default-browser-in-ruby
# File lib/model-visualizer/visualizer.rb, line 44 def launch_browser(path) if RbConfig::CONFIG['host_os'] =~ /mswin|mingw|cygwin/ system "start #{path}" elsif RbConfig::CONFIG['host_os'] =~ /darwin/ system "open #{path}" elsif RbConfig::CONFIG['host_os'] =~ /linux|bsd/ system "xdg-open #{path}" end end