Class: Cockpit::Settings
- Inherits:
-
Object
show all
- Includes:
- Global
- Defined in:
- lib/cockpit/core/settings.rb,
lib/cockpit/core/spec.rb,
lib/cockpit/core/global.rb,
lib/cockpit/core/definition.rb
Overview
settings have one direct definition and many child proxy
Defined Under Namespace
Modules: Global
Classes: Definition, Spec
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Global
included
Constructor Details
#initialize(options = {}, &block) ⇒ Settings
Returns a new instance of Settings.
99
100
101
102
103
104
105
106
|
# File 'lib/cockpit/core/settings.rb', line 99
def initialize(options = {}, &block)
options = self.class.configure(options)
@name = options[:name]
@record = options[:record]
@record_type = options[:class] || @record.class
@store_type = options[:store]
@store = Cockpit::Store.use(options)
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
207
208
209
210
211
212
213
214
215
|
# File 'lib/cockpit/core/settings.rb', line 207
def method_missing(method, *args, &block)
if method.to_s =~ /(\w+)\?$/
present?($1)
elsif has_key?(method.to_s.gsub("=", ""))
Cockpit::Scope.new(self, method, *args, &block)
else
super(method, *args, &block)
end
end
|
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
97
98
99
|
# File 'lib/cockpit/core/settings.rb', line 97
def name
@name
end
|
#record ⇒ Object
Returns the value of attribute record.
97
98
99
|
# File 'lib/cockpit/core/settings.rb', line 97
def record
@record
end
|
#record_type ⇒ Object
Returns the value of attribute record_type.
97
98
99
|
# File 'lib/cockpit/core/settings.rb', line 97
def record_type
@record_type
end
|
#store ⇒ Object
Returns the value of attribute store.
97
98
99
|
# File 'lib/cockpit/core/settings.rb', line 97
def store
@store
end
|
#store_type ⇒ Object
Returns the value of attribute store_type.
97
98
99
|
# File 'lib/cockpit/core/settings.rb', line 97
def store_type
@store_type
end
|
Class Method Details
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
|
# File 'lib/cockpit/core/settings.rb', line 35
def configure(options)
name = (options[:name] || "default").to_s
relationship = options[:class] || options[:for] || options[:class_name] || options[:record]
store = options[:store]
clazz = case relationship
when Class
relationship
when Object
relationship.class
when String, Symbol
Object.const_get(relationship.to_s)
else
NilClass
end
unless store
if defined?(::ActiveRecord::Base) && clazz.ancestors.include?(::ActiveRecord::Base)
store = :active_record
else
store = :memory
end
end
options[:class] = clazz
options[:store] = store
options[:name] = name
options
end
|
.define!(options = {}, &block) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/cockpit/core/settings.rb', line 16
def define!(options = {}, &block)
options = {:store => options.to_sym} unless options.is_a?(Hash)
options = configure(options)
unless options[:class] == NilClass
options[:class].send(:include, Cockpit::Store.support(options[:store]))
end
spec options[:name], options[:class], Cockpit::Settings::Spec.new(options, &block)
settings = Cockpit::Settings.new(options)
global_setting options[:name], options[:class], settings
settings
end
|
.find(name) ⇒ Object
84
85
86
|
# File 'lib/cockpit/core/settings.rb', line 84
def find(name)
global_setting(name)
end
|
.for(clazz, name = :default) ⇒ Object
92
93
94
|
# File 'lib/cockpit/core/settings.rb', line 92
def for(clazz, name = :default)
global_setting(name, clazz)
end
|
.global ⇒ Object
80
81
82
|
# File 'lib/cockpit/core/settings.rb', line 80
def global
global_setting("default")
end
|
.global_setting(name, clazz = NilClass, value = nil) ⇒ Object
74
75
76
77
78
|
# File 'lib/cockpit/core/settings.rb', line 74
def global_setting(name, clazz = NilClass, value = nil)
global_settings[clazz.to_s] ||= {}
global_settings[clazz.to_s][name.to_s] = value if value
global_settings[clazz.to_s][name.to_s]
end
|
.global_settings ⇒ Object
12
13
14
|
# File 'lib/cockpit/core/settings.rb', line 12
def global_settings
@global_settings ||= {}
end
|
.method_missing(method, *args, &block) ⇒ Object
88
89
90
|
# File 'lib/cockpit/core/settings.rb', line 88
def method_missing(method, *args, &block)
global.send(method, *args, &block)
end
|
.spec(name, clazz = NilClass, value = nil) ⇒ Object
68
69
70
71
72
|
# File 'lib/cockpit/core/settings.rb', line 68
def spec(name, clazz = NilClass, value = nil)
specs[clazz.to_s] ||= {}
specs[clazz.to_s][name.to_s] ||= value if value
specs[clazz.to_s][name.to_s]
end
|
.specs ⇒ Object
8
9
10
|
# File 'lib/cockpit/core/settings.rb', line 8
def specs
@specs ||= {}
end
|
Instance Method Details
#[](key) ⇒ Object
118
119
120
|
# File 'lib/cockpit/core/settings.rb', line 118
def [](key)
store.has_key?(key.to_s) ? self.store[key.to_s] : default(key.to_s)
end
|
#[]=(key, value) ⇒ Object
122
123
124
125
126
|
# File 'lib/cockpit/core/settings.rb', line 122
def []=(key, value)
with_callbacks(key, value) do |value|
self.store[key.to_s] = value
end
end
|
#clear ⇒ Object
132
133
134
|
# File 'lib/cockpit/core/settings.rb', line 132
def clear
self.store.clear
end
|
#default(key) ⇒ Object
140
141
142
|
# File 'lib/cockpit/core/settings.rb', line 140
def default(key)
definition(key).value
end
|
#definition(key) ⇒ Object
144
145
146
|
# File 'lib/cockpit/core/settings.rb', line 144
def definition(key)
spec.definition(key)
end
|
#each(&block) ⇒ Object
161
162
163
164
165
166
167
168
169
170
|
# File 'lib/cockpit/core/settings.rb', line 161
def each(&block)
keys.each do |key|
case block.arity
when 1
yield(key)
when 2
yield(key, self[key])
end
end
end
|
#empty?(key) ⇒ Boolean
184
185
186
187
|
# File 'lib/cockpit/core/settings.rb', line 184
def empty?(key)
value = self[key]
value.respond_to?(:empty?) ? value.empty? : !value
end
|
#has_key?(key) ⇒ Boolean
136
137
138
|
# File 'lib/cockpit/core/settings.rb', line 136
def has_key?(key)
spec.has_key?(key)end
|
#keys ⇒ Object
114
115
116
|
# File 'lib/cockpit/core/settings.rb', line 114
def keys
spec.keys
end
|
#merge!(hash) ⇒ Object
108
109
110
111
112
|
# File 'lib/cockpit/core/settings.rb', line 108
def merge!(hash)
hash.each do |key, value|
self[key] = value
end
end
|
#present?(key) ⇒ Boolean
180
181
182
|
# File 'lib/cockpit/core/settings.rb', line 180
def present?(key)
has_key?(key) && !empty?(key)
end
|
#roots ⇒ Object
172
173
174
|
# File 'lib/cockpit/core/settings.rb', line 172
def roots
spec.roots
end
|
#spec ⇒ Object
176
177
178
|
# File 'lib/cockpit/core/settings.rb', line 176
def spec
@spec ||= self.class.spec(self.name, self.record_type)
end
|
#to_hash ⇒ Object
148
149
150
151
152
153
|
# File 'lib/cockpit/core/settings.rb', line 148
def to_hash
keys.inject({}) do |hash, key|
hash[key] = self[key]
hash
end
end
|
#to_tree ⇒ Object
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
# File 'lib/cockpit/core/settings.rb', line 189
def to_tree
keys.inject({}) do |result, path|
curr = result
names = path.split(".")
names.each do |name|
if name == names.last
curr[name] = Cockpit::Settings[path]
else
curr[name] ||= {}
curr = curr[name]
end
end
result
end
end
|
#update(hash) ⇒ Object
155
156
157
158
159
|
# File 'lib/cockpit/core/settings.rb', line 155
def update(hash)
hash.each do |key, value|
self[key] = value
end
end
|
#with_callbacks(key, new_value, &block) ⇒ Object
128
129
130
|
# File 'lib/cockpit/core/settings.rb', line 128
def with_callbacks(key, new_value, &block)
definition(key).with_callbacks(record, new_value, &block)
end
|