Class: Cockpit::Settings

Inherits:
Object
  • 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.



95
96
97
98
99
100
101
102
# File 'lib/cockpit/core/settings.rb', line 95

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 (protected)



203
204
205
206
207
208
209
210
211
# File 'lib/cockpit/core/settings.rb', line 203

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

#nameObject (readonly)

Returns the value of attribute name.



93
94
95
# File 'lib/cockpit/core/settings.rb', line 93

def name
  @name
end

#recordObject (readonly)

Returns the value of attribute record.



93
94
95
# File 'lib/cockpit/core/settings.rb', line 93

def record
  @record
end

#record_typeObject (readonly)

Returns the value of attribute record_type.



93
94
95
# File 'lib/cockpit/core/settings.rb', line 93

def record_type
  @record_type
end

#storeObject (readonly)

Returns the value of attribute store.



93
94
95
# File 'lib/cockpit/core/settings.rb', line 93

def store
  @store
end

#store_typeObject (readonly)

Returns the value of attribute store_type.



93
94
95
# File 'lib/cockpit/core/settings.rb', line 93

def store_type
  @store_type
end

Class Method Details

.configure(options) ⇒ Object



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]
  
  # class to include this in
  clazz = case relationship
  when Class
    relationship
  when Object
    relationship.class
  when String, Symbol
    Object.const_get(relationship.to_s)
  else
    NilClass
  end
  
  # store to use in the include
  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)
  
  if options[:class] == NilClass
    global_setting options[:name], options[:class], settings
  end
  
  settings
end

.find(name) ⇒ Object



84
85
86
# File 'lib/cockpit/core/settings.rb', line 84

def find(name)
  global_setting(name)
end

.globalObject



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_settingsObject



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

.specsObject



8
9
10
# File 'lib/cockpit/core/settings.rb', line 8

def specs
  @specs ||= {}
end

Instance Method Details

#[](key) ⇒ Object



114
115
116
# File 'lib/cockpit/core/settings.rb', line 114

def [](key)
  store.has_key?(key.to_s) ? self.store[key.to_s] : default(key.to_s)
end

#[]=(key, value) ⇒ Object



118
119
120
121
122
# File 'lib/cockpit/core/settings.rb', line 118

def []=(key, value)
  with_callbacks(key, value) do |value|
    self.store[key.to_s] = value
  end
end

#clearObject



128
129
130
# File 'lib/cockpit/core/settings.rb', line 128

def clear
  self.store.clear
end

#default(key) ⇒ Object



136
137
138
# File 'lib/cockpit/core/settings.rb', line 136

def default(key)
  definition(key).value
end

#definition(key) ⇒ Object



140
141
142
# File 'lib/cockpit/core/settings.rb', line 140

def definition(key)
  spec.definition(key)
end

#each(&block) ⇒ Object



157
158
159
160
161
162
163
164
165
166
# File 'lib/cockpit/core/settings.rb', line 157

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

Returns:

  • (Boolean)


180
181
182
183
# File 'lib/cockpit/core/settings.rb', line 180

def empty?(key)
  value = self[key]
  value.respond_to?(:empty?) ? value.empty? : !value
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/cockpit/core/settings.rb', line 132

def has_key?(key)
  spec.has_key?(key)#!definition(key).blank?
end

#keysObject



110
111
112
# File 'lib/cockpit/core/settings.rb', line 110

def keys
  spec.keys
end

#merge!(hash) ⇒ Object



104
105
106
107
108
# File 'lib/cockpit/core/settings.rb', line 104

def merge!(hash)
  hash.each do |key, value|
    self[key] = value
  end
end

#present?(key) ⇒ Boolean

Returns:

  • (Boolean)


176
177
178
# File 'lib/cockpit/core/settings.rb', line 176

def present?(key)
  has_key?(key) && !empty?(key)  
end

#rootsObject



168
169
170
# File 'lib/cockpit/core/settings.rb', line 168

def roots
  spec.roots
end

#specObject



172
173
174
# File 'lib/cockpit/core/settings.rb', line 172

def spec
  @spec ||= self.class.spec(self.name, self.record_type)
end

#to_hashObject



144
145
146
147
148
149
# File 'lib/cockpit/core/settings.rb', line 144

def to_hash
  keys.inject({}) do |hash, key|
    hash[key] = self[key]
    hash
  end
end

#to_treeObject



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/cockpit/core/settings.rb', line 185

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



151
152
153
154
155
# File 'lib/cockpit/core/settings.rb', line 151

def update(hash)
  hash.each do |key, value|
    self[key] = value
  end
end

#with_callbacks(key, new_value, &block) ⇒ Object



124
125
126
# File 'lib/cockpit/core/settings.rb', line 124

def with_callbacks(key, new_value, &block)
  definition(key).with_callbacks(record, new_value, &block)
end