Class: Aspen::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/aspen/node.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label:, attributes: {}) ⇒ Node

Returns a new instance of Node.



12
13
14
15
16
# File 'lib/aspen/node.rb', line 12

def initialize(label: , attributes: {})
  @label      = label
  @attributes = attributes
  @nickname   = nickname_from_first_attr_value
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



9
10
11
# File 'lib/aspen/node.rb', line 9

def attributes
  @attributes
end

#labelObject (readonly)

Returns the value of attribute label.



9
10
11
# File 'lib/aspen/node.rb', line 9

def label
  @label
end

#nicknameObject

Returns the value of attribute nickname.



9
10
11
# File 'lib/aspen/node.rb', line 9

def nickname
  @nickname
end

Instance Method Details

#==(other) ⇒ Object



46
47
48
49
50
# File 'lib/aspen/node.rb', line 46

def ==(other)
  label      == other.label &&
  attributes == other.attributes &&
  nickname   == other.nickname
end

#attribute_stringObject



38
39
40
41
42
43
44
# File 'lib/aspen/node.rb', line 38

def attribute_string
  attributes.to_s.
    gsub(/"(?<token>[[:alpha:]_]+)"=>/, '\k<token>: ').
    # This puts a single space inside curly braces.
    gsub(/\{(\s*)/, "{ ").
    gsub(/(\s*)\}/, " }")
end

#nickname_from_first_attr_valueObject



18
19
20
# File 'lib/aspen/node.rb', line 18

def nickname_from_first_attr_value
  "#{@label}-#{@attributes.values.first}".parameterize.underscore
end

#nickname_nodeObject



30
31
32
# File 'lib/aspen/node.rb', line 30

def nickname_node
  "(#{nickname})"
end

#signatureObject



34
35
36
# File 'lib/aspen/node.rb', line 34

def signature
  "(#{label})"
end

#to_cypherObject



22
23
24
25
26
27
28
# File 'lib/aspen/node.rb', line 22

def to_cypher
  if nickname
    "(#{nickname}:#{label} #{ attribute_string })"
  else
    "(#{label} #{ attribute_string })"
  end
end