Class: DatabaseConfig
- Inherits:
-
Object
- Object
- DatabaseConfig
- Defined in:
- app/database_config.rb
Overview
Config for a single database.
Instance Attribute Summary collapse
-
#client_params ⇒ Object
readonly
Returns the value of attribute client_params.
-
#columns ⇒ Object
readonly
Returns the value of attribute columns.
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#display_name ⇒ Object
readonly
Returns the value of attribute display_name.
-
#joins ⇒ Object
readonly
Returns the value of attribute joins.
-
#saved_path ⇒ Object
readonly
Returns the value of attribute saved_path.
-
#tables ⇒ Object
readonly
Returns the value of attribute tables.
-
#url_path ⇒ Object
readonly
Returns the value of attribute url_path.
Instance Method Summary collapse
-
#initialize(hash) ⇒ DatabaseConfig
constructor
A new instance of DatabaseConfig.
- #with_client(&block) ⇒ Object
Constructor Details
#initialize(hash) ⇒ DatabaseConfig
Returns a new instance of DatabaseConfig.
13 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 |
# File 'app/database_config.rb', line 13 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 start with a /' unless @url_path.start_with?('/') raise ArgumentError, 'url_path should not end with a /' if @url_path.length > 1 && @url_path.end_with?('/') @saved_path = Args.fetch_non_empty_string(hash, :saved_path).strip # 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_params ⇒ Object (readonly)
Returns the value of attribute client_params.
11 12 13 |
# File 'app/database_config.rb', line 11 def client_params @client_params end |
#columns ⇒ Object (readonly)
Returns the value of attribute columns.
11 12 13 |
# File 'app/database_config.rb', line 11 def columns @columns end |
#description ⇒ Object (readonly)
Returns the value of attribute description.
11 12 13 |
# File 'app/database_config.rb', line 11 def description @description end |
#display_name ⇒ Object (readonly)
Returns the value of attribute display_name.
11 12 13 |
# File 'app/database_config.rb', line 11 def display_name @display_name end |
#joins ⇒ Object (readonly)
Returns the value of attribute joins.
11 12 13 |
# File 'app/database_config.rb', line 11 def joins @joins end |
#saved_path ⇒ Object (readonly)
Returns the value of attribute saved_path.
11 12 13 |
# File 'app/database_config.rb', line 11 def saved_path @saved_path end |
#tables ⇒ Object (readonly)
Returns the value of attribute tables.
11 12 13 |
# File 'app/database_config.rb', line 11 def tables @tables end |
#url_path ⇒ Object (readonly)
Returns the value of attribute url_path.
11 12 13 |
# File 'app/database_config.rb', line 11 def url_path @url_path end |
Instance Method Details
#with_client(&block) ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'app/database_config.rb', line 85 def with_client(&block) client = Mysql2::Client.new(@client_params) result = block.call(client) client.close client = nil result ensure client&.close end |