Class: ExcADG::VStateData::Key

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/excadg/vstate_data.rb

Overview

class to support comparison and Arrays operations

Direct Known Subclasses

Full

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: nil, vertex: nil) ⇒ Key

Returns a new instance of Key.



17
18
19
20
21
22
23
24
# File 'lib/excadg/vstate_data.rb', line 17

def initialize name: nil, vertex: nil
  raise 'name or vertex are required' if name.nil? && vertex.nil?

  Assertions.is_a?(vertex, Vertex) unless vertex.nil?

  @name = name
  @vertex = vertex
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/excadg/vstate_data.rb', line 15

def name
  @name
end

#vertexObject (readonly)

Returns the value of attribute vertex.



15
16
17
# File 'lib/excadg/vstate_data.rb', line 15

def vertex
  @vertex
end

Instance Method Details

#<=>(other) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/excadg/vstate_data.rb', line 27

def <=> other
  return nil unless (other.is_a? self.class) || (is_a? other.class)
  # no mutual fields to compare
  return nil if (@vertex.nil? && other.name.nil?) || (@name.nil? && other.vertex.nil?)

  # name takes preference
  @name.nil? || other.name.nil? ? @vertex <=> other.vertex : @name <=> other.name
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/excadg/vstate_data.rb', line 36

def eql? other
  (self <=> other)&.zero? || false
end

#to_sObject



40
41
42
# File 'lib/excadg/vstate_data.rb', line 40

def to_s
  (@name || @vertex).to_s
end