class MotionBlender::GraphMaker

Attributes

dependencies[R]
filter[RW]
layout[RW]
output[RW]
title[RW]

Public Class Methods

new(dependencies, opts = {}) click to toggle source
# File lib/motion_blender/graph_maker.rb, line 10
def initialize dependencies, opts = {}
  @dependencies = dependencies
  @title = opts[:title]
  @filter = opts[:filter]
  @layout = opts[:layout].try(:to_sym) || :sfdp
  @output = opts[:output] || 'graph.pdf'
end

Public Instance Methods

build() click to toggle source
# File lib/motion_blender/graph_maker.rb, line 18
def build
  g = GraphViz.new(@title || 'Graph', type: :digraph, use: @layout)
  label = [@title, (@filter && "/#{@filter}/")].compact.join(' ')
  g[:label] = label if label.present?
  g[:overlap] = false

  deps =
    @dependencies
    .select { |k, _| acceptable? k }
    .map { |k, v| [k, v.select { |f| acceptable? f }] }
  deps.map { |k, v| [k, *v] }.flatten.uniq.reverse_each do |f|
    g.add_node f, node_options_for(f)
  end
  deps.each do |k, v|
    v.each { |f| g.add_edge k, f, edge_options_for(k, f) }
  end

  g.output(output_format => @output)
end

Private Instance Methods

acceptable?(file) click to toggle source
# File lib/motion_blender/graph_maker.rb, line 44
def acceptable? file
  shorten(file) =~ filter_pattern
end
color_for(file) click to toggle source
# File lib/motion_blender/graph_maker.rb, line 69
def color_for file
  h = Zlib.crc32(shortened(file)) % 360
  Colorable::Color.new(Colorable::HSB.new(h, 90, 100))
end
edge_options_for(file, _) click to toggle source
# File lib/motion_blender/graph_maker.rb, line 84
def edge_options_for file, _
  {
    color: (color_for(file) * 'Gray'.to_color).hex + '66'
  }
end
filter_pattern() click to toggle source
# File lib/motion_blender/graph_maker.rb, line 48
def filter_pattern
  @filter_pattern ||= /#{@filter}/
end
node_options_for(file) click to toggle source
# File lib/motion_blender/graph_maker.rb, line 74
def node_options_for file
  {
    label: shorten(file).pathmap('%X'),
    href: file,
    color: color_for(file).hex,
    fillcolor: color_for(file).hex + '99',
    style: :filled
  }
end
output_format() click to toggle source
# File lib/motion_blender/graph_maker.rb, line 40
def output_format
  File.extname(@output)[1..-1].to_sym
end
shorten(file) click to toggle source
# File lib/motion_blender/graph_maker.rb, line 61
def shorten file
  file.sub(shorten_pattern, '')
end
shorten_pattern() click to toggle source
# File lib/motion_blender/graph_maker.rb, line 52
def shorten_pattern
  @shorten_pattern ||=
    begin
      paths = MotionBlender.config.motion_dirs + $LOAD_PATH
      patterns = paths.map { |p| Regexp.escape File.join(p, '') }
      Regexp.new(patterns.join('|'))
    end
end
shortened(file) click to toggle source
# File lib/motion_blender/graph_maker.rb, line 65
def shortened file
  file[shorten_pattern]
end