Module: SettingAccessors::SettingScaffold::ClassMethods
- Defined in:
- lib/setting_accessors/setting_scaffold.rb
Instance Method Summary collapse
-
#[]=(name, *args) ⇒ Object
An alias for #set with a slightly different API.
-
#get(name, assignable = nil) ⇒ Object, NilClass
(also: #[])
Searches for a setting in the database and returns its value.
-
#get_default_value(name, assignable = nil) ⇒ Object
The default value for the given setting.
-
#get_or_default(name, assignable) ⇒ Object
Tries to look the setting up using #get, if no existing setting is found, the setting’s default value is returned.
-
#method_missing(method, *args, &block) ⇒ Object
Makes accessing settings a little easier.
-
#set(name, value, assignable: nil) ⇒ Object
Creates or updates the setting with the given name.
-
#setting_record(name, assignable = nil) ⇒ Setting, NilClass
Looks up a setting record for the given name and assignable Unlike the other methods here, this one actually returns a Setting object instead of its value.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
Makes accessing settings a little easier. Examples:
#Loading **the value** of a global setting named "my_setting"
Setting.my_setting
#Setting **the value** of a global setting named "my_setting"
Setting.my_setting = [1,2,3,4,5]
#Loading **the value** of an assigned setting named "cool_setting"
#+some_cool_user+ is here an instance of ActiveRecord::Base
Setting.cool_setting(some_cool_user)
125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/setting_accessors/setting_scaffold.rb', line 125 def method_missing(method, *args, &block) method_name = method.to_s if method_name.last == '=' return set(method_name[0..-2], args.first) elsif args.size <= 1 return get(method_name, args.first) end super end |
Instance Method Details
#[]=(name, *args) ⇒ Object
An alias for #set with a slightly different API. This allows the following usage:
Setting['my_setting', my_assignable] ||= new_value
88 89 90 91 |
# File 'lib/setting_accessors/setting_scaffold.rb', line 88 def []=(name, *args) assignable = args.size > 1 ? args.first : nil set(name, args.last, assignable: assignable) end |
#get(name, assignable = nil) ⇒ Object, NilClass Also known as: []
Searches for a setting in the database and returns its value
36 37 38 |
# File 'lib/setting_accessors/setting_scaffold.rb', line 36 def get(name, assignable = nil) setting_record(name, assignable).try(:value) end |
#get_default_value(name, assignable = nil) ⇒ Object
Returns the default value for the given setting.
96 97 98 |
# File 'lib/setting_accessors/setting_scaffold.rb', line 96 def get_default_value(name, assignable = nil) new(name: name, assignable: assignable).default_value end |
#get_or_default(name, assignable) ⇒ Object
Tries to look the setting up using #get, if no existing setting is found, the setting’s default value is returned.
This only works for class-wise settings, meaning that an assignable has to be present.
48 49 50 51 52 53 54 |
# File 'lib/setting_accessors/setting_scaffold.rb', line 48 def get_or_default(name, assignable) if (val = get(name, assignable)).nil? new(name: name, assignable: assignable).default_value else val end end |
#set(name, value, assignable: nil) ⇒ Object
Creates or updates the setting with the given name
@toto: Bless the rains down in Africa!
76 77 78 79 80 81 |
# File 'lib/setting_accessors/setting_scaffold.rb', line 76 def set(name, value, assignable: nil) (setting_record(name, assignable) || new(name: name, assignable: assignable)).tap do |setting| setting.raw_value = value setting.save end.value end |
#setting_record(name, assignable = nil) ⇒ Setting, NilClass
Looks up a setting record for the given name and assignable Unlike the other methods here, this one actually returns a Setting object instead of its value.
107 108 109 |
# File 'lib/setting_accessors/setting_scaffold.rb', line 107 def setting_record(name, assignable = nil) find_by(name: name.to_s, assignable: assignable) end |