konfig 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/konfig.gemspec +7 -2
- data/lib/generators/konfig/USAGE +21 -0
- data/lib/generators/konfig/konfig_generator.rb +31 -0
- data/lib/generators/konfig/templates/konfig.tt +2 -0
- data/lib/konfig.rb +1 -0
- data/lib/konfig/adapter.rb +56 -19
- data/lib/konfig/child_class_manager.rb +51 -0
- data/lib/konfig/rails/adapters/asset_expansion_adapter.rb +8 -15
- data/lib/konfig/rails/adapters/smtp_adapter.rb +36 -8
- data/lib/konfig/rails/railtie.rb +2 -1
- data/test/fixtures/template.yml +5 -0
- data/test/test_adapter.rb +67 -15
- metadata +8 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.0
|
data/konfig.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{konfig}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.3.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Starr Horne"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-06-01}
|
13
13
|
s.description = %q{ Automatically loads yaml config files and makes them available to your rails app.}
|
14
14
|
s.email = %q{starr@chromahq.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -24,8 +24,12 @@ Gem::Specification.new do |s|
|
|
24
24
|
"Rakefile",
|
25
25
|
"VERSION",
|
26
26
|
"konfig.gemspec",
|
27
|
+
"lib/generators/konfig/USAGE",
|
28
|
+
"lib/generators/konfig/konfig_generator.rb",
|
29
|
+
"lib/generators/konfig/templates/konfig.tt",
|
27
30
|
"lib/konfig.rb",
|
28
31
|
"lib/konfig/adapter.rb",
|
32
|
+
"lib/konfig/child_class_manager.rb",
|
29
33
|
"lib/konfig/evaluator.rb",
|
30
34
|
"lib/konfig/helpers.rb",
|
31
35
|
"lib/konfig/rails/adapters/asset_expansion_adapter.rb",
|
@@ -34,6 +38,7 @@ Gem::Specification.new do |s|
|
|
34
38
|
"lib/konfig/store.rb",
|
35
39
|
"test/fixtures/dynamic.yml",
|
36
40
|
"test/fixtures/static.yml",
|
41
|
+
"test/fixtures/template.yml",
|
37
42
|
"test/helper.rb",
|
38
43
|
"test/test_adapter.rb",
|
39
44
|
"test/test_evaluator.rb",
|
@@ -0,0 +1,21 @@
|
|
1
|
+
How to Use Konfig Generator
|
2
|
+
===========================
|
3
|
+
|
4
|
+
This generator can be used to create Konfig files for a number
|
5
|
+
of plugins, as well as internal rails settings like SMTP, Javascript
|
6
|
+
Expansions, etc...
|
7
|
+
|
8
|
+
Usage
|
9
|
+
-----
|
10
|
+
|
11
|
+
To generate a konfig template:
|
12
|
+
|
13
|
+
rails generate konfig [template_name]
|
14
|
+
|
15
|
+
Example
|
16
|
+
-------
|
17
|
+
|
18
|
+
Here's how you would add some smtp settings
|
19
|
+
|
20
|
+
rails generate konfig smtp
|
21
|
+
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
|
3
|
+
class KonfigGenerator < Rails::Generators::Base
|
4
|
+
|
5
|
+
desc "Generates Konfig files"
|
6
|
+
argument :name, :type => :string, :required => false, :desc => "Name of konfig template"
|
7
|
+
|
8
|
+
def generate
|
9
|
+
self.name ? render_template : show_instructions
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.source_root
|
13
|
+
@source_root ||= File.join(File.dirname(__FILE__), 'templates')
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def show_instructions
|
19
|
+
print File.read(File.join(File.dirname(__FILE__), 'USAGE'))
|
20
|
+
end
|
21
|
+
|
22
|
+
def render_template
|
23
|
+
if Konfig::Adapter.template_for(self.name).present?
|
24
|
+
template('konfig.tt', File.join(Konfig.path, "#{ self.name }.yml"))
|
25
|
+
else
|
26
|
+
print("Invalid Template Name: #{ self.name }\n")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
data/lib/konfig.rb
CHANGED
data/lib/konfig/adapter.rb
CHANGED
@@ -1,39 +1,76 @@
|
|
1
|
-
|
2
1
|
module Konfig
|
3
2
|
|
3
|
+
|
4
4
|
# All adapters are subclassed from Konfig::Adapter
|
5
5
|
class Adapter
|
6
6
|
|
7
|
+
include ChildClassManager
|
8
|
+
|
9
|
+
attr_reader :data
|
10
|
+
|
11
|
+
def initialize(data)
|
12
|
+
@data = data
|
13
|
+
end
|
14
|
+
|
7
15
|
class << self
|
8
16
|
|
9
|
-
def
|
10
|
-
|
17
|
+
def has_template(key, options = {})
|
18
|
+
if block_given?
|
19
|
+
content = yield
|
20
|
+
else
|
21
|
+
content = options[:file] && File.read(options[:file])
|
22
|
+
unless content
|
23
|
+
content = options[:content]
|
24
|
+
content = adjust_whitespace(content) unless options[:preserve_indentation]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
Adapter.templates[key] = content
|
29
|
+
end
|
30
|
+
|
31
|
+
def template_for(key)
|
32
|
+
Adapter.templates[key]
|
11
33
|
end
|
12
34
|
|
13
|
-
|
14
|
-
|
15
|
-
def children
|
16
|
-
@children ||= []
|
35
|
+
def templates
|
36
|
+
@templates ||= HashWithIndifferentAccess.new
|
17
37
|
end
|
18
38
|
|
19
|
-
|
20
|
-
|
21
|
-
|
39
|
+
def adjust_whitespace(content)
|
40
|
+
content.gsub!(/\t/, " ")
|
41
|
+
adjustment = content.lines.map { |l| l =~ /^( *)\S+$/; $1 && $1.size }.compact.min
|
42
|
+
content.lines.map { |l| l =~ /^[ ]{#{ adjustment }}(.*)$/; $1.to_s }.join("\n")
|
22
43
|
end
|
23
44
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
45
|
+
end
|
46
|
+
|
47
|
+
protected
|
48
|
+
|
49
|
+
|
50
|
+
def ok(key)
|
51
|
+
data[key][:_status] = :ok
|
52
|
+
log("Loaded #{key}")
|
28
53
|
end
|
29
54
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
children.each { |c| c.new.send(*args) }
|
55
|
+
def error(key)
|
56
|
+
data[key][:_status] = :error
|
57
|
+
log("Error Loading #{key}")
|
34
58
|
end
|
35
59
|
|
60
|
+
def log(message)
|
61
|
+
m = "[Konfig] #{ message }"
|
62
|
+
defined?(Rails) ? Rails.logger.info(m) : puts(m)
|
63
|
+
m
|
64
|
+
end
|
36
65
|
|
37
|
-
|
66
|
+
def using(key, options={})
|
67
|
+
if !data[key]
|
68
|
+
raise(log("Required Konfig key #{key} is missing")) if options[:required]
|
69
|
+
return
|
70
|
+
end
|
71
|
+
|
72
|
+
yield(data[key])
|
73
|
+
ok(key) unless data[key][:_status]
|
74
|
+
end
|
38
75
|
end
|
39
76
|
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Konfig
|
2
|
+
|
3
|
+
# Provides tools for keeping track of a Class's descendents
|
4
|
+
|
5
|
+
module ChildClassManager
|
6
|
+
|
7
|
+
def self.included(base)
|
8
|
+
base.extend(ClassMethods)
|
9
|
+
end
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
|
13
|
+
# Should not be called directly.
|
14
|
+
def inherited(child_class)
|
15
|
+
child_classes << child_class
|
16
|
+
end
|
17
|
+
|
18
|
+
# Get all child classes
|
19
|
+
# @return [Array] an array of child classes
|
20
|
+
def child_classes
|
21
|
+
@child_classes ||= []
|
22
|
+
end
|
23
|
+
|
24
|
+
# Remove child classes from registry
|
25
|
+
def clear_child_classes
|
26
|
+
@child_classes = []
|
27
|
+
end
|
28
|
+
|
29
|
+
# Instanciates all child classes
|
30
|
+
# @return [Array] Instances of all child classes
|
31
|
+
def create_child_instances(*params)
|
32
|
+
@child_instances = child_classes.map { |c| c.new(*params) }
|
33
|
+
end
|
34
|
+
|
35
|
+
# Invoke 'send' on all child instances
|
36
|
+
# @param params Parameters for 'send'
|
37
|
+
def send_to_child_instances(*params)
|
38
|
+
@child_instances.each { |c| c.send(*params) }
|
39
|
+
end
|
40
|
+
|
41
|
+
# Instanciates all child classes
|
42
|
+
# @return [Array] Instances of all child classes
|
43
|
+
def child_instances
|
44
|
+
@child_instances || []
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
@@ -1,22 +1,15 @@
|
|
1
1
|
module Konfig
|
2
2
|
class AssetExpansionAdapter < Adapter
|
3
|
-
def adapt(data)
|
4
|
-
javascripts(data)
|
5
|
-
stylesheets(data)
|
6
|
-
end
|
7
3
|
|
8
|
-
def
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
Rails.logger.info "[Konfig] Loaded javascript expansions"
|
13
|
-
end
|
4
|
+
def adapt
|
5
|
+
using(:_javascript_expansions) do |data|
|
6
|
+
ActionView::Helpers::AssetTagHelper.register_javascript_expansion(data)
|
7
|
+
end
|
14
8
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
c = d[:_adapted] = true
|
19
|
-
Rails.logger.info "[Konfig] Loaded stylesheet expansions"
|
9
|
+
using(:_stylesheet_expansions) do |data|
|
10
|
+
ActionView::Helpers::AssetTagHelper.register_stylesheet_expansion(data)
|
11
|
+
end
|
20
12
|
end
|
13
|
+
|
21
14
|
end
|
22
15
|
end
|
@@ -1,14 +1,42 @@
|
|
1
1
|
module Konfig
|
2
2
|
class SmtpAdapter < Adapter
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
ActionMailer::Base.smtp_settings =
|
7
|
-
|
8
|
-
c = data[:_smtp][:_adapted] = true
|
9
|
-
Rails.logger.info "[Konfig] Loaded SMTP setting"
|
10
|
-
rescue
|
3
|
+
|
4
|
+
def adapt
|
5
|
+
using(:_smtp) do |data|
|
6
|
+
ActionMailer::Base.smtp_settings = data[Rails.env]
|
11
7
|
end
|
12
8
|
end
|
9
|
+
|
10
|
+
# TODO: Implement something like this
|
11
|
+
# configures :smtp do |data|
|
12
|
+
# ActionMailer::Base.smtp_settings = data[Rails.env]
|
13
|
+
# end
|
14
|
+
|
15
|
+
has_template :smtp, :content => %[
|
16
|
+
|
17
|
+
# SMTP Configuration
|
18
|
+
# For more information: http://guides.rails.info/action_mailer_basics.html
|
19
|
+
|
20
|
+
development:
|
21
|
+
enable_starttls_auto: true
|
22
|
+
address: smtp.gmail.com
|
23
|
+
port: 587
|
24
|
+
domain: example.com
|
25
|
+
authentication: :login
|
26
|
+
user_name: example@example.com
|
27
|
+
password: password
|
28
|
+
|
29
|
+
production:
|
30
|
+
enable_starttls_auto: true
|
31
|
+
address: smtp.gmail.com
|
32
|
+
port: 587
|
33
|
+
domain: example.com
|
34
|
+
authentication: :login
|
35
|
+
user_name: example@example.com
|
36
|
+
password: password
|
37
|
+
|
38
|
+
test:
|
39
|
+
]
|
40
|
+
|
13
41
|
end
|
14
42
|
end
|
data/lib/konfig/rails/railtie.rb
CHANGED
@@ -38,7 +38,8 @@ module Konfig
|
|
38
38
|
require_all user_adapters
|
39
39
|
|
40
40
|
# Apply the adapters to the data
|
41
|
-
Adapter.
|
41
|
+
Adapter.create_child_instances(Konfig.default_store.data)
|
42
|
+
Adapter.send_to_child_instances :adapt
|
42
43
|
end
|
43
44
|
|
44
45
|
def require_all(path)
|
data/test/test_adapter.rb
CHANGED
@@ -2,10 +2,10 @@ require 'helper'
|
|
2
2
|
|
3
3
|
class TestEvaluator < Test::Unit::TestCase
|
4
4
|
|
5
|
-
Konfig::Adapter.clear_children
|
6
|
-
|
7
5
|
context "Adapter with two subclasses" do
|
8
6
|
|
7
|
+
Konfig::Adapter.clear_child_classes
|
8
|
+
|
9
9
|
setup do
|
10
10
|
|
11
11
|
class FirstAdapter < Konfig::Adapter
|
@@ -32,29 +32,81 @@ class TestEvaluator < Test::Unit::TestCase
|
|
32
32
|
end
|
33
33
|
|
34
34
|
should "have two registered subclasses" do
|
35
|
-
assert_equal 2, Konfig::Adapter.
|
35
|
+
assert_equal 2, Konfig::Adapter.child_classes.size
|
36
36
|
end
|
37
37
|
|
38
38
|
should "have the correct subclasses in registry" do
|
39
|
-
assert_equal FirstAdapter, Konfig::Adapter.
|
40
|
-
assert_equal SecondAdapter, Konfig::Adapter.
|
39
|
+
assert_equal FirstAdapter, Konfig::Adapter.child_classes[0]
|
40
|
+
assert_equal SecondAdapter, Konfig::Adapter.child_classes[1]
|
41
41
|
end
|
42
42
|
|
43
43
|
should "not have bleed over to child classes" do
|
44
|
-
assert_equal 0, FirstAdapter.
|
45
|
-
assert_equal 0, SecondAdapter.
|
44
|
+
assert_equal 0, FirstAdapter.child_classes.size
|
45
|
+
assert_equal 0, SecondAdapter.child_classes.size
|
46
|
+
end
|
47
|
+
|
48
|
+
context "and create_childeren called" do
|
49
|
+
setup do
|
50
|
+
@data = { :a => 1, :b => 2 }
|
51
|
+
@child_instances = Konfig::Adapter.create_child_instances(@data)
|
52
|
+
end
|
53
|
+
|
54
|
+
should "create 2 instances" do
|
55
|
+
assert_equal 2, @child_instances.size
|
56
|
+
end
|
57
|
+
|
58
|
+
should "make instances available through accessor" do
|
59
|
+
assert_equal @child_instances, Konfig::Adapter.child_instances
|
60
|
+
end
|
61
|
+
|
62
|
+
should "create the correct instances" do
|
63
|
+
assert @child_instances[0].is_a?(FirstAdapter)
|
64
|
+
assert @child_instances[1].is_a?(SecondAdapter)
|
65
|
+
end
|
66
|
+
|
67
|
+
should "assign the correct data to child instances" do
|
68
|
+
assert_equal @data, @child_instances[0].data
|
69
|
+
assert_equal @data, @child_instances[1].data
|
70
|
+
end
|
71
|
+
|
72
|
+
should "be able to send method calls to chld instances" do
|
73
|
+
r = []
|
74
|
+
Konfig::Adapter.send_to_child_instances(:instance_append, r)
|
75
|
+
assert_equal [9, 8], r
|
76
|
+
end
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
|
82
|
+
context "User adapter with 3 templates" do
|
83
|
+
|
84
|
+
setup do
|
85
|
+
|
86
|
+
class TemplateAdapter < Konfig::Adapter
|
87
|
+
has_template :inline, :content => "inline"
|
88
|
+
has_template :file, :file => "./test/fixtures/template.yml"
|
89
|
+
has_template :block do
|
90
|
+
"block"
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
should "register 3 templates" do
|
97
|
+
assert_equal 3, Konfig::Adapter.templates.size
|
98
|
+
end
|
99
|
+
|
100
|
+
should "return correct value for inline template" do
|
101
|
+
assert_equal "inline", Konfig::Adapter.templates[:inline]
|
46
102
|
end
|
47
103
|
|
48
|
-
should "
|
49
|
-
|
50
|
-
Konfig::Adapter.send_to_children(:append, values)
|
51
|
-
assert_equal [2, 4], values
|
104
|
+
should "return correct value for file template" do
|
105
|
+
assert_equal File.read("./test/fixtures/template.yml"), Konfig::Adapter.templates[:file]
|
52
106
|
end
|
53
107
|
|
54
|
-
should "
|
55
|
-
|
56
|
-
Konfig::Adapter.create_and_send_to_children(:instance_append, values)
|
57
|
-
assert_equal [9, 8], values
|
108
|
+
should "return correct value for block template" do
|
109
|
+
assert_equal "block", Konfig::Adapter.templates[:block]
|
58
110
|
end
|
59
111
|
|
60
112
|
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
7
|
+
- 3
|
8
8
|
- 0
|
9
|
-
version: 0.
|
9
|
+
version: 0.3.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Starr Horne
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-06-01 00:00:00 -05:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -46,8 +46,12 @@ files:
|
|
46
46
|
- Rakefile
|
47
47
|
- VERSION
|
48
48
|
- konfig.gemspec
|
49
|
+
- lib/generators/konfig/USAGE
|
50
|
+
- lib/generators/konfig/konfig_generator.rb
|
51
|
+
- lib/generators/konfig/templates/konfig.tt
|
49
52
|
- lib/konfig.rb
|
50
53
|
- lib/konfig/adapter.rb
|
54
|
+
- lib/konfig/child_class_manager.rb
|
51
55
|
- lib/konfig/evaluator.rb
|
52
56
|
- lib/konfig/helpers.rb
|
53
57
|
- lib/konfig/rails/adapters/asset_expansion_adapter.rb
|
@@ -56,6 +60,7 @@ files:
|
|
56
60
|
- lib/konfig/store.rb
|
57
61
|
- test/fixtures/dynamic.yml
|
58
62
|
- test/fixtures/static.yml
|
63
|
+
- test/fixtures/template.yml
|
59
64
|
- test/helper.rb
|
60
65
|
- test/test_adapter.rb
|
61
66
|
- test/test_evaluator.rb
|