cockpit 0.0.1.5 → 0.0.1.7
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +52 -2
- data/Rakefile +2 -2
- data/app/models/setting.rb +59 -0
- data/lib/cockpit/cockpit.rb +8 -1
- data/lib/cockpit/configuration.rb +2 -2
- data/lib/cockpit/tree_hash.rb +5 -2
- data/lib/cockpit.rb +5 -5
- data/test/lib/database.rb +2 -1
- metadata +5 -4
data/README.markdown
CHANGED
@@ -2,6 +2,31 @@
|
|
2
2
|
|
3
3
|
<q>Super DRY Settings for Ruby, Rails, and Sinatra Apps.</q>
|
4
4
|
|
5
|
+
I am going to use this in all future gems that need configurable variables. Reason being, every gem uses _some_ configurable variables, and you end up writing the same code over and over again.
|
6
|
+
|
7
|
+
This will make it so if say 10 gems have custom settings, you can change this:
|
8
|
+
|
9
|
+
Paperclip.config = ...
|
10
|
+
S3.configure = ...
|
11
|
+
Authlogic.setup = ...
|
12
|
+
MyApp.settings = ....
|
13
|
+
|
14
|
+
to this:
|
15
|
+
|
16
|
+
Settings do
|
17
|
+
paperclip ...
|
18
|
+
s3 ...
|
19
|
+
authentication ...
|
20
|
+
app ...
|
21
|
+
end
|
22
|
+
|
23
|
+
Paperclip.config = Settings(:paperclip)
|
24
|
+
S3.configure = Settings(:s3)
|
25
|
+
Authlogic.setup = Settings(:authentication)
|
26
|
+
MyApp.settings = Settings(:app)
|
27
|
+
|
28
|
+
Which translates to 1, uniform, clean, configuration file.
|
29
|
+
|
5
30
|
## Install
|
6
31
|
|
7
32
|
sudo gem install cockpit
|
@@ -149,8 +174,32 @@ Sometimes you need a global store, sometimes that global store needs to be custo
|
|
149
174
|
|
150
175
|
## Todo
|
151
176
|
|
177
|
+
- Add ability to `freeze` certain branches of the tree (so plugins can use it and know `Settings.clear` won't remove it)
|
152
178
|
- Settings should be sorted by the way they were constructed
|
153
179
|
- Check type, so when it is saved it knows what to do.
|
180
|
+
- Store global declarations in memory
|
181
|
+
- Create "context" for each set of settings, giving it its own `tree`. Allows mimicking subclasses.
|
182
|
+
- `Settings` should be a collection of trees or `contexts`:
|
183
|
+
Settings
|
184
|
+
user
|
185
|
+
global
|
186
|
+
default
|
187
|
+
user_a
|
188
|
+
user_b
|
189
|
+
widget
|
190
|
+
global
|
191
|
+
default
|
192
|
+
widget_a
|
193
|
+
widget_b
|
194
|
+
text
|
195
|
+
default
|
196
|
+
widget_a
|
197
|
+
widget_b
|
198
|
+
social
|
199
|
+
default
|
200
|
+
widget_a
|
201
|
+
widget_b
|
202
|
+
Settings.for(:widget, :social) #=> default social widget settings.
|
154
203
|
|
155
204
|
This ended up being very similar to i18n:
|
156
205
|
|
@@ -161,7 +210,7 @@ I think the i18n gem should be broken down into two parts: Configuration (key/va
|
|
161
210
|
|
162
211
|
#### End Goal
|
163
212
|
|
164
|
-
- Base key-value functionality gem, which allows you to store arbitrary key values in any database (similar to moneta).
|
213
|
+
- Base key-value functionality gem, which allows you to store arbitrary key values in any database (similar to moneta). Should store settings in MongoDB by default.
|
165
214
|
- i18n and Cockpit build on top of that
|
166
215
|
|
167
216
|
### Alternatives
|
@@ -172,4 +221,5 @@ I think the i18n gem should be broken down into two parts: Configuration (key/va
|
|
172
221
|
- [SimpleConfig](http://github.com/lukeredpath/simpleconfig)
|
173
222
|
- [Configatron](http://github.com/markbates/configatron)
|
174
223
|
- [RConfig](http://github.com/rahmal/rconfig)
|
175
|
-
- [Serenity](http://github.com/progressions/serenity)
|
224
|
+
- [Serenity](http://github.com/progressions/serenity)
|
225
|
+
- [ApplicationSettings](http://github.com/bradhaydon/application_settings)
|
data/Rakefile
CHANGED
@@ -5,7 +5,7 @@ require 'rake/gempackagetask'
|
|
5
5
|
spec = Gem::Specification.new do |s|
|
6
6
|
s.name = "cockpit"
|
7
7
|
s.authors = ["Lance Pollard"]
|
8
|
-
s.version = "0.0.1.
|
8
|
+
s.version = "0.0.1.7"
|
9
9
|
s.summary = "Cockpit: Super DRY Configuration for Ruby, Rails, and Sinatra Apps"
|
10
10
|
s.homepage = "http://github.com/viatropos/cockpit"
|
11
11
|
s.email = "lancejpollard@gmail.com"
|
@@ -13,7 +13,7 @@ spec = Gem::Specification.new do |s|
|
|
13
13
|
s.has_rdoc = false
|
14
14
|
s.rubyforge_project = "cockpit"
|
15
15
|
s.platform = Gem::Platform::RUBY
|
16
|
-
s.files = %w(README.markdown Rakefile init.rb MIT-LICENSE) + Dir["{lib,rails,test}/**/*"] - Dir["test/tmp"]
|
16
|
+
s.files = %w(README.markdown Rakefile init.rb MIT-LICENSE) + Dir["{lib,rails,test,app}/**/*"] - Dir["test/tmp"]
|
17
17
|
s.require_path = "lib"
|
18
18
|
end
|
19
19
|
|
@@ -0,0 +1,59 @@
|
|
1
|
+
class Setting < ActiveRecord::Base
|
2
|
+
acts_as_settable
|
3
|
+
|
4
|
+
# set_table_name 'settings'
|
5
|
+
# attr_protected :is_proc, :interpolations
|
6
|
+
|
7
|
+
# serialize :value
|
8
|
+
# serialize :interpolations, Array
|
9
|
+
|
10
|
+
def save_with_setting
|
11
|
+
if save_without_setting
|
12
|
+
# update hash, eventually, just in case
|
13
|
+
end
|
14
|
+
end
|
15
|
+
alias_method_chain :save, :setting
|
16
|
+
|
17
|
+
class << self
|
18
|
+
# adjust the arguments, so we find automatically by key
|
19
|
+
def find(*args)
|
20
|
+
key = args.first
|
21
|
+
unless key.to_s =~ /^(all|first|last)$/
|
22
|
+
args[0] = :first
|
23
|
+
options = args.extract_options!
|
24
|
+
options[:conditions] = {:key => key.to_s} if key
|
25
|
+
args << options
|
26
|
+
end
|
27
|
+
result = super(*args)
|
28
|
+
# sync the global hash if necessary
|
29
|
+
sync_settings_hash(result)
|
30
|
+
|
31
|
+
result
|
32
|
+
end
|
33
|
+
|
34
|
+
def scoped(options = {}, &block)
|
35
|
+
result = super(options, &block)
|
36
|
+
sync_settings_hash(result)
|
37
|
+
result
|
38
|
+
end
|
39
|
+
|
40
|
+
def sync_settings_hash(result)
|
41
|
+
return unless result
|
42
|
+
if result.is_a?(Array) || (defined?(ActiveRecord::Relation) && result.is_a?(ActiveRecord::Relation))
|
43
|
+
result.each do |record|
|
44
|
+
sync_setting(record)
|
45
|
+
end
|
46
|
+
else
|
47
|
+
sync_setting(result)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def sync_setting(record)
|
52
|
+
record.value = Cockpit.type_cast(record.value, record.cast_as)
|
53
|
+
if record.respond_to?(:key) && record.configurable.nil?
|
54
|
+
Settings.store.set_one_without_database(record.key, record.value)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
data/lib/cockpit/cockpit.rb
CHANGED
@@ -5,7 +5,12 @@ module Cockpit
|
|
5
5
|
end
|
6
6
|
|
7
7
|
module ClassMethods
|
8
|
-
|
8
|
+
# can be "unique_by_key"
|
9
|
+
# settings :text do
|
10
|
+
# ...
|
11
|
+
# settings :social do
|
12
|
+
# ...
|
13
|
+
def acts_as_configurable(*args, &block)
|
9
14
|
options = args.extract_options!
|
10
15
|
settings_name = (args.shift || "settings").to_s
|
11
16
|
clazz_name = self.to_s.downcase.split("::").last
|
@@ -34,6 +39,8 @@ module Cockpit
|
|
34
39
|
end
|
35
40
|
|
36
41
|
end
|
42
|
+
alias configurable acts_as_configurable
|
43
|
+
alias settings acts_as_configurable
|
37
44
|
|
38
45
|
def acts_as_settable
|
39
46
|
belongs_to :configurable, :polymorphic => true
|
@@ -132,10 +132,10 @@ module Cockpit
|
|
132
132
|
@global
|
133
133
|
end
|
134
134
|
|
135
|
-
def
|
135
|
+
def define!(*args, &block)
|
136
136
|
options = args.extract_options!
|
137
137
|
path = args.first
|
138
|
-
if path && !path.is_a?(Hash)
|
138
|
+
if path && !path.is_a?(Hash) && !block_given?
|
139
139
|
global[path]
|
140
140
|
elsif !options.empty?
|
141
141
|
global.set(options)
|
data/lib/cockpit/tree_hash.rb
CHANGED
@@ -96,8 +96,11 @@ module Cockpit
|
|
96
96
|
options = args.extract_options!
|
97
97
|
meth = meth.to_s.gsub("=", "").to_sym
|
98
98
|
if args.empty?
|
99
|
-
|
100
|
-
|
99
|
+
if self.has_key?(meth)
|
100
|
+
found = self[meth]
|
101
|
+
else
|
102
|
+
found = get(meth).set_attributes(options)
|
103
|
+
end
|
101
104
|
found = found.instance_eval(&block) if block_given?
|
102
105
|
found
|
103
106
|
else
|
data/lib/cockpit.rb
CHANGED
@@ -2,21 +2,21 @@ require 'rubygems'
|
|
2
2
|
require 'active_support'
|
3
3
|
require 'active_record'
|
4
4
|
|
5
|
-
this = File.dirname(__FILE__)
|
5
|
+
this = File.expand_path(File.dirname(__FILE__))
|
6
6
|
Dir["#{this}/cockpit/*"].each { |c| require c }
|
7
7
|
|
8
8
|
class Settings
|
9
9
|
include Cockpit::Configuration
|
10
10
|
end
|
11
11
|
|
12
|
+
ActiveRecord::Base.send(:include, Cockpit) if defined?(ActiveRecord::Base)
|
13
|
+
|
12
14
|
def Settings(*args, &block)
|
13
|
-
Settings.
|
15
|
+
Settings.define!(*args, &block)
|
14
16
|
end
|
15
17
|
|
16
18
|
def Cockpit(*args, &block)
|
17
19
|
Settings(*args, &block)
|
18
20
|
end
|
19
21
|
|
20
|
-
|
21
|
-
|
22
|
-
require "#{this}/../app/models/setting.rb"
|
22
|
+
require File.expand_path("#{this}/../app/models/setting.rb")
|
data/test/lib/database.rb
CHANGED
@@ -14,9 +14,10 @@ ActiveRecord::Schema.define(:version => 1) do
|
|
14
14
|
t.string :cast_as
|
15
15
|
t.string :configurable_type
|
16
16
|
t.integer :configurable_id
|
17
|
+
t.string :context
|
17
18
|
end
|
18
19
|
|
19
|
-
create_table
|
20
|
+
create_table :users, :force => true do |t|
|
20
21
|
end
|
21
22
|
|
22
23
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cockpit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 69
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
9
|
- 1
|
10
|
-
-
|
11
|
-
version: 0.0.1.
|
10
|
+
- 7
|
11
|
+
version: 0.0.1.7
|
12
12
|
platform: ruby
|
13
13
|
authors:
|
14
14
|
- Lance Pollard
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2010-
|
19
|
+
date: 2010-07-02 00:00:00 -07:00
|
20
20
|
default_executable:
|
21
21
|
dependencies: []
|
22
22
|
|
@@ -48,6 +48,7 @@ files:
|
|
48
48
|
- test/test_settings.rb
|
49
49
|
- test/test_settings_in_database.rb
|
50
50
|
- test/test_settings_on_model.rb
|
51
|
+
- app/models/setting.rb
|
51
52
|
has_rdoc: true
|
52
53
|
homepage: http://github.com/viatropos/cockpit
|
53
54
|
licenses: []
|