cockpit 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/{MIT-LICENSE → MIT-LICENSE.markdown} +0 -0
- data/README.markdown +70 -47
- data/lib/cockpit.rb +1 -0
- data/lib/cockpit/core/definition.rb +187 -76
- data/lib/cockpit/core/global.rb +35 -0
- data/lib/cockpit/core/helpers.rb +12 -0
- data/lib/cockpit/core/include.rb +29 -74
- data/lib/cockpit/core/scope.rb +46 -0
- data/lib/cockpit/core/settings.rb +113 -74
- data/lib/cockpit/core/spec.rb +65 -0
- data/lib/cockpit/core/store.rb +34 -38
- data/lib/cockpit/stores/active_record.rb +128 -0
- data/lib/cockpit/stores/file_system.rb +11 -0
- data/lib/cockpit/stores/memory.rb +16 -0
- data/lib/cockpit/stores/mongo.rb +29 -0
- data/lib/cockpit/stores/redis.rb +11 -0
- metadata +16 -19
- data/Rakefile +0 -79
- data/lib/cockpit/core/definitions.rb +0 -55
- data/lib/cockpit/many/include.rb +0 -18
- data/lib/cockpit/many/settings.rb +0 -21
- data/lib/cockpit/moneta/active_record.rb +0 -120
- data/lib/cockpit/moneta/simple_active_record.rb +0 -93
- data/test/lib/database.rb +0 -22
- data/test/lib/user.rb +0 -10
- data/test/test_active_record.rb +0 -80
- data/test/test_helper.rb +0 -88
- data/test/test_mongo.rb +0 -82
- data/test/test_stores.rb +0 -125
@@ -0,0 +1,35 @@
|
|
1
|
+
module Cockpit
|
2
|
+
class Settings
|
3
|
+
module Global
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
def [](key)
|
10
|
+
global[key]
|
11
|
+
end
|
12
|
+
|
13
|
+
def []=(key, value)
|
14
|
+
global[key] = value
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.Settings(*args)
|
21
|
+
case args.length
|
22
|
+
when 0
|
23
|
+
Cockpit::Settings.global
|
24
|
+
when 1
|
25
|
+
case args.first
|
26
|
+
when Hash
|
27
|
+
|
28
|
+
else
|
29
|
+
Cockpit::Settings[args.first]
|
30
|
+
end
|
31
|
+
when 2
|
32
|
+
Cockpit::Settings[args.first] = args.last
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Cockpit
|
2
|
+
module ViewHelpers
|
3
|
+
unless respond_to?(:c)
|
4
|
+
def c(key)
|
5
|
+
Cockpit::Settings.global[key]
|
6
|
+
end
|
7
|
+
end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
::ActionView::Helpers.send(:include, Cockpit::ViewHelpers) if defined?(::ActionView::Helpers)
|
12
|
+
::Sinatra.helpers.send(:helper, Cockpit::ViewHelpers) if defined?(::Sinatra)
|
data/lib/cockpit/core/include.rb
CHANGED
@@ -1,89 +1,44 @@
|
|
1
1
|
module Cockpit
|
2
|
+
|
2
3
|
def self.included(base)
|
3
|
-
base.
|
4
|
-
|
5
|
-
base.send(:include, ActiveRecordInclude)
|
6
|
-
end
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
base.send(:include, InstanceMethods)
|
7
6
|
end
|
8
7
|
|
9
|
-
module
|
10
|
-
def
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
)
|
19
|
-
|
20
|
-
@cockpit.keys.each do |key|
|
21
|
-
next if key =~ /\./
|
22
|
-
|
23
|
-
define_method key do
|
24
|
-
send(:cockpit)[key]
|
25
|
-
end
|
26
|
-
|
27
|
-
define_method "#{key}?" do
|
28
|
-
!send(key).blank?
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
else
|
33
|
-
@cockpit
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
def cockpit
|
38
|
-
unless @cockpit
|
39
|
-
@cockpit = Cockpit::Settings.new(
|
40
|
-
:name => self.class.cockpit.name,
|
41
|
-
:store => :active_record,
|
42
|
-
:record => self
|
43
|
-
)
|
8
|
+
module ClassMethods
|
9
|
+
def cockpit(options = {}, &block)
|
10
|
+
if block_given?
|
11
|
+
options = {:store => options.to_sym} unless options.is_a?(Hash)
|
12
|
+
@cockpit = Cockpit::Settings.define!(options.merge(:for => self), &block)
|
13
|
+
|
14
|
+
@cockpit.roots.map(&:key).flatten.each do |key|
|
15
|
+
define_method key do
|
16
|
+
send(:cockpit).send(key)
|
44
17
|
end
|
45
18
|
|
46
|
-
|
19
|
+
define_method "#{key}?" do
|
20
|
+
send(:cockpit).has_key?(key)
|
21
|
+
end
|
47
22
|
end
|
23
|
+
end
|
24
|
+
|
25
|
+
@cockpit
|
26
|
+
end
|
27
|
+
|
28
|
+
unless respond_to?(:key)
|
29
|
+
def key(*args)
|
48
30
|
|
49
|
-
def get(key)
|
50
|
-
cockpit[key]
|
51
|
-
end unless respond_to?(:get)
|
52
|
-
|
53
|
-
def set(*args)
|
54
|
-
if args.last.is_a?(Hash)
|
55
|
-
cockpit.set(args.last)
|
56
|
-
else
|
57
|
-
cockpit[args.first] = args.last
|
58
|
-
end
|
59
|
-
end unless respond_to?(:set)
|
60
31
|
end
|
61
32
|
end
|
62
33
|
end
|
63
34
|
|
64
|
-
module
|
65
|
-
def
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
:scope => "default",
|
72
|
-
:store => args.first || "memory",
|
73
|
-
&block
|
74
|
-
)
|
75
|
-
else
|
76
|
-
@cockpit
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
def cockpit
|
81
|
-
unless @cockpit
|
82
|
-
@cockpit = self.class.cockpit.dup
|
83
|
-
end
|
84
|
-
|
85
|
-
@cockpit
|
86
|
-
end
|
35
|
+
module InstanceMethods
|
36
|
+
def cockpit(key = nil)
|
37
|
+
@cockpit ||= Cockpit::Settings.new(:record => self)
|
38
|
+
if key
|
39
|
+
@cockpit.definition(key)
|
40
|
+
else
|
41
|
+
@cockpit
|
87
42
|
end
|
88
43
|
end
|
89
44
|
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Cockpit
|
2
|
+
class Scope
|
3
|
+
attr_reader :key, :settings
|
4
|
+
|
5
|
+
def initialize(settings, method, *args, &block)
|
6
|
+
@settings = settings
|
7
|
+
process(method, *args, &block)
|
8
|
+
end
|
9
|
+
|
10
|
+
def value
|
11
|
+
settings[key]
|
12
|
+
end
|
13
|
+
|
14
|
+
def value=(x)
|
15
|
+
settings[key] = x
|
16
|
+
end
|
17
|
+
|
18
|
+
def process(method, *args, &block)
|
19
|
+
node = method.to_s
|
20
|
+
|
21
|
+
boolean = !!(node =~ /\?$/)
|
22
|
+
node.gsub!(/\?/, "") if boolean
|
23
|
+
|
24
|
+
set = !!(node =~ /=$/)
|
25
|
+
node.gsub!("=", "") if set
|
26
|
+
|
27
|
+
if key
|
28
|
+
@key = "#{key}.#{node}"
|
29
|
+
else
|
30
|
+
@key = node
|
31
|
+
end
|
32
|
+
|
33
|
+
if boolean == true
|
34
|
+
settings.has_key?(key)
|
35
|
+
elsif set == true
|
36
|
+
self.value = args.pop
|
37
|
+
else
|
38
|
+
self
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def method_missing(method, *args, &block)
|
43
|
+
process(method, *args, &block)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -1,86 +1,103 @@
|
|
1
1
|
module Cockpit
|
2
2
|
# settings have one direct definition and many child proxy
|
3
3
|
class Settings
|
4
|
+
include Global
|
5
|
+
|
4
6
|
class << self
|
5
|
-
|
6
|
-
|
7
|
-
# Cockpit::Settings.define!(:name => "root", :scope => "default")
|
8
|
-
def define!(*args, &block)
|
9
|
-
setting = Cockpit::Settings.new(*args, &block)
|
10
|
-
@root ||= setting
|
11
|
-
setting
|
7
|
+
def specs
|
8
|
+
@specs ||= {}
|
12
9
|
end
|
13
10
|
|
14
|
-
def
|
15
|
-
|
11
|
+
def global_settings
|
12
|
+
@global_settings ||= {}
|
16
13
|
end
|
17
14
|
|
18
|
-
def
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
15
|
+
def define!(options = {}, &block)
|
16
|
+
options = {:store => options.to_sym} unless options.is_a?(Hash)
|
17
|
+
options = configure(options)
|
18
|
+
|
19
|
+
unless options[:class] == NilClass
|
20
|
+
options[:class].send(:include, Cockpit::Store.support(options[:store]))
|
21
|
+
end
|
22
|
+
|
23
|
+
spec options[:name], options[:class], Cockpit::Settings::Spec.new(options, &block)
|
24
|
+
|
25
|
+
settings = Cockpit::Settings.new(options)
|
26
|
+
|
27
|
+
if options[:class] == NilClass
|
28
|
+
global_setting options[:name], options[:class], settings
|
29
|
+
end
|
30
|
+
|
31
|
+
settings
|
24
32
|
end
|
25
33
|
|
26
|
-
def
|
27
|
-
|
28
|
-
|
34
|
+
def configure(options)
|
35
|
+
name = (options[:name] || "default").to_s
|
36
|
+
relationship = options[:class] || options[:for] || options[:class_name] || options[:record]
|
37
|
+
store = options[:store]
|
38
|
+
|
39
|
+
# class to include this in
|
40
|
+
clazz = case relationship
|
41
|
+
when Class
|
42
|
+
relationship
|
43
|
+
when Object
|
44
|
+
relationship.class
|
45
|
+
when String, Symbol
|
46
|
+
Object.const_get(relationship.to_s)
|
47
|
+
else
|
48
|
+
NilClass
|
49
|
+
end
|
50
|
+
|
51
|
+
# store to use in the include
|
52
|
+
unless store
|
53
|
+
if defined?(::ActiveRecord::Base) && clazz.ancestors.include?(::ActiveRecord::Base)
|
54
|
+
store = :active_record
|
55
|
+
else
|
56
|
+
store = :memory
|
57
|
+
end
|
29
58
|
end
|
59
|
+
|
60
|
+
options[:class] = clazz
|
61
|
+
options[:store] = store
|
62
|
+
options[:name] = name
|
63
|
+
|
64
|
+
options
|
30
65
|
end
|
31
66
|
|
32
|
-
def
|
33
|
-
|
67
|
+
def spec(name, clazz = NilClass, value = nil)
|
68
|
+
specs[clazz.to_s] ||= {}
|
69
|
+
specs[clazz.to_s][name.to_s] ||= value if value
|
70
|
+
specs[clazz.to_s][name.to_s]
|
34
71
|
end
|
35
72
|
|
36
|
-
def
|
37
|
-
|
73
|
+
def global_setting(name, clazz = NilClass, value = nil)
|
74
|
+
global_settings[clazz.to_s] ||= {}
|
75
|
+
global_settings[clazz.to_s][name.to_s] = value if value
|
76
|
+
global_settings[clazz.to_s][name.to_s]
|
38
77
|
end
|
39
78
|
|
40
|
-
def
|
41
|
-
|
42
|
-
end
|
43
|
-
|
44
|
-
def []=(key, value)
|
45
|
-
root[key] = value
|
46
|
-
end
|
47
|
-
|
48
|
-
def clear
|
49
|
-
root.clear
|
79
|
+
def global
|
80
|
+
global_setting("default")
|
50
81
|
end
|
51
82
|
|
52
|
-
def
|
53
|
-
|
83
|
+
def find(name)
|
84
|
+
global_setting(name)
|
54
85
|
end
|
55
86
|
|
56
|
-
def
|
57
|
-
|
87
|
+
def method_missing(method, *args, &block)
|
88
|
+
global.send(method, *args, &block)
|
58
89
|
end
|
59
90
|
end
|
60
91
|
|
61
|
-
|
92
|
+
attr_reader :name, :record, :store, :store_type, :record_type
|
62
93
|
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
options[:
|
67
|
-
options[:
|
68
|
-
|
69
|
-
|
70
|
-
end
|
71
|
-
raise ArgumentError.new("pass in a :store to Cockpit::Settings") if self.store.nil?
|
72
|
-
|
73
|
-
args << options
|
74
|
-
|
75
|
-
if definition = self.class.definitions_for(options[:name])
|
76
|
-
definition.define!(*args, &block)
|
77
|
-
else
|
78
|
-
self.class.definitions << Cockpit::Definitions.new(*args, &block)
|
79
|
-
end
|
80
|
-
end
|
81
|
-
|
82
|
-
def store=(value)
|
83
|
-
@store = Cockpit::Store.new(name, value, record).adapter
|
94
|
+
def initialize(options = {}, &block)
|
95
|
+
options = self.class.configure(options)
|
96
|
+
@name = options[:name]
|
97
|
+
@record = options[:record]
|
98
|
+
@record_type = options[:class] || @record.class
|
99
|
+
@store_type = options[:store]
|
100
|
+
@store = Cockpit::Store.use(options)
|
84
101
|
end
|
85
102
|
|
86
103
|
def merge!(hash)
|
@@ -88,17 +105,23 @@ module Cockpit
|
|
88
105
|
self[key] = value
|
89
106
|
end
|
90
107
|
end
|
91
|
-
|
108
|
+
|
92
109
|
def keys
|
93
|
-
|
110
|
+
spec.keys
|
94
111
|
end
|
95
|
-
|
112
|
+
|
96
113
|
def [](key)
|
97
114
|
self.store[key.to_s] || default(key.to_s)
|
98
115
|
end
|
99
116
|
|
100
117
|
def []=(key, value)
|
101
|
-
|
118
|
+
with_callbacks(key, value) do |value|
|
119
|
+
self.store[key.to_s] = value
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def with_callbacks(key, new_value, &block)
|
124
|
+
definition(key).with_callbacks(record, new_value, &block)
|
102
125
|
end
|
103
126
|
|
104
127
|
def clear
|
@@ -106,15 +129,15 @@ module Cockpit
|
|
106
129
|
end
|
107
130
|
|
108
131
|
def has_key?(key)
|
109
|
-
|
132
|
+
spec.has_key?(key)#!definition(key).blank?
|
110
133
|
end
|
111
134
|
|
112
135
|
def default(key)
|
113
|
-
|
136
|
+
definition(key).value
|
114
137
|
end
|
115
138
|
|
116
139
|
def definition(key)
|
117
|
-
|
140
|
+
spec.definition(key)
|
118
141
|
end
|
119
142
|
|
120
143
|
def to_hash
|
@@ -124,22 +147,38 @@ module Cockpit
|
|
124
147
|
end
|
125
148
|
end
|
126
149
|
|
127
|
-
def
|
128
|
-
|
150
|
+
def update(hash)
|
151
|
+
hash.each do |key, value|
|
152
|
+
self[key] = value
|
153
|
+
end
|
129
154
|
end
|
130
155
|
|
131
|
-
|
132
|
-
|
133
|
-
|
156
|
+
def each(&block)
|
157
|
+
keys.each do |key|
|
158
|
+
case block.arity
|
159
|
+
when 1
|
160
|
+
yield(key)
|
161
|
+
when 2
|
162
|
+
yield(key, self[key])
|
163
|
+
end
|
164
|
+
end
|
134
165
|
end
|
135
166
|
|
136
|
-
def
|
137
|
-
|
167
|
+
def roots
|
168
|
+
spec.roots
|
138
169
|
end
|
139
170
|
|
171
|
+
def spec
|
172
|
+
@spec ||= self.class.spec(self.name, self.record_type)
|
173
|
+
end
|
174
|
+
|
175
|
+
protected
|
176
|
+
|
140
177
|
def method_missing(method, *args, &block)
|
141
|
-
if
|
142
|
-
|
178
|
+
if method.to_s =~ /(\w+)\?$/
|
179
|
+
has_key?($1)
|
180
|
+
elsif has_key?(method)
|
181
|
+
Cockpit::Scope.new(self, method, *args, &block)
|
143
182
|
else
|
144
183
|
super(method, *args, &block)
|
145
184
|
end
|