Class: MetaDb::DbObject

Inherits:
Object
  • Object
show all
Defined in:
lib/meta_db/dump.rb,
lib/meta_db/db_object.rb

Direct Known Subclasses

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent, name) ⇒ DbObject

Returns a new instance of DbObject.



35
36
37
38
39
# File 'lib/meta_db/db_object.rb', line 35

def initialize(parent, name)
  @name, @parent = name, parent
  @children = {}
  parent&.send(:attach, self)
end

Instance Attribute Details

#childrenObject (readonly)

Hash from name to contained objects



33
34
35
# File 'lib/meta_db/db_object.rb', line 33

def children
  @children
end

#nameObject (readonly)

Name of object. Unique within contianing object. Not nil



27
28
29
# File 'lib/meta_db/db_object.rb', line 27

def name
  @name
end

#parentObject (readonly)

Parent DbObject. Non-nil except for the top-level MetaDb::Database object



30
31
32
# File 'lib/meta_db/db_object.rb', line 30

def parent
  @parent
end

Class Method Details

.attrs(*attrs) ⇒ Object

Used to multi-assign values and for dump

TODO: rename db_attr or ‘column’, and create read/writer methods



9
10
11
12
13
14
15
16
# File 'lib/meta_db/db_object.rb', line 9

def self.attrs(*attrs)
  attrs = Array(attrs)
  if attrs.empty?
    @attrs
  else
    @attrs = attrs
  end
end

.init(row) ⇒ Object



18
19
20
21
22
# File 'lib/meta_db/db_object.rb', line 18

def self.init(row)
  h = []
  self.attrs.each { |a| h << row[a] if row.key?(a) }
  self.new(*h)
end

Instance Method Details

#<=>(r) ⇒ Object

Compare two objects by identity



66
# File 'lib/meta_db/db_object.rb', line 66

def <=>(r) path <=> r.path end

#[](name) ⇒ Object

Return child object with the given name



55
# File 'lib/meta_db/db_object.rb', line 55

def [](name) @children[name] end

#dot(path, strip: false) ⇒ Object

Recursively lookup object by dot-separated list of names. If strip is true, the first element will be stripped from the path



59
60
61
62
63
# File 'lib/meta_db/db_object.rb', line 59

def dot(path, strip: false)
  elems = path.split(".")
  elems.shift if strip
  elems.inject(self) { |a,e| a[e] } or raise "Can't lookup '#{path}' in #{self.path}"
end

#dumpObject



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

def dump
  puts self.class.to_s
  dump_attrs
end

#dump_attrs(*attrs) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/meta_db/dump.rb', line 11

def dump_attrs(*attrs)
  attrs = Array(attrs)
  attrs = self.class.attrs if attrs.empty?
  indent {
    for attr in Array(attrs)
      value = self.send(attr)
      case value
        when Array
          puts "#{attr}:"
          indent { value.each { |v| v.dump } }
        when DbObject
          puts "#{attr}: #{value.name} (#{value.class})"
      else
        puts "#{attr}: #{self.send(attr).inspect}"
      end
    end
  }
end

#inspectObject

Mostly for debug



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/meta_db/db_object.rb', line 69

def inspect
  string = StringIO.new
  stdout = $stdout
  begin
    $stdout = string
    dump
  ensure
    $stdout = stdout
  end
  string.string
end

#path(strip: false) ⇒ Object

Unique dot-separated list of names leading from the top-level MetaDb::Database object to self. Omits the first path-element if strip is true



43
44
45
46
47
48
49
50
51
52
# File 'lib/meta_db/db_object.rb', line 43

def path(strip: false)
  r = []
  node = self
  while node
    r << node.name
    node = node.parent
  end
  r.pop if strip
  r.reverse.compact.join(".")
end