Class: Diagram::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/diagram/graph.rb

Overview

Tool class to write dot file

Constant Summary collapse

OPTIONS =
{
  damping: 'Damping',
  k: 'K',
  url: 'URL',
  background: '_background',
  arrow_head: 'arrowhead',
  arrow_size: 'arrowsize',
  arrow_tail: 'arrowtail',
  bounding_box: 'bb',
  background_color: 'bgcolor',
  cluster_rank: 'clusterrank',
  color_scheme: 'colorscheme',
  default_distance: 'defaultdist',
  dir_edge_constraints: 'diredgeconstraints',
  edge_url: 'edgeURL',
  edge_href: 'edgehref',
  edge_length: 'len',
  edge_target: 'edgetarget',
  edge_tooltip: 'edgetooltip',
  edge_separator: 'esep',
  fill_color: 'fillcolor',
  fixed_size: 'fixedsize',
  font_color: 'fontcolor',
  font_name: 'fontname',
  font_names: 'fontnames',
  font_path: 'fontpath',
  font_size: 'fontsize',
  force_labels: 'forcelabels',
  gradient_angle: 'gradientangle',
  head_url: 'headURL',
  head_clip: 'headclip',
  head_href: 'headhref',
  head_label: 'headlabel',
  head_port: 'headport',
  head_target: 'headtarget',
  head_tooltip: 'headtooltip',
  image_path: 'imagepath',
  image_scale: 'imagescale',
  input_scale: 'inputscale',
  label_url: 'labelURL',
  label_scheme: 'label_scheme',
  label_angle: 'labelangle',
  label_distance: 'labeldistance',
  label_float: 'labelfloat',
  label_font_color: 'labelfontcolor',
  label_font_name: 'labelfontname',
  label_font_size: 'labelfontsize',
  label_href: 'labelhref',
  label_just: 'labeljust',
  label_loc: 'labelloc',
  label_target: 'labeltarget',
  label_tooltip: 'labeltooltip',
  layer_list_separator: 'layerlistsep',
  layer_select: 'layerselect',
  layer_separator: 'layersep',
  levels_gap: 'levelsgap',
  logical_head: 'lhead',
  logical_tail: 'ltail',
  label_height: 'lheight',
  label_position: 'lp',
  label_width: 'lwidth',
  max_iteration: 'maxiter',
  mc_limit: 'mclimit',
  min_distance: 'mindist',
  min_length: 'minlen',
  node_separator: 'nodesep',
  no_justify: 'nojustify',
  no_translate: 'notranslate',
  ns_limit: 'nslimit',
  ns_limit1: 'nslimit1',
  output_order: 'outputorder',
  overlap: 'overlap',
  overlap_scaling: 'overlap_scaling',
  overlap_shrink: 'overlap_shrink',
  pack_mode: 'packmode',
  page_dir: 'pagedir',
  pen_color: 'pencolor',
  pen_width: 'penwidth',
  position: 'pos',
  quad_tree: 'quadtree',
  rank_dir: 'rankdir',
  rank_separator: 'ranksep',
  re_min_cross: 'remincross',
  repulsive_force: 'repulsiveforce',
  same_head: 'samehead',
  same_tail: 'sametail',
  sample_points: 'samplepoints',
  search_size: 'searchsize',
  separator: 'sep',
  shape: 'shape',
  shape_file: 'shapefile',
  show_boxes: 'showboxes',
  tail_url: 'tailURL',
  tail_lp: 'tail_lp',
  tail_clip: 'tailclip',
  tail_href: 'tailhref',
  tail_label: 'taillabel',
  tail_port: 'tailport',
  tail_target: 'tailtarget',
  tail_tooltip: 'tailtooltip',
  true_color: 'truecolor',
  dot_version: 'xdotversion',
  external_label: 'xlabel',
  external_label_position: 'xlp'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Graph

Returns a new instance of Graph.


112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/diagram/graph.rb', line 112

def initialize(*args)
  options = args.extract_options!
  @name = args.shift || options.delete(:name) || 'G'
  @type = args.shift || options.delete(:type) || 'graph'
  @node_options = options.delete(:node) || {}
  @edge_options = options.delete(:edge) || {}
  @processor = options.delete(:processor) || :dot
  @options = options
  options[:overlap] = false
  options[:font_name] ||= 'Open Sans'
  [:font_name].each do |attr|
    @node_options[attr] ||= @options[attr]
    @edge_options[attr] ||= @options[attr]
  end
  @edge_options[:font_color] ||= '#688ED8'
  %i[head size tail].each do |key|
    if @edge_options[key]
      @edge_options["arrow_#{key}".to_sym] = @edge_options.delete(key)
    end
  end
  %i[url href target tooltip length separator].each do |key|
    if @edge_options[key]
      @edge_options["edge_#{key}".to_sym] = @edge_options.delete(key)
    end
  end
  @content = ''
end

Instance Attribute Details

#nameObject

Returns the value of attribute name


110
111
112
# File 'lib/diagram/graph.rb', line 110

def name
  @name
end

#processorObject

Returns the value of attribute processor


110
111
112
# File 'lib/diagram/graph.rb', line 110

def processor
  @processor
end

Instance Method Details

#arrow(from, to, options = {}) ⇒ Object


150
151
152
153
154
155
156
# File 'lib/diagram/graph.rb', line 150

def arrow(from, to, options = {})
  %i[head size tail].each do |key|
    options["arrow_#{key}".to_sym] = options.delete(key) if options[key]
  end
  options[:operator] = '->'
  edge(from, to, options)
end

#edge(from, to, options = {}) ⇒ Object


158
159
160
161
162
163
164
165
166
# File 'lib/diagram/graph.rb', line 158

def edge(from, to, options = {})
  %i[url href target tooltip length separator].each do |key|
    options["edge_#{key}".to_sym] = options.delete(key) if options[key]
  end
  operator = options.delete(:operator) || '--'
  @content << "  #{from} #{operator} #{to}"
  @content << " #{options_for_dot(options)}" if options.any?
  @content << ";\n"
end

#node(name, options = {}) ⇒ Object


140
141
142
143
144
# File 'lib/diagram/graph.rb', line 140

def node(name, options = {})
  @content << "  #{name}"
  @content << " #{options_for_dot(options)}" if options.any?
  @content << ";\n"
end

#record(name, options = {}) ⇒ Object


146
147
148
# File 'lib/diagram/graph.rb', line 146

def record(name, options = {})
  node(name, options.merge(shape: 'record'))
end

#subgraph(name, options = {}, &_block) ⇒ Object


168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/diagram/graph.rb', line 168

def subgraph(name, options = {}, &_block)
  @content << "  subgraph #{name} {\n"
  if options[:node]
    @content << "  node #{options_for_dot(options.delete(:node))};\n"
  end
  if options[:edge]
    @content << "  edge #{options_for_dot(options.delete(:edge))};\n"
  end
  @content << "  graph #{options_for_dot(options)};\n"
  yield
  @content << "  }\n"
end

#to_dotObject


181
182
183
184
185
186
187
188
# File 'lib/diagram/graph.rb', line 181

def to_dot
  graph = "#{@type} #{@name.to_s.underscore} {\n"
  graph << "  graph #{options_for_dot(@options)};\n"
  graph << "  node  #{options_for_dot(@node_options)};\n"
  graph << "  edge  #{options_for_dot(@edge_options)};\n"
  graph << @content
  graph << '}'
end

#write(options = {}) ⇒ Object


190
191
192
193
194
195
196
197
198
199
200
# File 'lib/diagram/graph.rb', line 190

def write(options = {})
  path = options[:path] || Rails.root.join('doc', 'diagrams', @name.to_s).to_s
  dot_file = "#{path}.gv"
  FileUtils.mkdir_p(File.dirname(dot_file))
  File.write(dot_file, to_dot)
  formats = options[:formats] || %w[png]
  formats.each do |format|
    `#{@processor} -T#{format} #{dot_file} > #{path + '.' + format.to_s}`
  end
  FileUtils.rm_rf dot_file
end