Class: Macrocosm

Inherits:
Object
  • Object
show all
Defined in:
lib/macrocosm.rb,
lib/macrocosm/version.rb,
lib/macrocosm/template.rb

Defined Under Namespace

Classes: Links, Template

Constant Summary collapse

NoCategory =
'no-category'
VERSION =
"0.1.7"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(curveness: 0) ⇒ Macrocosm

Returns a new instance of Macrocosm.



54
55
56
57
58
59
60
61
# File 'lib/macrocosm.rb', line 54

def initialize(curveness: 0)
  category_index = -1
  @categories = Hash.new{ |h, cate| h[cate] = (category_index += 1) }
  @nodes = []
  @links = Links.new

  @curveness = curveness
end

Instance Attribute Details

#curvenessObject (readonly)

Returns the value of attribute curveness.



52
53
54
# File 'lib/macrocosm.rb', line 52

def curveness
  @curveness
end

Instance Method Details



72
73
74
75
76
77
78
79
80
# File 'lib/macrocosm.rb', line 72

def add_link(source, target, relation_in_list: nil, relation_in_graph: nil, line_style: {})
  @links.add_link(
    source,
    target,
    relation_in_list: relation_in_list,
    relation_in_graph: relation_in_graph,
    line_style: line_style
  )
end

#add_node(name, category = nil) ⇒ Object



63
64
65
66
67
68
69
70
# File 'lib/macrocosm.rb', line 63

def add_node(name, category = nil)
  category = (category || NoCategory).to_s
  idx = @categories[category]
  @nodes << {
    name: name,
    category: idx
  }
end

#graphObject



89
90
91
92
93
94
95
# File 'lib/macrocosm.rb', line 89

def graph
  JSON.pretty_generate({
    nodes: @nodes,
    links: @links.links,
    categories: @categories.keys.sort!.map{ |name| {name: name} }
  })
end

#to_sObject



82
83
84
85
86
87
# File 'lib/macrocosm.rb', line 82

def to_s
  Template.new(
    graph: graph,
    curveness: curveness,
  ).render
end