cockpit 0.0.1.7 → 0.1.1

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.
@@ -1,44 +0,0 @@
1
- module Cockpit::Helper
2
-
3
- # always returns either an array or a string
4
- def c(*args)
5
- options = args.extract_options!
6
- result = args.collect {|i| Settings.get(i).value }
7
- result = result.pop if result.length == 1
8
- result.blank? ? nil : result.to_s
9
- end
10
-
11
- def setting_tag(tag)
12
-
13
- end
14
-
15
- def settings_tag(key, &block)
16
- Settings(key).each_setting do |key, attributes, value|
17
-
18
- end
19
- end
20
-
21
- def setting_value(value)
22
- result = case value
23
- when Proc
24
- value.call
25
- when Cockpit::TreeHash
26
- value
27
- else
28
- value
29
- end
30
- end
31
-
32
- def setting_options(attributes)
33
- return {} unless (attributes.is_a?(Hash) && attributes[:options])
34
- options = case attributes[:options]
35
- when Proc
36
- attributes[:options].call
37
- else
38
- attributes[:options]
39
- end
40
- end
41
-
42
- end
43
-
44
- ActionView::Base.send(:include, Cockpit::Helper) if defined?(ActionView)
data/lib/cockpit/store.rb DELETED
@@ -1,103 +0,0 @@
1
- module Cockpit
2
- module Store
3
- class Base
4
- attr_accessor :configurable
5
-
6
- def initialize(*args)
7
- options = args.extract_options!
8
- options.each do |k, v|
9
- self.send("#{k.to_s}=", v) if self.respond_to?("#{k.to_s}=")
10
- end
11
- @configuration = args.first
12
- end
13
-
14
- def configuration
15
- @configuration
16
- end
17
-
18
- def tree=(value)
19
- @tree = value if value.is_a?(TreeHash)
20
- @tree
21
- end
22
-
23
- def tree
24
- @tree ||= TreeHash.new
25
- end
26
-
27
- def clear(options = {})
28
- tree.each do |k, v|
29
- tree.delete(k) unless (options[:except] && options[:except].include?(k.to_sym))
30
- end
31
- end
32
-
33
- def get(path)
34
- tree.get(path)
35
- end
36
- alias_method :[], :get
37
-
38
- def get!(path)
39
- result = get(path)
40
- raise "'#{path.to_s}' was not in Config" if result.blank?
41
- result
42
- end
43
-
44
- def set(value)
45
- return unless value.is_a?(Hash)
46
- value.each { |k,v| set_one(k, v) }
47
- end
48
-
49
- def set_one(key, value)
50
- tree.set(key, value)
51
- end
52
-
53
- def set!(value)
54
- result = set(value)
55
- raise "'#{path.to_s}' was not set in Config" if result.blank?
56
- result
57
- end
58
-
59
- def []=(key, value)
60
- set_one(key => value)
61
- end
62
- end
63
-
64
- class Memory < Base
65
-
66
- end
67
-
68
- class Database < Base
69
-
70
- def clear(options = {})
71
- configuration.setting_class.all.collect(&:destroy) if options[:hard] == true
72
- super(options)
73
- end
74
-
75
- def find_or_create(key)
76
- result = configuration.setting_class.find(key.to_s) rescue nil
77
- result ||= configuration.setting_class.create(:key => key.to_s)
78
- end
79
-
80
- def set_one_with_database(key, value)
81
- setting = find_or_create(key)
82
- cast_as = (setting.cast_as || Cockpit.get_type(value)).to_s
83
- attributes = {:value => value, :cast_as => cast_as}
84
- attributes[:configurable] = self.configurable if self.configurable
85
- setting.update_attributes(attributes)
86
- set_one_without_database(key, Cockpit.type_cast(value, cast_as))
87
- end
88
- alias_method_chain :set_one, :database
89
-
90
- def get(key)
91
- result = super(key)
92
- if result.blank? && setting = find_or_create(key)
93
- set(key => setting.value)
94
- result = super(key)
95
- else
96
-
97
- end
98
- result
99
- end
100
-
101
- end
102
- end
103
- end
@@ -1,116 +0,0 @@
1
- # http://www.daniel-azuma.com/blog/view/z3bqa0t01uugg1/implementing_dsl_blocks
2
- module Cockpit
3
- class TreeHash < Hash
4
-
5
- def initialize
6
- block = Proc.new {|h,k| h[k] = TreeHash.new(&block)}
7
- super &block
8
- end
9
-
10
- def value_type
11
- has_key?(:type) ? self[:type] : :string
12
- end
13
-
14
- def each_setting(&block)
15
- dup.each do |k, v|
16
- atrib = Hash.new
17
- if v.has_key?(:value)
18
- value = clone(v[:value])
19
- atrib = clone(v)
20
- else
21
- v.each do |k, sub_v|
22
- atrib[k] = clone(v[k]) unless sub_v.is_a?(TreeHash)
23
- end
24
- value = clone(v)
25
- end
26
- yield(k.to_s, atrib, value) if block_given?
27
- end
28
- end
29
-
30
- def dup
31
- result = super
32
- result.each do |k,v|
33
- result[k] = v.dup if v.is_a?(TreeHash)
34
- end
35
- result
36
- end
37
-
38
- def children
39
- hash = {}
40
- each do |k, v|
41
- hash[k] = v if v.is_a?(Hash)
42
- end
43
- hash
44
- end
45
-
46
- def get(key)
47
- traverse(key)
48
- end
49
-
50
- def get_attribute(key, attribute)
51
- get(key)[attribute.to_sym]
52
- end
53
-
54
- def set(key, value)
55
- traverse(key, value)
56
- end
57
-
58
- def set_attribute(key, value)
59
- traverse(key, value, false)
60
- end
61
-
62
- def set_attributes(hash)
63
- hash.each { |k, v| set_attribute(k, v) }
64
- self
65
- end
66
-
67
- def to_hash
68
- hash = {}
69
- self.each do |k, v|
70
- hash[k] = v.is_a?(TreeHash) ? v.to_hash : v
71
- end
72
- hash
73
- end
74
-
75
- protected
76
- def traverse(path, value = nil, as_node = true)
77
- path = path.to_s.split('.')
78
- child = path.pop.to_sym
79
- parent = path.inject(self) { |h,k| h[k.to_sym] }
80
- unless value.nil?
81
- if as_node
82
- if parent[child].has_key?(:type)
83
- parent[child][:value] = Cockpit.type_cast(value, parent[child][:type])
84
- else
85
- parent[child][:value] = value
86
- parent[child][:type] = Cockpit.get_type(value)
87
- end
88
- else
89
- parent[child] = value
90
- end
91
- end
92
- parent[child]
93
- end
94
-
95
- def method_missing(meth, *args, &block)
96
- options = args.extract_options!
97
- meth = meth.to_s.gsub("=", "").to_sym
98
- if args.empty?
99
- if self.has_key?(meth)
100
- found = self[meth]
101
- else
102
- found = get(meth).set_attributes(options)
103
- end
104
- found = found.instance_eval(&block) if block_given?
105
- found
106
- else
107
- get(meth).set_attributes({:type => Cockpit.get_type(args.first)}.merge(options).merge(:value => args.first))
108
- end
109
- end
110
-
111
- private
112
- def clone(object)
113
- (object.respond_to?(:dup) ? object.dup : object) rescue object
114
- end
115
- end
116
- end
data/rails/init.rb DELETED
@@ -1 +0,0 @@
1
- require 'cockpit'
@@ -1,228 +0,0 @@
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
@@ -1,153 +0,0 @@
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