Class: GEXF::Edge

Inherits:
Object
  • Object
show all
Includes:
Attribute::Assignable
Defined in:
lib/gexf/edge.rb

Constant Summary collapse

DIRECTED =
:directed
UNDIRECTED =
:undirected
MUTUAL =
:mutual
TYPES =
[ DIRECTED, UNDIRECTED, MUTUAL]

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, source_id, target_id, opts = {}) ⇒ Edge

Returns a new instance of Edge.

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/gexf/edge.rb', line 12

def initialize(id, source_id, target_id, opts={})

  type      = opts[:type] || UNDIRECTED
  weight    = opts[:weight] && opts[:weight].to_f
  graph     = opts[:graph]
  label     = opts[:label] && opts[:label].to_s || nil
  id        = id.to_s
  source_id = source_id.to_s
  target_id = target_id.to_s

  raise ArgumentError.new("Invalid type: #{type}")                          if !TYPES.include?(type)
  raise ArgumentError.new("'weight' should be a positive, numerical value") if weight && weight < 0.1
  raise ArgumentError.new("Missing graph")                                  if !graph
  raise ArgumentError.new("Missing id")                                     if !id     || id.empty?
  raise ArgumentError.new("Missing source_id")                              if !source_id || source_id.empty?
  raise ArgumentError.new("Missing target_id")                              if !target_id || target_id.empty?

  @id          = id.to_s
  @label       = label
  @type        = type
  @source_id   = source_id.to_s
  @target_id   = target_id.to_s
  @weight      = weight || 1.0
  @graph       = graph
  # see GEXF::Attribute::Assignable
  @collection  = @graph.edges
  @attr_values = {}

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

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



10
11
12
# File 'lib/gexf/edge.rb', line 10

def id
  @id
end

#labelObject (readonly)

Returns the value of attribute label.



10
11
12
# File 'lib/gexf/edge.rb', line 10

def label
  @label
end

#source_idObject (readonly)

Returns the value of attribute source_id.



10
11
12
# File 'lib/gexf/edge.rb', line 10

def source_id
  @source_id
end

#target_idObject (readonly)

Returns the value of attribute target_id.



10
11
12
# File 'lib/gexf/edge.rb', line 10

def target_id
  @target_id
end

#typeObject (readonly)

Returns the value of attribute type.



10
11
12
# File 'lib/gexf/edge.rb', line 10

def type
  @type
end

#weightObject (readonly)

Returns the value of attribute weight.



10
11
12
# File 'lib/gexf/edge.rb', line 10

def weight
  @weight
end

Instance Method Details

#sourceObject



53
54
55
# File 'lib/gexf/edge.rb', line 53

def source
  @graph.nodes[source_id]
end

#targetObject



49
50
51
# File 'lib/gexf/edge.rb', line 49

def target
  @graph.nodes[target_id]
end

#to_hashObject



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/gexf/edge.rb', line 57

def to_hash
  optional = {}
  optional[:label] = label if label && !label.empty?
  optional[:weight] = weight if weight

  {:id => id,
   :source => source_id,
   :target => target_id,
   :type => type

  }.merge(optional)
end