class Setting
Constants
- VERSION
Attributes
available_settings[R]
Public Class Methods
[](value)
click to toggle source
available_settings()
click to toggle source
DEPRECATED: Please use method accessors
instead.
# File lib/setting.rb, line 94 def available_settings instance.available_settings end
load(**args)
click to toggle source
This method can be called only once.
Parameter hash looks like this:
{ :files => [ "file1.yml", "file2.yml", ...], :path => "/var/www/apps/my-app/current/config/settings", :local => true }
If :local => true is set, we will load all *.yml files under :path/local directory after all files in :files have been loaded. “Local” settings thus take precedence by design. See README for more details.
# File lib/setting.rb, line 45 def load(**args) raise AlreadyLoaded, 'Settings already loaded' if instance.loaded? instance.load(**args) end
method_missing(method, *args)
click to toggle source
In Method invocation syntax we collapse Hash
values and return a single value if 'default' is found among keys or Hash
has only one key/value pair.
For example, if the YML data is: tax:
default: 0.0 california: 7.5
Then calling Setting.tax returns “0.0”“
This is the preferred method of using settings class.
# File lib/setting.rb, line 68 def method_missing(method, *args) instance.value_for(method, args) do |v, args| instance.collapse_hashes(v, args) end end
new()
click to toggle source
Instance Methods
# File lib/setting.rb, line 101 def initialize @available_settings = {} end
reload(**args)
click to toggle source
# File lib/setting.rb, line 51 def reload(**args) instance.load(**args) end
respond_to_missing?()
click to toggle source
# File lib/setting.rb, line 74 def respond_to_missing? true end
Public Instance Methods
collapse_hashes(v, args)
click to toggle source
This method performs collapsing of the Hash
settings values if the Hash
contains 'default' value, or just 1 element.
# File lib/setting.rb, line 140 def collapse_hashes(v, args) out = if v.is_a?(Hash) if args.empty? if v.key?("default") v['default'].nil? ? "" : v['default'] elsif v.keys.size == 1 v.values.first else v end else v[args.shift.to_s] end else v end if out.is_a?(Hash) && !args.empty? collapse_hashes(out, args) elsif out.is_a?(Hash) && out.key?('default') out['default'] else out end end
key?(key)
click to toggle source
@param [Object] key
# File lib/setting.rb, line 106 def key?(key) @available_settings.key?(key) || (key[-1, 1] == '?' && @available_settings.key?(key.chop)) end
Also aliased as: has_key?
load(**params)
click to toggle source
# File lib/setting.rb, line 169 def load(**params) # reset settings hash @available_settings = {} @loaded = false files = [] path = params[:path] || Dir.pwd params[:files].each do |file| files << File.join(path, file) end if params[:local] files << Dir.glob(File.join(path, 'local', '*.yml')).sort end files.flatten.each do |file| if File.exist?(file) @available_settings.recursive_merge! load_file(file) end rescue StandardError => e raise FileError, "Error parsing file #{file}, with: #{e.message}" end @loaded = true @available_settings end
loaded?()
click to toggle source
# File lib/setting.rb, line 165 def loaded? @loaded end
value_for(key, args = []) { |v, args| ... }
click to toggle source
# File lib/setting.rb, line 113 def value_for(key, args = []) name = key.to_s unless key?(name) raise NotFound, "#{name} was not found" end bool = false if name[-1, 1] == '?' name.chop! bool = true end v = @available_settings[name] if block_given? v = yield(v, args) end if v.is_a?(Integer) && bool v.to_i > 0 else v end end
Private Instance Methods
load_file(file)
click to toggle source
# File lib/setting.rb, line 197 def load_file(file) ::YAML.load( ::ERB.new(::IO.read(file)).result, fallback: {}, ) end