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.



31
32
33
34
35
36
# File 'lib/pg_meta/meta.rb', line 31

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”, eg. ‘func(integer)’



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

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. TODO: Rename ‘database’



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

Unique id within a RDBMS



27
28
29
# File 'lib/pg_meta/meta.rb', line 27

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

#inspectObject



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

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

#sidObject

Unique id within a schema. Only tables, columns, and functions have a sid



17
18
19
# File 'lib/pg_meta/meta.rb', line 17

def sid()
  @sid = nil
end

#to_hObject

Raises:

  • (StandardError)


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

def to_h = raise StandardError, "Undefined method"

#to_sObject



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

def to_s = name.to_s

#to_yamlObject



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

def to_yaml = to_h.to_yaml

#uidObject

Unique id within a database. The database object itself has a nil uid



22
23
24
# File 'lib/pg_meta/meta.rb', line 22

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