Class: DatabaseConfig

Inherits:
Object
  • Object
show all
Defined in:
app/database_config.rb

Overview

Config for a single database.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ DatabaseConfig

Returns a new instance of DatabaseConfig.

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/database_config.rb', line 14

def initialize(hash)
  @display_name = Args.fetch_non_empty_string(hash, :display_name).strip
  @description = Args.fetch_non_empty_string(hash, :description).strip
  @url_path = Args.fetch_non_empty_string(hash, :url_path).strip
  raise ArgumentError, 'url_path should not start with a /' if @url_path.start_with?('/')
  raise ArgumentError, 'url_path should not end with a /' if @url_path.end_with?('/')

  saved_config_hash = Args.fetch_optional_hash(hash, :saved_config)
  @saved_config = saved_config_hash.nil? ? nil : SavedConfig.new(saved_config_hash)

  # Make joins an array. It is only a map to allow for YAML extension.
  @joins = (Args.fetch_optional_hash(hash, :joins) || {}).values
  @joins.each do |join|
    next if join.is_a?(Hash) &&
            join.keys.size == 2 &&
            join[:label].is_a?(String) && !join[:label].strip.empty? &&
            join[:apply].is_a?(String) && !join[:apply].strip.empty?

    raise ArgumentError, "invalid join #{join.to_json}"
  end

  @tables = Args.fetch_optional_hash(hash, :tables) || {}
  @tables.each do |table, table_config|
    unless table_config.is_a?(Hash)
      raise ArgumentError, "invalid table config for #{table} (#{table_config}), expected hash"
    end

    table_alias = table_config[:alias]
    if table_alias && !table_alias.is_a?(String)
      raise ArgumentError, "invalid table alias for #{table} (#{table_alias}), expected string"
    end

    table_boost = table_config[:boost]
    if table_boost && !table_boost.is_a?(Integer)
      raise ArgumentError, "invalid table boost for #{table} (#{table_boost}), expected int"
    end
  end

  @columns = Args.fetch_optional_hash(hash, :columns) || {}
  @columns.each do |column, column_config|
    unless column_config.is_a?(Hash)
      raise ArgumentError, "invalid column config for #{column} (#{column_config}), expected hash"
    end

    links = Args.fetch_optional_hash(column_config, :links) || {}
    links.each_value do |link_config|
      unless link_config.is_a?(Hash)
        raise ArgumentError, "invalid link config for #{column} (#{link_config}), expected hash"
      end

      unless link_config[:short_name].is_a?(String)
        raise ArgumentError,
              "invalid link short_name for #{column} link (#{link_config[:short_name]}), expected string"
      end
      unless link_config[:long_name].is_a?(String)
        raise ArgumentError, "invalid link long_name for #{column} link (#{link_config[:long_name]}), expected string"
      end
      unless link_config[:template].is_a?(String)
        raise ArgumentError, "invalid link template for #{column} link (#{link_config[:template]}), expected string"
      end
    end
    # Make links an array. It is only a map to allow for YAML extension
    column_config[:links] = links.values
  end
  aliases = @tables.map { |_table, table_config| table_config[:alias] }.compact
  if aliases.to_set.size < aliases.size
    duplicate_aliases = aliases.reject { |a| aliases.count(a) == 1 }.to_set
    raise ArgumentError, "duplicate table aliases: #{duplicate_aliases.join(', ')}"
  end

  @client_params = Args.fetch_non_empty_hash(hash, :client_params)
end

Instance Attribute Details

#client_paramsObject (readonly)

Returns the value of attribute client_params.



12
13
14
# File 'app/database_config.rb', line 12

def client_params
  @client_params
end

#columnsObject (readonly)

Returns the value of attribute columns.



12
13
14
# File 'app/database_config.rb', line 12

def columns
  @columns
end

#descriptionObject (readonly)

Returns the value of attribute description.



12
13
14
# File 'app/database_config.rb', line 12

def description
  @description
end

#display_nameObject (readonly)

Returns the value of attribute display_name.



12
13
14
# File 'app/database_config.rb', line 12

def display_name
  @display_name
end

#joinsObject (readonly)

Returns the value of attribute joins.



12
13
14
# File 'app/database_config.rb', line 12

def joins
  @joins
end

#saved_configObject (readonly)

Returns the value of attribute saved_config.



12
13
14
# File 'app/database_config.rb', line 12

def saved_config
  @saved_config
end

#tablesObject (readonly)

Returns the value of attribute tables.



12
13
14
# File 'app/database_config.rb', line 12

def tables
  @tables
end

#url_pathObject (readonly)

Returns the value of attribute url_path.



12
13
14
# File 'app/database_config.rb', line 12

def url_path
  @url_path
end

Instance Method Details

#with_client(&block) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'app/database_config.rb', line 87

def with_client(&block)
  client = Mysql2::Client.new(@client_params)
  result = block.call(client)
  client.close
  client = nil
  result
ensure
  client&.close
end