Class: Pgtk::Wire::Yaml

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

Overview

Using configuration from YAML file.

Author

Yegor Bugayenko ([email protected])

Copyright

Copyright © 2019-2025 Yegor Bugayenko

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(file, node = 'pgsql') ⇒ Yaml

Constructor.

Parameters:

  • file (String)

    Path to the YAML configuration file

  • node (String) (defaults to: 'pgsql')

    The root node name in the YAML file containing PostgreSQL configuration



91
92
93
94
95
96
# File 'lib/pgtk/wire.rb', line 91

def initialize(file, node = 'pgsql')
  raise "The name of the file can't be nil" if file.nil?
  @file = file
  raise "The name of the node in the YAML file can't be nil" if node.nil?
  @node = node
end

Instance Method Details

#connectionObject

Create a new connection to PostgreSQL server.



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/pgtk/wire.rb', line 99

def connection
  raise "The file #{@file.inspect} not found" unless File.exist?(@file)
  cfg = YAML.load_file(@file)
  raise "The node '#{@node}' not found in YAML file #{@file.inspect}" unless cfg[@node]
  Pgtk::Wire::Direct.new(
    host: cfg[@node]['host'],
    port: cfg[@node]['port'],
    dbname: cfg[@node]['dbname'],
    user: cfg[@node]['user'],
    password: cfg[@node]['password']
  ).connection
end