Class: SettingsHolder
- Inherits:
-
Object
- Object
- SettingsHolder
- Includes:
- Singleton
- Defined in:
- lib/settings_tree/settings_holder.rb
Overview
Part of the “rails-settings” gem, this class is designed to store several OpenStruct representing ‘settings’. It can load them from a YAML file, reload them, and accept queries.
This class is usually not used directly but through the ‘settings’ module, offering shortcuts.
Instance Attribute Summary collapse
-
#environment ⇒ Object
Returns the value of attribute environment.
Instance Method Summary collapse
-
#debug_inspect ⇒ Object
a convenience function which displays the datas.
-
#get_settings(name) ⇒ Object
Return this group of settings as an openstruct.
-
#register_settings_file(name, file) ⇒ Object
Register a source file for a group.
- #reload_all ⇒ Object
- #reload_group(name) ⇒ Object
-
#reset ⇒ Object
a convenience function which reset the settings, forgetting about all the groups, files, etc.
-
#set_environment(env) ⇒ Object
Set the environment to be used for conditional settings.
Instance Attribute Details
#environment ⇒ Object
Returns the value of attribute environment.
24 25 26 |
# File 'lib/settings_tree/settings_holder.rb', line 24 def environment @environment end |
Instance Method Details
#debug_inspect ⇒ Object
a convenience function which displays the datas
35 36 37 38 39 40 41 42 |
# File 'lib/settings_tree/settings_holder.rb', line 35 def debug_inspect puts "*** Current settings :" #puts @settings_list.inspect @settings_list.each do |key, value| puts "Settings.#{key}." @settings_list[key][:data].debug_inspect(1) end end |
#get_settings(name) ⇒ Object
Return this group of settings as an openstruct
70 71 72 73 74 75 76 77 78 |
# File 'lib/settings_tree/settings_holder.rb', line 70 def get_settings(name) #puts name.inspect #puts @settings_list.inspect if !@settings_list.has_key?(name) then raise ArgumentError, "Settings : unknown settings group '#{name.to_s}'" else return @settings_list[name][:data] end end |
#register_settings_file(name, file) ⇒ Object
Register a source file for a group. The group will be created if not already existing.
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/settings_tree/settings_holder.rb', line 46 def register_settings_file(name, file) group_just_created = false # create the group if not already here unless has_group?(name) register_new_group(name) unless has_group?(name) group_just_created = true end # add this file as source res = false begin res = register_new_src_file_for_group(name, file) rescue Exception => e # delete group if just created ? Not for now. # proceed with exception raise e end return res end |
#reload_all ⇒ Object
92 93 94 95 96 |
# File 'lib/settings_tree/settings_holder.rb', line 92 def reload_all @settings_list.each do |key, value| reload_group(key) end end |
#reload_group(name) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/settings_tree/settings_holder.rb', line 98 def reload_group(name) res = false if !has_group?(name) then raise ArgumentError, "This group doesn't exist !" else data = Hash.new @settings_list[name.to_sym][:src].each do |src| data.deep_merge!(hash_data_for_src(src)) end @settings_list[name.to_sym][:data] = RecursiveOpenStruct.new(data) res = true end # check parameters return res end |
#reset ⇒ Object
a convenience function which reset the settings, forgetting about all the groups, files, etc.
30 31 32 |
# File 'lib/settings_tree/settings_holder.rb', line 30 def reset initialize end |
#set_environment(env) ⇒ Object
Set the environment to be used for conditional settings.
81 82 83 84 85 86 |
# File 'lib/settings_tree/settings_holder.rb', line 81 def set_environment(env) @environment = env # need to reload all reload_all end |