Module: Sunrise::Models::Settings::ClassMethods
- Defined in:
- lib/sunrise/models/settings.rb
Instance Method Summary
collapse
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
get or set a variable with the variable as the called method
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/sunrise/models/settings.rb', line 30
def method_missing(method, *args)
if self.respond_to?(method)
super
else
method_name = method.to_s
if method_name =~ /=$/
var_name = method_name.gsub('=', '')
value = args.first
self[var_name] = value
else
self[method_name]
end
end
end
|
Instance Method Details
#[](var_name) ⇒ Object
get a setting value by [] notation
81
82
83
84
85
86
87
88
89
|
# File 'lib/sunrise/models/settings.rb', line 81
def [](var_name)
cache.fetch(cache_key(var_name), cache_options) do
if (var = target(var_name)).present?
var.value
else
defaults[var_name.to_sym]
end
end
end
|
#[]=(var_name, value) ⇒ Object
set a setting value by [] notation
92
93
94
95
96
97
98
99
100
|
# File 'lib/sunrise/models/settings.rb', line 92
def []=(var_name, value)
record = target_scoped.where(:var => var_name.to_s).first
record ||= new(:var => var_name.to_s)
record.value = value
record.save!
cache.write(cache_key(var_name), value, cache_options)
value
end
|
#all(starting_with = nil) ⇒ Object
retrieve all settings as a hash (optionally starting with a given namespace)
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/sunrise/models/settings.rb', line 67
def all(starting_with=nil)
query = target_scoped
query = query.where(["var LIKE ?", "#{starting_with}%"]) if starting_with
result = defaults.dup
query.all.each do |record|
result[record.var.to_sym] = record.value
end
result.with_indifferent_access
end
|
#cache_key(var_name) ⇒ Object
25
26
27
|
# File 'lib/sunrise/models/settings.rb', line 25
def cache_key(var_name)
[target_id, target_type, var_name].compact.map(&:to_s).join("::")
end
|
#delete_all(conditions = nil) ⇒ Object
61
62
63
64
|
# File 'lib/sunrise/models/settings.rb', line 61
def delete_all(conditions = nil)
cache.clear
super
end
|
#destroy(var_name) ⇒ Object
destroy the specified settings record
50
51
52
53
54
55
56
57
58
59
|
# File 'lib/sunrise/models/settings.rb', line 50
def destroy(var_name)
var_name = var_name.to_s
begin
target(var_name).destroy
cache.delete(cache_key(var_name))
true
rescue NoMethodError
raise SettingNotFound, "Setting variable \"#{var_name}\" not found"
end
end
|
#merge!(var_name, hash_value) ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/sunrise/models/settings.rb', line 102
def merge!(var_name, hash_value)
raise ArgumentError unless hash_value.is_a?(Hash)
old_value = self[var_name] || {}
raise TypeError, "Existing value is not a hash, can't merge!" unless old_value.is_a?(Hash)
new_value = old_value.merge(hash_value)
self[var_name] = new_value if new_value != old_value
new_value
end
|
#target(var_name) ⇒ Object
114
115
116
|
# File 'lib/sunrise/models/settings.rb', line 114
def target(var_name)
target_scoped.where(:var => var_name.to_s).first
end
|
#target_id ⇒ Object
122
123
124
|
# File 'lib/sunrise/models/settings.rb', line 122
def target_id
nil
end
|
#target_scoped ⇒ Object
118
119
120
|
# File 'lib/sunrise/models/settings.rb', line 118
def target_scoped
scoped.where(:target_id => target_id, :target_type => target_type)
end
|
#target_type ⇒ Object
126
127
128
|
# File 'lib/sunrise/models/settings.rb', line 126
def target_type
nil
end
|
#to_key ⇒ Object
138
139
140
|
# File 'lib/sunrise/models/settings.rb', line 138
def to_key
['settings']
end
|
#update_attributes(attrs) ⇒ Object
130
131
132
133
134
135
136
|
# File 'lib/sunrise/models/settings.rb', line 130
def update_attributes(attrs)
attrs.each do |key, value|
self[key] = value
end
cache.clear
end
|