Created
October 11, 2015 13:42
-
-
Save tinbka/f02edf245bb8f0d118ab to your computer and use it in GitHub Desktop.
Dynamic config over RailsConfig and ActiveRecord.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# The MIT License (MIT) | |
# | |
# Copyright (c) 2015 <tinbka@gmail.com> | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in | |
# all copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
# THE SOFTWARE. | |
# n.b. in this snippet I used `Setting` as ORM model, and `Settings` as RailsConfig.const_name | |
# Here is minimal definition of an underlying table | |
ActiveRecord::Schema.define do | |
create_table "settings" do | |
t.string "param" | |
t.text "value" | |
end | |
end | |
# Every config (incl. rails_config) parameter is accessible via `Setting[name]`, e.g. `Setting['int.app.uuid']` | |
# Furthermore, if the record named 'int.app.uuid' exists inside `settings` table inside DB, | |
# then it's `value` would be used instead, e.g.: | |
# Setting.create(param: "path.to.key", value: 'whatever') | |
# corresponds to rails_config's yml-file with the next content: | |
# path: | |
# to: | |
# key: whatever | |
# Default way of obtaining a parameter value `Settings.path.to.param` still does not interact with DB. | |
class Setting < ActiveRecord::Base | |
RailsConfig = Object.const_get ::RailsConfig.const_name | |
validates_uniqueness_of :param | |
class << self | |
def from_rails_config(param) | |
settings = param.split('.').reduce(RailsConfig) {|_, i| _ && _[i]} | |
if settings.is_a? ::RailsConfig::Options | |
return settings.to_hash.with_indifferent_access | |
end | |
settings | |
end | |
def cast_value(val) | |
case val | |
when 'false'; false | |
when 'true'; true | |
when 'nil'; nil | |
when /\Ad+\z/; val.to_i | |
when /\Ad+\.\d+\z/; val.to_f | |
else val # в т.ч. пустая строка | |
end | |
end | |
def [](param, default=nil) | |
if setting = table_exists? && where(param: param).first | |
return cast_value(setting.value) | |
end | |
rc_setting = from_rails_config(param) | |
unless rc_setting.is_a? Hash or rc_setting.nil? | |
return rc_setting | |
end | |
if settings = table_exists? && where('param LIKE ?', "#{param}.%").to_a.presence | |
branch = {} | |
settings.each {|s| | |
path = s.param[param.size+1..-1].split('.') | |
path.reduce(branch) {|_, i| _[i] ||= i.equal?(path.last) ? cast_value(s.value) : {}} | |
} | |
if rc_setting.is_a? Hash | |
branch = rc_setting.deep_merge branch | |
end | |
return branch | |
end | |
if rc_setting.is_a? Hash | |
return rc_setting | |
end | |
default | |
end | |
alias value [] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment