Class: BarkestCore::DatabaseConfig
- Inherits:
-
Object
- Object
- BarkestCore::DatabaseConfig
- Includes:
- ActiveModel::Model, ActiveModel::Validations
- Defined in:
- app/models/barkest_core/database_config.rb
Overview
Defines a database configuration for a database other than the core database.
The core database must be configurared in database.yml
since it defines the SystemConfig.
Other databases (say for a 3rd party database) can easily be defined using SystemConfig and this class.
Constant Summary collapse
- VALID_ADAPTERS =
%w(sqlite3 mysql2 postgresql sqlserver)
Instance Attribute Summary collapse
-
#adapter ⇒ Object
Returns the value of attribute adapter.
-
#database ⇒ Object
Returns the value of attribute database.
-
#encoding ⇒ Object
Returns the value of attribute encoding.
-
#extra_1_label ⇒ Object
Returns the value of attribute extra_1_label.
-
#extra_1_name ⇒ Object
Returns the value of attribute extra_1_name.
-
#extra_1_type ⇒ Object
Returns the value of attribute extra_1_type.
-
#extra_1_value ⇒ Object
writeonly
Sets the attribute extra_1_value.
-
#extra_2_label ⇒ Object
Returns the value of attribute extra_2_label.
-
#extra_2_name ⇒ Object
Returns the value of attribute extra_2_name.
-
#extra_2_type ⇒ Object
Returns the value of attribute extra_2_type.
-
#extra_2_value ⇒ Object
writeonly
Sets the attribute extra_2_value.
-
#extra_3_label ⇒ Object
Returns the value of attribute extra_3_label.
-
#extra_3_name ⇒ Object
Returns the value of attribute extra_3_name.
-
#extra_3_type ⇒ Object
Returns the value of attribute extra_3_type.
-
#extra_3_value ⇒ Object
writeonly
Sets the attribute extra_3_value.
-
#extra_4_label ⇒ Object
Returns the value of attribute extra_4_label.
-
#extra_4_name ⇒ Object
Returns the value of attribute extra_4_name.
-
#extra_4_type ⇒ Object
Returns the value of attribute extra_4_type.
-
#extra_4_value ⇒ Object
writeonly
Sets the attribute extra_4_value.
-
#extra_5_label ⇒ Object
Returns the value of attribute extra_5_label.
-
#extra_5_name ⇒ Object
Returns the value of attribute extra_5_name.
-
#extra_5_type ⇒ Object
Returns the value of attribute extra_5_type.
-
#extra_5_value ⇒ Object
writeonly
Sets the attribute extra_5_value.
-
#host ⇒ Object
Returns the value of attribute host.
-
#name ⇒ Object
Returns the value of attribute name.
-
#password ⇒ Object
Returns the value of attribute password.
-
#pool ⇒ Object
Returns the value of attribute pool.
-
#port ⇒ Object
Returns the value of attribute port.
-
#reconnect ⇒ Object
Returns the value of attribute reconnect.
-
#timeout ⇒ Object
Returns the value of attribute timeout.
-
#update_password ⇒ Object
Returns the value of attribute update_password.
-
#update_username ⇒ Object
Returns the value of attribute update_username.
-
#username ⇒ Object
Returns the value of attribute username.
Class Method Summary collapse
-
.load(name) ⇒ Object
Loads the configuration for the specified database from SystemConfig.
-
.register(db_id) ⇒ Object
Registers a database for enumeration.
-
.registered ⇒ Object
Gets a list of the registered databases for enumeration.
Instance Method Summary collapse
-
#extra_field_options(index) ⇒ Object
Gets the options for a select field.
-
#extra_field_type(index) ⇒ Object
Gets the field type for an extra value.
-
#extra_label(index) ⇒ Object
Gets the label for an extra value.
-
#extra_name(index) ⇒ Object
Gets the name for an extra value.
-
#extra_type(index) ⇒ Object
Gets the type for an extra value.
-
#extra_value(index, convert = false) ⇒ Object
Gets an extra value.
-
#initialize(*args) ⇒ DatabaseConfig
constructor
Initializes the configuration.
-
#method_missing(m, *a, &b) ⇒ Object
:nodoc:.
-
#reconnect? ⇒ Boolean
Is the database configured to reconnect?.
-
#save ⇒ Object
Saves this configuration (encrypted) to SystemConfig.
-
#to_h(convert_extra = true) ⇒ Object
Converts this configuration into a hash.
Constructor Details
#initialize(*args) ⇒ DatabaseConfig
Initializes the configuration.
Define the parameters as hash values. A string without a key will be used to set the name.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'app/models/barkest_core/database_config.rb', line 39 def initialize(*args) @extra = [] args.each do |arg| if arg.is_a?(String) self.name = arg elsif arg.is_a?(Hash) arg.each do |k,v| if respond_to?(:"#{k}?") send :"#{k}=", ((v === true || v === '1') ? '1' : '0') elsif respond_to?(:"#{k}") send :"#{k}=", v.to_s elsif (extra = EXTRA_REGEX.match(k.to_s)) key = extra['KEY'].to_i if VALID_EXTRA_KEY.include?(key) send :"extra_#{key}_#{extra['VAR']}=", v.to_s end end end end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(m, *a, &b) ⇒ Object
:nodoc:
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'app/models/barkest_core/database_config.rb', line 62 def method_missing(m,*a,&b) m = m.to_s if (key = EXTRA_REGEX.match(m)) if key['VAR'] == 'value' key = key['KEY'].to_i if VALID_EXTRA_KEY.include?(key) ivar = :"@#{m}" val = instance_variable_defined?(ivar) ? instance_variable_get(ivar) : nil tp = send("extra_#{key}_type") if tp == 'boolean' val = ((val === true) || (val == '1')) ? '1' : '0' end return val end end end super m, *a, &b end |
Instance Attribute Details
#adapter ⇒ Object
Returns the value of attribute adapter.
14 15 16 |
# File 'app/models/barkest_core/database_config.rb', line 14 def adapter @adapter end |
#database ⇒ Object
Returns the value of attribute database.
14 15 16 |
# File 'app/models/barkest_core/database_config.rb', line 14 def database @database end |
#encoding ⇒ Object
Returns the value of attribute encoding.
14 15 16 |
# File 'app/models/barkest_core/database_config.rb', line 14 def encoding @encoding end |
#extra_1_label ⇒ Object
Returns the value of attribute extra_1_label.
15 16 17 |
# File 'app/models/barkest_core/database_config.rb', line 15 def extra_1_label @extra_1_label end |
#extra_1_name ⇒ Object
Returns the value of attribute extra_1_name.
15 16 17 |
# File 'app/models/barkest_core/database_config.rb', line 15 def extra_1_name @extra_1_name end |
#extra_1_type ⇒ Object
Returns the value of attribute extra_1_type.
15 16 17 |
# File 'app/models/barkest_core/database_config.rb', line 15 def extra_1_type @extra_1_type end |
#extra_1_value=(value) ⇒ Object (writeonly)
Sets the attribute extra_1_value
20 21 22 |
# File 'app/models/barkest_core/database_config.rb', line 20 def extra_1_value=(value) @extra_1_value = value end |
#extra_2_label ⇒ Object
Returns the value of attribute extra_2_label.
16 17 18 |
# File 'app/models/barkest_core/database_config.rb', line 16 def extra_2_label @extra_2_label end |
#extra_2_name ⇒ Object
Returns the value of attribute extra_2_name.
16 17 18 |
# File 'app/models/barkest_core/database_config.rb', line 16 def extra_2_name @extra_2_name end |
#extra_2_type ⇒ Object
Returns the value of attribute extra_2_type.
16 17 18 |
# File 'app/models/barkest_core/database_config.rb', line 16 def extra_2_type @extra_2_type end |
#extra_2_value=(value) ⇒ Object (writeonly)
Sets the attribute extra_2_value
20 21 22 |
# File 'app/models/barkest_core/database_config.rb', line 20 def extra_2_value=(value) @extra_2_value = value end |
#extra_3_label ⇒ Object
Returns the value of attribute extra_3_label.
17 18 19 |
# File 'app/models/barkest_core/database_config.rb', line 17 def extra_3_label @extra_3_label end |
#extra_3_name ⇒ Object
Returns the value of attribute extra_3_name.
17 18 19 |
# File 'app/models/barkest_core/database_config.rb', line 17 def extra_3_name @extra_3_name end |
#extra_3_type ⇒ Object
Returns the value of attribute extra_3_type.
17 18 19 |
# File 'app/models/barkest_core/database_config.rb', line 17 def extra_3_type @extra_3_type end |
#extra_3_value=(value) ⇒ Object (writeonly)
Sets the attribute extra_3_value
20 21 22 |
# File 'app/models/barkest_core/database_config.rb', line 20 def extra_3_value=(value) @extra_3_value = value end |
#extra_4_label ⇒ Object
Returns the value of attribute extra_4_label.
18 19 20 |
# File 'app/models/barkest_core/database_config.rb', line 18 def extra_4_label @extra_4_label end |
#extra_4_name ⇒ Object
Returns the value of attribute extra_4_name.
18 19 20 |
# File 'app/models/barkest_core/database_config.rb', line 18 def extra_4_name @extra_4_name end |
#extra_4_type ⇒ Object
Returns the value of attribute extra_4_type.
18 19 20 |
# File 'app/models/barkest_core/database_config.rb', line 18 def extra_4_type @extra_4_type end |
#extra_4_value=(value) ⇒ Object (writeonly)
Sets the attribute extra_4_value
20 21 22 |
# File 'app/models/barkest_core/database_config.rb', line 20 def extra_4_value=(value) @extra_4_value = value end |
#extra_5_label ⇒ Object
Returns the value of attribute extra_5_label.
19 20 21 |
# File 'app/models/barkest_core/database_config.rb', line 19 def extra_5_label @extra_5_label end |
#extra_5_name ⇒ Object
Returns the value of attribute extra_5_name.
19 20 21 |
# File 'app/models/barkest_core/database_config.rb', line 19 def extra_5_name @extra_5_name end |
#extra_5_type ⇒ Object
Returns the value of attribute extra_5_type.
19 20 21 |
# File 'app/models/barkest_core/database_config.rb', line 19 def extra_5_type @extra_5_type end |
#extra_5_value=(value) ⇒ Object (writeonly)
Sets the attribute extra_5_value
20 21 22 |
# File 'app/models/barkest_core/database_config.rb', line 20 def extra_5_value=(value) @extra_5_value = value end |
#host ⇒ Object
Returns the value of attribute host.
14 15 16 |
# File 'app/models/barkest_core/database_config.rb', line 14 def host @host end |
#name ⇒ Object
Returns the value of attribute name.
14 15 16 |
# File 'app/models/barkest_core/database_config.rb', line 14 def name @name end |
#password ⇒ Object
Returns the value of attribute password.
14 15 16 |
# File 'app/models/barkest_core/database_config.rb', line 14 def password @password end |
#pool ⇒ Object
Returns the value of attribute pool.
14 15 16 |
# File 'app/models/barkest_core/database_config.rb', line 14 def pool @pool end |
#port ⇒ Object
Returns the value of attribute port.
14 15 16 |
# File 'app/models/barkest_core/database_config.rb', line 14 def port @port end |
#reconnect ⇒ Object
Returns the value of attribute reconnect.
14 15 16 |
# File 'app/models/barkest_core/database_config.rb', line 14 def reconnect @reconnect end |
#timeout ⇒ Object
Returns the value of attribute timeout.
14 15 16 |
# File 'app/models/barkest_core/database_config.rb', line 14 def timeout @timeout end |
#update_password ⇒ Object
Returns the value of attribute update_password.
21 22 23 |
# File 'app/models/barkest_core/database_config.rb', line 21 def update_password @update_password end |
#update_username ⇒ Object
Returns the value of attribute update_username.
21 22 23 |
# File 'app/models/barkest_core/database_config.rb', line 21 def update_username @update_username end |
#username ⇒ Object
Returns the value of attribute username.
14 15 16 |
# File 'app/models/barkest_core/database_config.rb', line 14 def username @username end |
Class Method Details
.load(name) ⇒ Object
Loads the configuration for the specified database from SystemConfig.
202 203 204 |
# File 'app/models/barkest_core/database_config.rb', line 202 def DatabaseConfig.load(name) DatabaseConfig.new(name, SystemConfig.get(name)) end |
.register(db_id) ⇒ Object
Registers a database for enumeration.
214 215 216 217 218 219 220 |
# File 'app/models/barkest_core/database_config.rb', line 214 def DatabaseConfig.register(db_id) db_id = db_id.to_s.downcase unless db_id.blank? registered << db_id unless registered.include?(db_id) end nil end |
.registered ⇒ Object
Gets a list of the registered databases for enumeration.
208 209 210 |
# File 'app/models/barkest_core/database_config.rb', line 208 def DatabaseConfig.registered @registered ||= [] end |
Instance Method Details
#extra_field_options(index) ⇒ Object
Gets the options for a select field.
134 135 136 137 138 139 140 |
# File 'app/models/barkest_core/database_config.rb', line 134 def (index) if extra_field_type(index) == 'select' eval extra_type(index).partition(':')[2] else nil end end |
#extra_field_type(index) ⇒ Object
Gets the field type for an extra value.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'app/models/barkest_core/database_config.rb', line 114 def extra_field_type(index) t = extra_type(index).to_s case t when 'password' 'password' when 'integer', 'float' 'number' when 'boolean' 'checkbox' else if t.downcase.index('in:') 'select' else 'text' end end end |
#extra_label(index) ⇒ Object
Gets the label for an extra value.
98 99 100 101 102 103 |
# File 'app/models/barkest_core/database_config.rb', line 98 def extra_label(index) return nil if index < 1 || index > 5 txt = send("extra_#{index}_label") txt = extra_name(index).to_s.humanize.capitalize if txt.blank? txt end |
#extra_name(index) ⇒ Object
Gets the name for an extra value.
91 92 93 94 |
# File 'app/models/barkest_core/database_config.rb', line 91 def extra_name(index) return nil if index < 1 || index > 5 send "extra_#{index}_name" end |
#extra_type(index) ⇒ Object
Gets the type for an extra value.
107 108 109 110 |
# File 'app/models/barkest_core/database_config.rb', line 107 def extra_type(index) return nil if index < 1 || index > 5 send "extra_#{index}_type" end |
#extra_value(index, convert = false) ⇒ Object
Gets an extra value.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'app/models/barkest_core/database_config.rb', line 144 def extra_value(index, convert = false) return nil if index < 1 || index > 5 val = send "extra_#{index}_value" if convert case extra_type(index) when 'boolean' BarkestCore::BooleanParser.parse_for_boolean_column(val) when 'integer' BarkestCore::NumberParser.parse_for_int_column(val) when 'float' BarkestCore::NumberParser.parse_for_float_column(val) else val.to_s end end end |
#reconnect? ⇒ Boolean
Is the database configured to reconnect?
85 86 87 |
# File 'app/models/barkest_core/database_config.rb', line 85 def reconnect? reconnect.to_s.to_i != 0 end |
#save ⇒ Object
Saves this configuration (encrypted) to SystemConfig.
196 197 198 |
# File 'app/models/barkest_core/database_config.rb', line 196 def save SystemConfig.set name, to_h(false), true end |
#to_h(convert_extra = true) ⇒ Object
Converts this configuration into a hash.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'app/models/barkest_core/database_config.rb', line 164 def to_h(convert_extra = true) ret = { adapter: adapter.to_s, database: database.to_s, pool: pool.to_s.to_i, timeout: timeout.to_s.to_i, reconnect: reconnect?, encoding: encoding ? encoding.to_s : nil, host: host.blank? ? nil : host.to_s, port: port.blank? ? nil : port.to_s.to_i, username: username.blank? ? nil : username.to_s, password: password.blank? ? nil : password.to_s, update_username: update_username.blank? ? nil : update_username.to_s, update_password: update_password.blank? ? nil : update_password.to_s, } VALID_EXTRA_KEY.each do |idx| if convert_extra unless extra_name(idx).blank? ret[extra_name(idx).to_sym] = extra_value(idx, true) end else ret[:"extra_#{idx}_name"] = send(:"extra_#{idx}_name") ret[:"extra_#{idx}_label"] = send(:"extra_#{idx}_label") ret[:"extra_#{idx}_type"] = send(:"extra_#{idx}_type") ret[:"extra_#{idx}_value"] = send(:"extra_#{idx}_value") end end ret end |