You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Settingasm is my personal solution to application wide settings that uses an ERB enabled YAML file. It's pretty simple and straightforward, which is why I like it. I decided to go ahead and share it with the world.
4
+
5
+
So here is my question to you.....is Settingasm a great settings solution or the greatest?
6
+
7
+
== 1. Configure
8
+
9
+
Configuration is optional. See Settingasm::Config for more details.
10
+
11
+
# config/initializers/settingasm.rb
12
+
Settingasm::Config.configure do |config|
13
+
config.file_name = :config # will look for config/config.yml
14
+
config.file_name = "config" # will look for config
15
+
config.file_name = "config.yaml" # will look for confg.yaml
16
+
config.file_name = "/absolute/path/config.yml" # will look for /absolute/path/config.yaml
17
+
18
+
config.class_name = "Setting" # use Setting.my_setting instead of Settingasm::Setting.my_setting
I use Setting because I specified it in my configuration. Otherwise you would have to use Settingasm::Setting. Since Setting is such a generic name I let you specify your own in the configuration. You could use anything you want.
45
+
46
+
>> RAILS_ENV
47
+
=> "development"
48
+
49
+
>> Setting.cool
50
+
=> "this is cool"
51
+
52
+
>> Setting.cool.saweet
53
+
=> "nested settings"
54
+
55
+
>> Setting.new_setting
56
+
=> 800
57
+
58
+
>> Setting.awesome_setting
59
+
=> "Did you know 5 + 5 = 10?"
60
+
61
+
== Multiple settings
62
+
63
+
settings1 = Setting.new(:settings1) # looks for config/settings1.yml
64
+
settings2 = Setting.new("setting2.yaml") # looks for config/settings2.yml
# The name of the class you want to use to access your settings. Maybe you don't like "Setting" or maybe it will conflict with a model you have. Just set this to "AppSetting" or "Config", whatever you want.
11
+
#
12
+
# * <tt>Default:</tt> "Setting"
13
+
# * <tt>Accepts:</tt> Symbol or String
14
+
defclass_name
15
+
@class_name
16
+
end
17
+
18
+
defclass_name=(value)# :nodoc:
19
+
eval("::#{value} = Setting")
20
+
@class_name=value
21
+
end
22
+
23
+
# The name of the file that your settings will be stored for singleton access. Meaning the settings file that will be used when calling methods on the class level:
24
+
#
25
+
# Setting.setting1
26
+
# Setting.setting2
27
+
# # etc...
28
+
#
29
+
# All that you need to do is specify the name of the file. It will automatically look in the config directory.
# A simple settings solution using a YAML file. See README for more information.
5
+
classSetting
6
+
class << self
7
+
defname# :nodoc:
8
+
ifinstance._settings.key?("name")
9
+
instance.name
10
+
else
11
+
super
12
+
end
13
+
end
14
+
15
+
# Resets the singleton instance. Useful if you are changing the configuration on the fly. If you are changing the configuration on the fly you could consider creating instances.
16
+
defreset!
17
+
@instance=nil
18
+
end
19
+
20
+
private
21
+
definstance
22
+
@instance ||= new
23
+
end
24
+
25
+
defmethod_missing(name, *args, &block)
26
+
instance.send(name, *args, &block)
27
+
end
28
+
end
29
+
30
+
attr_accessor:_settings
31
+
32
+
# Initializes a new settings object. You can initialize an object in any of the following ways:
33
+
#
34
+
# Setting.new(:application) # will look for config/application.yml
35
+
# Setting.new("application.yaml") # will look for application.yaml
36
+
# Setting.new("/var/configs/application.yml") # will look for /var/configs/application.yml
37
+
# Setting.new(:config1 => 1, :config2 => 2)
38
+
#
39
+
# Basically if you pass a symbol it will look for that file in the configs directory of your rails app, if you are using this in rails. If you pass a string it should be an absolute path to your settings file.
40
+
# Then you can pass a hash, and it just allows you to access the hash via methods.
0 commit comments