Class: SettingCrazy::SettingsProxy
- Inherits:
-
Object
- Object
- SettingCrazy::SettingsProxy
show all
- Defined in:
- lib/settingcrazy/settings_proxy.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(model, template) ⇒ SettingsProxy
Returns a new instance of SettingsProxy.
5
6
7
8
9
10
|
# File 'lib/settingcrazy/settings_proxy.rb', line 5
def initialize(model, template)
@model = model
@template = template
@namespaces = model.class._setting_namespaces
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args, &block) ⇒ Object
48
49
50
51
52
53
54
55
|
# File 'lib/settingcrazy/settings_proxy.rb', line 48
def method_missing(method_name, *args, &block)
if method_name =~ /=$/
attribute = method_name[0...-1]
self[attribute] = args.first
else
self[method_name]
end
end
|
Instance Attribute Details
#template ⇒ Object
Returns the value of attribute template.
3
4
5
|
# File 'lib/settingcrazy/settings_proxy.rb', line 3
def template
@template
end
|
Instance Method Details
#[](key) ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
|
# File 'lib/settingcrazy/settings_proxy.rb', line 26
def [](key)
if @namespaces && namespace = @namespaces[key.to_sym]
return NamespacedSettingsProxy.new(@model, namespace)
end
sv = setting_record(key)
if sv.blank?
parent_value(key) || template_default_value(key) || nil
else
sv.value
end
end
|
#[]=(key, value) ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/settingcrazy/settings_proxy.rb', line 12
def []=(key, value)
if @namespaces && namespace = @namespaces[key.to_sym]
return NamespacedSettingsProxy.new(@model, namespace).bulk_assign(value)
end
value.reject!(&:blank?) if value.respond_to?(:reject!)
sv = setting_record(key)
if sv.blank?
build_value(key, value)
else
sv.value = value
end
end
|
#bulk_assign(attributes) ⇒ Object
38
39
40
41
42
|
# File 'lib/settingcrazy/settings_proxy.rb', line 38
def bulk_assign(attributes)
attributes.each do |(k,v)|
self[k] = v
end
end
|
#delete(key) ⇒ Object
44
45
46
|
# File 'lib/settingcrazy/settings_proxy.rb', line 44
def delete(key)
@model.setting_values.delete(setting_record(key))
end
|
#each(&block) ⇒ Object
62
63
64
|
# File 'lib/settingcrazy/settings_proxy.rb', line 62
def each(&block)
setting_values.each(&block)
end
|
#inspect ⇒ Object
70
71
72
73
|
# File 'lib/settingcrazy/settings_proxy.rb', line 70
def inspect
@model.reload unless @model.new_record?
self.to_hash.inspect
end
|
#map(&block) ⇒ Object
66
67
68
|
# File 'lib/settingcrazy/settings_proxy.rb', line 66
def map(&block)
setting_values.map(&block)
end
|
#parent_settings ⇒ Object
57
58
59
60
|
# File 'lib/settingcrazy/settings_proxy.rb', line 57
def parent_settings
return nil unless @model.class._inheritor.present?
@model.class._inheritor.parent_settings_for(@model)
end
|
#to_hash ⇒ Object
75
76
77
78
79
80
|
# File 'lib/settingcrazy/settings_proxy.rb', line 75
def to_hash
setting_values.inject({}) do |hash, sv|
hash[sv.key] = sv.value
hash
end.symbolize_keys
end
|