Class: Graph::Node

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

Overview

A node. This class is just a wrapper around a hash of attributes. Before 0.1.6, nodes were simple hashs

Since:

  • 0.1.6

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = nil) ⇒ Node

Create a new Node

Parameters:

  • attrs (Node, Hash) (defaults to: nil)

Since:

  • 0.1.6



59
60
61
# File 'lib/graph.rb', line 59

def initialize(attrs=nil)
    @attrs = attrs.is_a?(Node) ? attrs.attrs : attrs || {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object

Since:

  • 0.1.6



79
80
81
82
83
84
# File 'lib/graph.rb', line 79

def method_missing(method, *args, &block)
    return @attrs[method.to_sym] if @attrs.has_key? method.to_sym
    return @attrs[method.to_s] if @attrs.has_key? method.to_s

    @attrs.send(method, *args, &block)
end

Instance Attribute Details

#attrsObject

Returns Node’s attributes.

Returns:

  • Node’s attributes

Since:

  • 0.1.6



55
56
57
# File 'lib/graph.rb', line 55

def attrs
  @attrs
end

Instance Method Details

#==(other) ⇒ Boolean

compare two nodes

Parameters:

Returns:

  • (Boolean)

Since:

  • 0.1.6



66
67
68
69
70
# File 'lib/graph.rb', line 66

def ==(other)
    return false if !other.is_a?(Node)

    @attrs == other.attrs
end

#update(h) ⇒ Node

Update the current node, like the Hash#update method.

Parameters:

  • h (Hash)

Returns:

Since:

  • 0.1.6



75
76
77
# File 'lib/graph.rb', line 75

def update(h)
    Node.new super(h)
end