cockpit 0.0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/MIT-LICENSE +20 -0
- data/README.markdown +175 -0
- data/Rakefile +78 -0
- data/init.rb +1 -0
- data/lib/cockpit.rb +22 -0
- data/lib/cockpit/cockpit.rb +94 -0
- data/lib/cockpit/configuration.rb +218 -0
- data/lib/cockpit/definition.rb +55 -0
- data/lib/cockpit/extensions.rb +25 -0
- data/lib/cockpit/helper.rb +44 -0
- data/lib/cockpit/store.rb +103 -0
- data/lib/cockpit/tree_hash.rb +113 -0
- data/rails/init.rb +1 -0
- data/test/lib/database.rb +22 -0
- data/test/lib/user.rb +8 -0
- data/test/test_helper.rb +90 -0
- data/test/test_settings.rb +228 -0
- data/test/test_settings_in_database.rb +153 -0
- data/test/test_settings_on_model.rb +68 -0
- metadata +86 -0
@@ -0,0 +1,228 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
|
+
|
3
|
+
class SettingsTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
context "Settings" do
|
6
|
+
|
7
|
+
setup do
|
8
|
+
Settings.store = :memory
|
9
|
+
Settings.set(:lance => "first", :dane => 2)
|
10
|
+
end
|
11
|
+
|
12
|
+
should "have have values" do
|
13
|
+
desired_result = {
|
14
|
+
:lance => {
|
15
|
+
:type => :string,
|
16
|
+
:value => "first"
|
17
|
+
},
|
18
|
+
:dane => {
|
19
|
+
:type => :integer,
|
20
|
+
:value => 2
|
21
|
+
}
|
22
|
+
}
|
23
|
+
assert_equal(desired_result, Settings.tree)
|
24
|
+
end
|
25
|
+
|
26
|
+
context "get" do
|
27
|
+
|
28
|
+
setup do
|
29
|
+
Settings.clear
|
30
|
+
Settings.set(:lance => "first", :dane => 2)
|
31
|
+
end
|
32
|
+
|
33
|
+
should "be able to get values" do
|
34
|
+
assert_equal "first", Settings.get(:lance).value
|
35
|
+
assert_equal :string, Settings.get(:lance).value_type
|
36
|
+
assert_equal :integer, Settings.get(:dane).value_type
|
37
|
+
end
|
38
|
+
|
39
|
+
should "be able ot get values by using string" do
|
40
|
+
assert_equal "first", Settings.get("lance").value
|
41
|
+
end
|
42
|
+
|
43
|
+
should "be able to get values using all syntax" do
|
44
|
+
assert_equal "first", Settings.get("lance").value
|
45
|
+
assert_equal "first", Settings("lance").value
|
46
|
+
assert_equal "first", Settings["lance"].value
|
47
|
+
assert_equal "first", Settings.lance.value
|
48
|
+
end
|
49
|
+
|
50
|
+
should "return empty TreeHash if value doesn't exist" do
|
51
|
+
assert_equal({}, Settings.get("nope"))
|
52
|
+
assert_equal false, Settings.get("nope").nil?
|
53
|
+
assert Settings.get("nope").empty?
|
54
|
+
assert Settings.get("nope").blank?
|
55
|
+
assert_equal({}, Settings["asset.thumb.random"].value)
|
56
|
+
end
|
57
|
+
|
58
|
+
should "raise an error if we use exclamation on 'get!'" do
|
59
|
+
assert_raise(RuntimeError) { Settings.get!("nope") }
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
context "set" do
|
64
|
+
|
65
|
+
setup do
|
66
|
+
Settings.clear
|
67
|
+
end
|
68
|
+
|
69
|
+
should "be able to set new values" do
|
70
|
+
Settings.set(:name => "value")
|
71
|
+
assert_equal "value", Settings[:name].value
|
72
|
+
end
|
73
|
+
|
74
|
+
should "be able to set using []=" do
|
75
|
+
Settings[:name] = "value"
|
76
|
+
assert_equal "value", Settings[:name].value
|
77
|
+
end
|
78
|
+
|
79
|
+
should "not be able to set a value if it's a string" do
|
80
|
+
assert_equal nil, Settings.set("set value")
|
81
|
+
end
|
82
|
+
|
83
|
+
should "be able to set a value using parentheses syntax" do
|
84
|
+
Settings("path.to.hash" => "me")
|
85
|
+
assert_equal "me", Settings("path.to.hash").value
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
context "nested values" do
|
91
|
+
|
92
|
+
should "be able to set nested value by string key" do
|
93
|
+
Settings["a.nested.string"] = "value"
|
94
|
+
assert_equal "value", Settings["a.nested.string"].value
|
95
|
+
assert_kind_of Cockpit::TreeHash, Settings["a.nested"]
|
96
|
+
end
|
97
|
+
|
98
|
+
should "be able to change string to further nested hash" do
|
99
|
+
Settings["a.nested.string"] = "value"
|
100
|
+
assert_equal "value", Settings["a.nested.string"].value
|
101
|
+
Settings["a.nested.string.with.further.depth"] = "value"
|
102
|
+
assert_equal "value", Settings["a.nested.string.with.further.depth"].value
|
103
|
+
Settings["a.nested.string"] = "value"
|
104
|
+
assert_equal "value", Settings["a.nested.string"].value
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
|
109
|
+
context "with dsl" do
|
110
|
+
|
111
|
+
setup do
|
112
|
+
Settings.clear
|
113
|
+
Settings do
|
114
|
+
asset :title => "Asset (and related) Settings" do
|
115
|
+
thumb do
|
116
|
+
width 100, :tip => "Thumb's width"
|
117
|
+
height 100, :tip => "Thumb's height"
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
should "property should be a hash of values" do
|
124
|
+
desired_result = {
|
125
|
+
:type => :integer,
|
126
|
+
:value => 100,
|
127
|
+
:tip => "Thumb's width"
|
128
|
+
}
|
129
|
+
assert_equal(desired_result, Settings["asset.thumb.width"])
|
130
|
+
end
|
131
|
+
|
132
|
+
should "be able to call 'value' on object" do
|
133
|
+
assert_equal 100, Settings["asset.thumb.width.value"]
|
134
|
+
assert_equal 100, Settings["asset.thumb.width"].value
|
135
|
+
end
|
136
|
+
|
137
|
+
should "be able to call 'tip' if it has tooltip" do
|
138
|
+
assert_equal "Thumb's width", Settings["asset.thumb.width"].tip
|
139
|
+
end
|
140
|
+
|
141
|
+
should "be able to call 'tip' through chain" do
|
142
|
+
assert_equal "Thumb's width", Settings.asset.thumb.width.tip
|
143
|
+
end
|
144
|
+
|
145
|
+
context "should be able to take a lambda as a value" do
|
146
|
+
|
147
|
+
setup do
|
148
|
+
Settings.clear
|
149
|
+
@languages = %w(English Italian Spanish)
|
150
|
+
Settings do
|
151
|
+
site :title => "Default Site Settings" do
|
152
|
+
languages :value => lambda { %w(English Italian Spanish) }
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
|
157
|
+
should "have a proc as a value" do
|
158
|
+
assert_kind_of Proc, Settings["site.languages.value"]
|
159
|
+
end
|
160
|
+
|
161
|
+
should "be able to call the proc" do
|
162
|
+
assert @languages, Settings["site.languages.value"].call
|
163
|
+
end
|
164
|
+
|
165
|
+
end
|
166
|
+
|
167
|
+
context "should be able to take a Proc.new as a value" do
|
168
|
+
setup do
|
169
|
+
Settings.clear
|
170
|
+
@timezones = %w(pacific central eastern)
|
171
|
+
Settings do
|
172
|
+
site :title => "Default Site Settings" do
|
173
|
+
timezones :value => Proc.new { %w(pacific central eastern) }
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
should "have a proc as a value" do
|
179
|
+
assert_kind_of Proc, Settings["site.timezones.value"]
|
180
|
+
end
|
181
|
+
|
182
|
+
should "be able to call the proc" do
|
183
|
+
assert @timezones, Settings["site.timezones.value"].call
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
context "should be able to take a large dsl and make something of it!" do
|
188
|
+
|
189
|
+
setup do
|
190
|
+
Settings.clear
|
191
|
+
load_settings
|
192
|
+
end
|
193
|
+
|
194
|
+
should "find the nested teaser" do
|
195
|
+
assert_equal 2, Settings["site.teasers.right"].value
|
196
|
+
end
|
197
|
+
|
198
|
+
end
|
199
|
+
|
200
|
+
end
|
201
|
+
|
202
|
+
context "setting categories" do
|
203
|
+
|
204
|
+
setup do
|
205
|
+
Settings.clear
|
206
|
+
load_settings
|
207
|
+
end
|
208
|
+
|
209
|
+
should "be able to get settings by category" do
|
210
|
+
[:site, :asset].each do |type|
|
211
|
+
assert Settings[type].keys
|
212
|
+
end
|
213
|
+
end
|
214
|
+
|
215
|
+
should "traverse categories" do
|
216
|
+
Settings(:site).each_setting do |key, attributes, value|
|
217
|
+
assert attributes
|
218
|
+
#puts "Key: #{key.to_s}, Attributes: #{attributes.inspect}, Value: #{value.inspect}"
|
219
|
+
end
|
220
|
+
end
|
221
|
+
|
222
|
+
end
|
223
|
+
|
224
|
+
teardown { Settings.clear }
|
225
|
+
|
226
|
+
end
|
227
|
+
|
228
|
+
end
|
@@ -0,0 +1,153 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
|
+
|
3
|
+
class SettingsTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
context "Database Store" do
|
6
|
+
|
7
|
+
setup do
|
8
|
+
Settings.clear
|
9
|
+
Settings.store = :db
|
10
|
+
end
|
11
|
+
|
12
|
+
should "have a db as a store" do
|
13
|
+
assert_kind_of Cockpit::Store::Database, Settings.store
|
14
|
+
end
|
15
|
+
|
16
|
+
context "save to the database" do
|
17
|
+
|
18
|
+
setup do
|
19
|
+
Settings["lantial"] = "to database"
|
20
|
+
Settings["pollard"] = "another"
|
21
|
+
end
|
22
|
+
|
23
|
+
should "have saved lance into the database" do
|
24
|
+
assert_equal "to database", Setting.find_by_key("lantial").value
|
25
|
+
end
|
26
|
+
|
27
|
+
should "be able to change a database record through tree" do
|
28
|
+
assert_equal "another", Setting.find_by_key("pollard").value
|
29
|
+
Settings["pollard"] = "first change"
|
30
|
+
assert_equal "first change", Setting.find_by_key("pollard").value
|
31
|
+
end
|
32
|
+
|
33
|
+
should "NOT be able to change a database record independently of the tree" do
|
34
|
+
setting = Setting.find_by_key("pollard")
|
35
|
+
assert_equal "another", setting.value
|
36
|
+
setting.value = "changed!"
|
37
|
+
setting.save
|
38
|
+
|
39
|
+
assert_equal "changed!", Setting.find_by_key("pollard").value
|
40
|
+
assert_equal "changed!", Settings["pollard"].value
|
41
|
+
end
|
42
|
+
|
43
|
+
context "get value" do
|
44
|
+
|
45
|
+
setup do
|
46
|
+
Settings.store = :db
|
47
|
+
Settings.clear(:hard => true)
|
48
|
+
end
|
49
|
+
|
50
|
+
should "be able to get a previously created setting from the db" do
|
51
|
+
assert_equal Cockpit::TreeHash.new, Settings.tree
|
52
|
+
Setting.create(:key => "lantial", :value => "custom value")
|
53
|
+
assert_equal "custom value", Settings["lantial"].value
|
54
|
+
assert_equal({:lantial => {:type => :string, :value => "custom value"}}, Settings.tree)
|
55
|
+
end
|
56
|
+
|
57
|
+
should "be able to save objects using other syntax" do
|
58
|
+
key = "im.a.nested.key"
|
59
|
+
value = "wow"
|
60
|
+
assert_equal nil, Setting.find_by_key(key)
|
61
|
+
Settings.im.a.nested.key = value
|
62
|
+
assert_equal value, Settings(key).value
|
63
|
+
# PENDING!
|
64
|
+
# assert_equal value, Setting.find_by_key(key)
|
65
|
+
Settings("im.nesting.myself" => "here")
|
66
|
+
assert_equal "here", Setting.find_by_key("im.nesting.myself").value
|
67
|
+
Settings["me.too.please"] = "let me in"
|
68
|
+
assert_equal "let me in", Setting.find_by_key("me.too.please").value
|
69
|
+
Settings.set("finally.me" => "too")
|
70
|
+
assert_equal "too", Setting.find_by_key("finally.me").value
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
context "should not save large tree to database" do
|
76
|
+
|
77
|
+
setup do
|
78
|
+
Settings.clear
|
79
|
+
load_settings
|
80
|
+
end
|
81
|
+
|
82
|
+
should "find nested objects in database" do
|
83
|
+
assert_equal nil, Setting.find_by_key("site.title")
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
context "should find setting" do
|
89
|
+
|
90
|
+
setup do
|
91
|
+
Settings.store = :db
|
92
|
+
Settings("lantial" => "saved")
|
93
|
+
end
|
94
|
+
|
95
|
+
should "find the thing" do
|
96
|
+
setting = Setting.find("lantial")
|
97
|
+
assert setting
|
98
|
+
end
|
99
|
+
|
100
|
+
teardown do
|
101
|
+
Settings.store = :memory
|
102
|
+
end
|
103
|
+
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
107
|
+
|
108
|
+
should "be able to switch the store without modifying the tree" do
|
109
|
+
# pending
|
110
|
+
end
|
111
|
+
|
112
|
+
context "type casting" do
|
113
|
+
|
114
|
+
setup do
|
115
|
+
@datetime = DateTime.now
|
116
|
+
@time = Time.now
|
117
|
+
Settings.store = :db
|
118
|
+
Setting.detonate
|
119
|
+
Settings["a.string"] = "string"
|
120
|
+
Settings["a.boolean"] = true
|
121
|
+
Settings["a.false_boolean"] = false
|
122
|
+
Settings["a.integer"] = 10
|
123
|
+
Settings["a.float"] = 10.11
|
124
|
+
Settings["a.datetime"] = @datetime
|
125
|
+
Settings["a.time"] = @time
|
126
|
+
end
|
127
|
+
|
128
|
+
should "parse types" do
|
129
|
+
assert_equal "string", Setting.find("a.string").value
|
130
|
+
assert_equal true, Setting.find("a.boolean").value
|
131
|
+
assert_equal false, Setting.find("a.false_boolean").value
|
132
|
+
assert_equal 10, Setting.find("a.integer").value
|
133
|
+
assert_equal 10.11, Setting.find("a.float").value
|
134
|
+
assert_equal @time.to_s, Setting.find("a.time").value.to_s
|
135
|
+
end
|
136
|
+
|
137
|
+
should "parse datetime" do
|
138
|
+
# close but not quite
|
139
|
+
# assert_equal @datetime, Setting.find("a.datetime").value
|
140
|
+
# pending
|
141
|
+
end
|
142
|
+
|
143
|
+
teardown do
|
144
|
+
Setting.detonate
|
145
|
+
end
|
146
|
+
|
147
|
+
end
|
148
|
+
|
149
|
+
teardown { Settings.clear }
|
150
|
+
|
151
|
+
end
|
152
|
+
|
153
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'test_helper')
|
2
|
+
|
3
|
+
class SettingsTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
context "Settings" do
|
6
|
+
context "acts_as_configurable module" do
|
7
|
+
|
8
|
+
setup do
|
9
|
+
@user = User.new
|
10
|
+
end
|
11
|
+
|
12
|
+
should "have added settings to global hash" do
|
13
|
+
assert User.settings
|
14
|
+
assert_equal "Lance", @user.settings.name.value
|
15
|
+
assert_equal({:type => :string, :value => "red"}, @user.settings.favorite.color)
|
16
|
+
end
|
17
|
+
|
18
|
+
should "be able to set value" do
|
19
|
+
Settings.user.login = "LOGIN"
|
20
|
+
assert_equal({:type=>:string, :value => "LOGIN"}, Settings.user.login)
|
21
|
+
assert_equal "LOGIN", Settings.user.login.value
|
22
|
+
end
|
23
|
+
|
24
|
+
context "multiple users, custom settings" do
|
25
|
+
|
26
|
+
setup do
|
27
|
+
Settings.clear(:except => [:user])
|
28
|
+
@user_a = User.create
|
29
|
+
@user_b = User.create
|
30
|
+
@now = Time.now
|
31
|
+
end
|
32
|
+
|
33
|
+
should "be able to change settings for user instances" do
|
34
|
+
@user_a.settings[:im_a] = "yes"
|
35
|
+
@user_b.settings[:im_b] = "yippee"
|
36
|
+
assert_equal "yes", @user_a.settings(:im_a).value
|
37
|
+
assert_equal "yippee", @user_b.settings.im_b.value
|
38
|
+
assert_not_equal @user_a.settings, Settings(:user)
|
39
|
+
assert_not_equal "yes", Settings(:user).im_a.value # global settings not changed
|
40
|
+
end
|
41
|
+
|
42
|
+
should "be able to save the user" do
|
43
|
+
@user_a.settings.store = :db
|
44
|
+
@user_a.settings["im_a.having.fun"] = "lance"
|
45
|
+
@user_a.settings["im_a.having.a.good.time"] = @now
|
46
|
+
assert @user_a.valid?
|
47
|
+
assert_equal "lance", Setting.find_by_key("im_a.having.fun").value
|
48
|
+
assert_equal @user_a, Setting.find_by_key("im_a.having.fun").configurable
|
49
|
+
assert_equal @user_a, Setting.find_by_key("im_a.having.a.good.time").configurable
|
50
|
+
assert_equal @now.to_s, Setting.find_by_key("im_a.having.a.good.time").value.to_s
|
51
|
+
# global settings should be unchanged
|
52
|
+
assert_kind_of Cockpit::Store::Database, @user_a.settings.store
|
53
|
+
#assert_kind_of Cockpit::Store::Memory, Settings.store
|
54
|
+
end
|
55
|
+
|
56
|
+
teardown do
|
57
|
+
@user_a.settings.store = :memory
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
teardown { Settings.clear(:except => [:user]) }
|
65
|
+
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|