Class: PgMeta::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/pg_meta/dump.rb,
lib/pg_meta/meta.rb

Direct Known Subclasses

Column, Constraint, Database, Function, Schema, Table, Trigger

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, name) ⇒ Node

Returns a new instance of Node.



23
24
25
26
27
28
# File 'lib/pg_meta/meta.rb', line 23

def initialize(parent, name)
  @root = parent&.root || self
  @parent = parent
  @name = name
  parent && parent.root.send(:add_node, self) # Add object to global database lookup
end

Instance Attribute Details

#nameObject (readonly)

Unique name within parent context. This is usually what we understand as the name of an object but functions have the full signature as “name”



13
14
15
# File 'lib/pg_meta/meta.rb', line 13

def name
  @name
end

#parentObject (readonly)

Parent object. nil for Database objects



9
10
11
# File 'lib/pg_meta/meta.rb', line 9

def parent
  @parent
end

#rootObject (readonly)

Database object. Equal to self for Database objects



6
7
8
# File 'lib/pg_meta/meta.rb', line 6

def root
  @root
end

Instance Method Details

#dumpObject



6
7
8
# File 'lib/pg_meta/dump.rb', line 6

def dump
  dump_value(to_h)
end

#dump_value(v) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/pg_meta/dump.rb', line 10

def dump_value(v)
  case v
    when Hash
      puts "{"
      indent {
        v.each { |k,v| 
          print "#{k}: "
          dump_value(v)
        }
      }
      puts "}"
    when []
      puts "[]"
    when Array
      puts "["
      indent {
        v.each { |v| 
          dump_value(v)
        }
      }
      puts "]"
  else
    puts (v.nil? ? "nil" : v)
  end
end

#guidObject



19
20
21
# File 'lib/pg_meta/meta.rb', line 19

def guid()
  @guid ||= [parent.guid, name].compact.join(".")
end

#inspectObject



33
# File 'lib/pg_meta/meta.rb', line 33

def inspect() "#<#{self.class}:#{guid}>" end

#to_hObject

Raises:

  • (StandardError)


30
# File 'lib/pg_meta/meta.rb', line 30

def to_h() raise StandardError, "Undefined method" end

#to_yamlObject



31
# File 'lib/pg_meta/meta.rb', line 31

def to_yaml() to_h.to_yaml end

#uidObject



15
16
17
# File 'lib/pg_meta/meta.rb', line 15

def uid()
  @uid ||= [parent.uid, name].compact.join(".")
end