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,65 @@
|
|
1
|
+
module Cockpit
|
2
|
+
class Settings
|
3
|
+
# settings have one direct definition and many child definitions
|
4
|
+
class Spec
|
5
|
+
attr_reader :name, :roots # for "User"
|
6
|
+
|
7
|
+
def initialize(options = {}, &block)
|
8
|
+
@name = options[:name]
|
9
|
+
@store = options[:store]
|
10
|
+
@roots = Cockpit::Settings::Definition.define!(options, &block)
|
11
|
+
end
|
12
|
+
|
13
|
+
# only returns keys that aren't defining a new scope.
|
14
|
+
# so site { title "Hello"; pages 10 } would just return
|
15
|
+
# ["site.title", "site.pages"], excluding "site"
|
16
|
+
def keys
|
17
|
+
@keys ||= roots.map(&:keys).flatten
|
18
|
+
end
|
19
|
+
|
20
|
+
# returns all keys, even the ones defining new scope
|
21
|
+
def all_keys
|
22
|
+
@all_keys ||= roots.map(&:all_keys).flatten
|
23
|
+
end
|
24
|
+
|
25
|
+
def has_key?(key)
|
26
|
+
all_keys.include?(key.to_s)
|
27
|
+
end
|
28
|
+
|
29
|
+
def each(&block)
|
30
|
+
roots.each { |root| root.each(&block) }
|
31
|
+
end
|
32
|
+
|
33
|
+
def map(&block)
|
34
|
+
roots.map { |root| root.map(&block) }
|
35
|
+
end
|
36
|
+
alias_method :collect, :map
|
37
|
+
|
38
|
+
def [](key)
|
39
|
+
definition(key).value rescue nil
|
40
|
+
end
|
41
|
+
|
42
|
+
def definition(key)
|
43
|
+
key = key.to_s
|
44
|
+
return nil if key.empty?
|
45
|
+
roots.each do |root|
|
46
|
+
value = root.child(key)
|
47
|
+
return value unless value.nil?
|
48
|
+
end
|
49
|
+
raise ArgumentError.new("Settings '#{name}' doesn't have key '#{key}'")
|
50
|
+
end
|
51
|
+
|
52
|
+
def to_hash
|
53
|
+
keys.inject({}) do |hash, key|
|
54
|
+
hash[key] = self[key]
|
55
|
+
hash
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def to_tree
|
60
|
+
roots.map(&:to_tree)
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/lib/cockpit/core/store.rb
CHANGED
@@ -3,49 +3,45 @@ module Cockpit
|
|
3
3
|
class << self
|
4
4
|
attr_accessor :stores
|
5
5
|
|
6
|
+
def find(name)
|
7
|
+
return store(name) if store?(name)
|
8
|
+
|
9
|
+
base_path = "#{File.dirname(__FILE__)}/../stores"
|
10
|
+
|
11
|
+
@stores[name.to_sym] = case name.to_sym
|
12
|
+
when :active_record
|
13
|
+
require "#{base_path}/active_record"
|
14
|
+
"::Cockpit::AR"
|
15
|
+
when :mongo
|
16
|
+
require "#{base_path}/mongo"
|
17
|
+
"::Cockpit::Mongo"
|
18
|
+
else
|
19
|
+
require "#{base_path}/memory"
|
20
|
+
"::Cockpit::Memory"
|
21
|
+
end
|
22
|
+
|
23
|
+
store(name)
|
24
|
+
end
|
25
|
+
|
26
|
+
def use(options)
|
27
|
+
eval("::#{find(options[:store])}::Store".gsub(/::[:]+/, "::")).new(options[:record], options[:name])
|
28
|
+
end
|
29
|
+
|
30
|
+
def support(name)
|
31
|
+
eval("::#{find(name)}::Support".gsub(/::[:]+/, "::"))
|
32
|
+
end
|
33
|
+
|
6
34
|
def stores
|
7
35
|
@stores ||= {}
|
8
36
|
end
|
9
37
|
|
10
|
-
def
|
11
|
-
stores[
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
require 'moneta/adapters/mongodb'
|
17
|
-
Moneta::Adapters::MongoDB.new(:collection => store.name)
|
18
|
-
when "active_record"
|
19
|
-
require File.dirname(__FILE__) + '/../moneta/active_record'
|
20
|
-
Moneta::Adapters::ActiveRecord.new(:record => store.scope)
|
21
|
-
when "file"
|
22
|
-
require 'moneta/adapters/basic_file'
|
23
|
-
Moneta::Adapters::BasicFile.new(:path => "./.cockpit")
|
24
|
-
when "redis"
|
25
|
-
require 'moneta/adapters/redis'
|
26
|
-
Moneta::Adapters::Redis.new
|
27
|
-
when "memory"
|
28
|
-
require 'moneta/adapters/memory'
|
29
|
-
Moneta::Adapters::Memory.new
|
30
|
-
when "yaml"
|
31
|
-
require 'moneta/adapters/yaml'
|
32
|
-
Moneta::Adapters::YAML.new
|
33
|
-
end
|
34
|
-
# end
|
35
|
-
stores[store.name.to_s][store.value.to_s]
|
38
|
+
def store(name)
|
39
|
+
stores[name.to_sym]
|
40
|
+
end
|
41
|
+
|
42
|
+
def store?(name)
|
43
|
+
stores.has_key?(name.to_sym)
|
36
44
|
end
|
37
|
-
end
|
38
|
-
|
39
|
-
attr_reader :name, :value, :scope
|
40
|
-
|
41
|
-
def initialize(name, value, scope = nil)
|
42
|
-
@name = name
|
43
|
-
@value = value
|
44
|
-
@scope = scope
|
45
|
-
end
|
46
|
-
|
47
|
-
def adapter
|
48
|
-
self.class.adapter(self)
|
49
45
|
end
|
50
46
|
end
|
51
47
|
end
|
@@ -0,0 +1,128 @@
|
|
1
|
+
begin
|
2
|
+
require "active_record"
|
3
|
+
rescue LoadError
|
4
|
+
abort "You need the activerecord gem in order to use the ActiveRecord cockpit store"
|
5
|
+
end
|
6
|
+
|
7
|
+
module Cockpit
|
8
|
+
module AR
|
9
|
+
module Support
|
10
|
+
def self.included(base)
|
11
|
+
base.class_eval do
|
12
|
+
has_many :settings, :as => :configurable, :class_name => "::Cockpit::AR::Setting", :dependent => :destroy
|
13
|
+
|
14
|
+
unless respond_to?("get")
|
15
|
+
def get(key)
|
16
|
+
cockpit[key]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
unless respond_to?("set")
|
21
|
+
def set(key, value)
|
22
|
+
cockpit[key] = value
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class Setting < ::ActiveRecord::Base
|
30
|
+
set_table_name 'settings'
|
31
|
+
belongs_to :configurable, :polymorphic => true
|
32
|
+
|
33
|
+
def cockpit
|
34
|
+
configurable ? configurable.cockpit : Cockpit::Settings.global
|
35
|
+
end
|
36
|
+
|
37
|
+
def parsed_value
|
38
|
+
JSON.parse(value)['root']
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class Store
|
43
|
+
attr_reader :record, :cache, :context
|
44
|
+
|
45
|
+
def initialize(record, context = "default")
|
46
|
+
@record = record
|
47
|
+
@context = context
|
48
|
+
end
|
49
|
+
|
50
|
+
def key?(key)
|
51
|
+
!!self[key]
|
52
|
+
end
|
53
|
+
|
54
|
+
def has_key?(key)
|
55
|
+
key?(key)
|
56
|
+
end
|
57
|
+
|
58
|
+
def [](key)
|
59
|
+
setting = find_setting(key)
|
60
|
+
setting ? setting.parsed_value : nil
|
61
|
+
end
|
62
|
+
|
63
|
+
def []=(key, value)
|
64
|
+
record.save! if record && record.new_record?
|
65
|
+
|
66
|
+
setting = find_setting(key)
|
67
|
+
attributes = {:value => {'root' => value}.to_json}#.merge(configurable_attributes)
|
68
|
+
|
69
|
+
if setting
|
70
|
+
setting.update_attributes!(attributes)
|
71
|
+
else
|
72
|
+
setting = Setting.new(attributes)
|
73
|
+
setting.key = key
|
74
|
+
record.settings << setting if record
|
75
|
+
setting.save!
|
76
|
+
cache << setting
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def delete(key)
|
81
|
+
setting = find_setting(key)
|
82
|
+
if setting
|
83
|
+
setting.destroy
|
84
|
+
setting.parsed_value
|
85
|
+
record.reload if record
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def clear
|
90
|
+
record.settings.destroy_all
|
91
|
+
end
|
92
|
+
|
93
|
+
def update_setting(setting)
|
94
|
+
cache.map! do |item|
|
95
|
+
item.id == setting.id ? setting : item
|
96
|
+
end if cache
|
97
|
+
end
|
98
|
+
|
99
|
+
def cache
|
100
|
+
if record
|
101
|
+
@cache ||= record.settings.all
|
102
|
+
else
|
103
|
+
@cache ||= Setting.all(:conditions => configurable_attributes)
|
104
|
+
end
|
105
|
+
|
106
|
+
@cache
|
107
|
+
end
|
108
|
+
|
109
|
+
private
|
110
|
+
def find_setting(key)
|
111
|
+
cache.detect { |i| i.key == key }
|
112
|
+
end
|
113
|
+
|
114
|
+
def configurable_attributes
|
115
|
+
conditions = {}
|
116
|
+
if record
|
117
|
+
conditions[:configurable_type] = [record.class.name, record.class.base_class.name]
|
118
|
+
conditions[:configurable_id] = record.id
|
119
|
+
else
|
120
|
+
conditions[:configurable_type] = nil
|
121
|
+
conditions[:configurable_id] = nil
|
122
|
+
end
|
123
|
+
conditions[:context] = context
|
124
|
+
conditions
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
begin
|
2
|
+
require "mongoid"
|
3
|
+
rescue LoadError
|
4
|
+
abort "You need the mongoid gem in order to use the ActiveRecord cockpit store"
|
5
|
+
end
|
6
|
+
|
7
|
+
module Cockpit
|
8
|
+
module Mongo
|
9
|
+
class Support
|
10
|
+
def included(base)
|
11
|
+
base.class_eval do
|
12
|
+
embeds_many :settings, :as => :configurable
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Setting
|
18
|
+
include Mongoid::Document
|
19
|
+
field :key
|
20
|
+
field :value
|
21
|
+
field :context
|
22
|
+
embedded_in :configurable, :inverse_of => :setting
|
23
|
+
end
|
24
|
+
|
25
|
+
class Store
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cockpit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Lance Pollard
|
@@ -15,11 +15,11 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-09-
|
18
|
+
date: 2010-09-18 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
22
|
-
name:
|
22
|
+
name: defined-by
|
23
23
|
prerelease: false
|
24
24
|
requirement: &id001 !ruby/object:Gem::Requirement
|
25
25
|
none: false
|
@@ -42,24 +42,21 @@ extra_rdoc_files: []
|
|
42
42
|
|
43
43
|
files:
|
44
44
|
- README.markdown
|
45
|
-
-
|
46
|
-
- MIT-LICENSE
|
45
|
+
- MIT-LICENSE.markdown
|
47
46
|
- lib/cockpit/core/definition.rb
|
48
|
-
- lib/cockpit/core/
|
47
|
+
- lib/cockpit/core/global.rb
|
48
|
+
- lib/cockpit/core/helpers.rb
|
49
49
|
- lib/cockpit/core/include.rb
|
50
|
+
- lib/cockpit/core/scope.rb
|
50
51
|
- lib/cockpit/core/settings.rb
|
52
|
+
- lib/cockpit/core/spec.rb
|
51
53
|
- lib/cockpit/core/store.rb
|
52
|
-
- lib/cockpit/
|
53
|
-
- lib/cockpit/
|
54
|
-
- lib/cockpit/
|
55
|
-
- lib/cockpit/
|
54
|
+
- lib/cockpit/stores/active_record.rb
|
55
|
+
- lib/cockpit/stores/file_system.rb
|
56
|
+
- lib/cockpit/stores/memory.rb
|
57
|
+
- lib/cockpit/stores/mongo.rb
|
58
|
+
- lib/cockpit/stores/redis.rb
|
56
59
|
- lib/cockpit.rb
|
57
|
-
- test/lib/database.rb
|
58
|
-
- test/lib/user.rb
|
59
|
-
- test/test_active_record.rb
|
60
|
-
- test/test_helper.rb
|
61
|
-
- test/test_mongo.rb
|
62
|
-
- test/test_stores.rb
|
63
60
|
has_rdoc: true
|
64
61
|
homepage: http://github.com/viatropos/cockpit
|
65
62
|
licenses: []
|
data/Rakefile
DELETED
@@ -1,79 +0,0 @@
|
|
1
|
-
require 'rake'
|
2
|
-
require "rake/rdoctask"
|
3
|
-
require 'rake/gempackagetask'
|
4
|
-
|
5
|
-
spec = Gem::Specification.new do |s|
|
6
|
-
s.name = "cockpit"
|
7
|
-
s.authors = ["Lance Pollard"]
|
8
|
-
s.version = "0.1.1"
|
9
|
-
s.summary = "Super DRY Configuration Management for Ruby, Rails, and Sinatra Apps. With Pluggable NoSQL/SQL backends using Moneta"
|
10
|
-
s.homepage = "http://github.com/viatropos/cockpit"
|
11
|
-
s.email = "lancejpollard@gmail.com"
|
12
|
-
s.description = "Super DRY Configuration for Ruby, Rails, and Sinatra Apps. With Pluggable NoSQL/SQL backends using Moneta"
|
13
|
-
s.has_rdoc = false
|
14
|
-
s.rubyforge_project = "cockpit"
|
15
|
-
s.platform = Gem::Platform::RUBY
|
16
|
-
s.files = %w(README.markdown Rakefile MIT-LICENSE) + Dir["{lib,test,app}/**/*"] - Dir["test/tmp"]
|
17
|
-
s.require_path = "lib"
|
18
|
-
s.add_dependency("moneta")
|
19
|
-
end
|
20
|
-
|
21
|
-
Rake::GemPackageTask.new(spec) do |pkg|
|
22
|
-
pkg.gem_spec = spec
|
23
|
-
pkg.package_dir = "pkg"
|
24
|
-
end
|
25
|
-
|
26
|
-
desc 'run unit tests'
|
27
|
-
task :test do
|
28
|
-
Dir["test/**/*"].each do |file|
|
29
|
-
next unless File.basename(file) =~ /test_/
|
30
|
-
next unless File.extname(file) == ".rb"
|
31
|
-
system "ruby #{file}"
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
desc "Create .gemspec file (useful for github)"
|
36
|
-
task :gemspec do
|
37
|
-
File.open("pkg/#{spec.name}.gemspec", "w") do |f|
|
38
|
-
f.puts spec.to_ruby
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
desc "Build the gem into the current directory"
|
43
|
-
task :gem => :gemspec do
|
44
|
-
`gem build pkg/#{spec.name}.gemspec`
|
45
|
-
end
|
46
|
-
|
47
|
-
desc "Publish gem to rubygems"
|
48
|
-
task :publish => [:package] do
|
49
|
-
%x[gem push pkg/#{spec.name}-#{spec.version}.gem]
|
50
|
-
end
|
51
|
-
|
52
|
-
desc "Print a list of the files to be put into the gem"
|
53
|
-
task :manifest do
|
54
|
-
File.open("Manifest", "w") do |f|
|
55
|
-
spec.files.each do |file|
|
56
|
-
f.puts file
|
57
|
-
end
|
58
|
-
end
|
59
|
-
end
|
60
|
-
|
61
|
-
desc "Install the gem locally"
|
62
|
-
task :install => [:package] do
|
63
|
-
File.mkdir("pkg") unless File.exists?("pkg")
|
64
|
-
command = "gem install pkg/#{spec.name}-#{spec.version} --no-ri --no-rdoc"
|
65
|
-
command = "sudo #{command}" if ENV["SUDO"] == true
|
66
|
-
sh %{#{command}}
|
67
|
-
end
|
68
|
-
|
69
|
-
desc "Generate the rdoc"
|
70
|
-
Rake::RDocTask.new do |rdoc|
|
71
|
-
files = ["README.markdown", "lib/**/*.rb"]
|
72
|
-
rdoc.rdoc_files.add(files)
|
73
|
-
rdoc.main = "README.markdown"
|
74
|
-
rdoc.title = spec.summary
|
75
|
-
end
|
76
|
-
|
77
|
-
task :yank do
|
78
|
-
`gem yank #{spec.name} -v #{spec.version}`
|
79
|
-
end
|