Class: GEXF::Node

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Attribute::Assignable
Defined in:
lib/gexf/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Attribute::Assignable

#[], #[]=, #attr_values, #attributes, included, #set_attr_by_id

Constructor Details

#initialize(id, graph, opts = {}) ⇒ Node

Returns a new instance of Node.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/gexf/node.rb', line 9

def initialize(id, graph, opts={})
  @graph       = graph
  @id          = id_type(id)
  @label       = opts[:label] || @id

  # see GEXF::Attribute::Assignable
  @collection  = graph.nodes
  @attr_values = {}

  set_attributes(opts.fetch(:attributes, {}))
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/gexf/node.rb', line 5

def id
  @id
end

#labelObject (readonly)

Returns the value of attribute label.



5
6
7
# File 'lib/gexf/node.rb', line 5

def label
  @label
end

Instance Method Details

#connect_to(target, opts = {}) ⇒ Object



21
22
23
24
# File 'lib/gexf/node.rb', line 21

def connect_to(target, opts={})
  graph.nodes << target
  graph.create_edge(self, target, opts)
end

#connectionsObject



32
33
34
# File 'lib/gexf/node.rb', line 32

def connections
  graph.edges[id].to_a
end

#create_and_connect_to(target_attr, opts = {}) ⇒ Object



26
27
28
29
30
# File 'lib/gexf/node.rb', line 26

def create_and_connect_to(target_attr, opts={})
  node = graph.create_node(target_attr)
  connect_to(node, opts)
  node
end

#incoming_connectionsObject



36
37
38
# File 'lib/gexf/node.rb', line 36

def incoming_connections
  connections.select { |edge| edge.target_id == id }
end

#outgoing_connectionsObject



40
41
42
# File 'lib/gexf/node.rb', line 40

def outgoing_connections
  connections.select { |edge| edge.source_id == id }
end

#to_hashObject



44
45
46
# File 'lib/gexf/node.rb', line 44

def to_hash
  {:id => id, :label => label}
end