Class: Visualizer
- Inherits:
-
Object
- Object
- Visualizer
- Defined in:
- lib/model-visualizer/visualizer.rb
Constant Summary collapse
- FILE_PATH =
'model-visualizer.html'
Instance Method Summary collapse
- #create_visualization ⇒ Object
-
#initialize(models) ⇒ Visualizer
constructor
A new instance of Visualizer.
- #launch_browser(path) ⇒ Object
Constructor Details
#initialize(models) ⇒ Visualizer
Returns a new instance of Visualizer.
8 9 10 |
# File 'lib/model-visualizer/visualizer.rb', line 8 def initialize(models) @models = models end |
Instance Method Details
#create_visualization ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/model-visualizer/visualizer.rb', line 12 def create_visualization # Get file from gem directory g = Gem::Specification.find_by_name 'model-visualizer' template = File.join(g.full_gem_path, 'share/template.html') d3 = File.join(g.full_gem_path, 'share/d3.min.js') # Insert data into file template_contents = File.read template output = template_contents.gsub(/<%= @models %>/, JSON.generate(@models)) .gsub(/<%= @d3 %>/, d3) # Write and open file File.open(FILE_PATH, 'w') {|file| file.puts output} self.launch_browser FILE_PATH end |
#launch_browser(path) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/model-visualizer/visualizer.rb', line 29 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 |