konfig 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -13,7 +13,7 @@ Konfig gives you a better, easier way to manage these config files.
13
13
 
14
14
  It works like this:
15
15
 
16
- > mkidr ./config/settings
16
+ > mkdir ./config/settings
17
17
  > cat "bar: baz" > ./config/settings/foo.yml
18
18
  > rails console
19
19
 
@@ -44,7 +44,7 @@ This code has access to all of the data in the template.
44
44
 
45
45
  one: 1
46
46
  two: 2
47
- one_plus_two: << data[:one] + data[:two]
47
+ one_plus_two: `data[:one] + data[:two]`
48
48
 
49
49
  == Helpers
50
50
 
@@ -59,7 +59,7 @@ You can also access helper methods in your embedded code.
59
59
  - ["Blue", "#0000ff"]
60
60
 
61
61
  # This returns {"#ff0000" => "Red", ...etc }
62
- color_names: << names_by_value(:colors)
62
+ color_names: `names_by_value(:colors)`
63
63
 
64
64
 
65
65
  == Rails 3
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{konfig}
8
- s.version = "0.1.1"
8
+ s.version = "0.2.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-05-27}
12
+ s.date = %q{2010-05-28}
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 = [
@@ -25,13 +25,17 @@ Gem::Specification.new do |s|
25
25
  "VERSION",
26
26
  "konfig.gemspec",
27
27
  "lib/konfig.rb",
28
+ "lib/konfig/adapter.rb",
28
29
  "lib/konfig/evaluator.rb",
29
30
  "lib/konfig/helpers.rb",
30
- "lib/konfig/railtie.rb",
31
+ "lib/konfig/rails/adapters/asset_expansion_adapter.rb",
32
+ "lib/konfig/rails/adapters/smtp_adapter.rb",
33
+ "lib/konfig/rails/railtie.rb",
31
34
  "lib/konfig/store.rb",
32
35
  "test/fixtures/dynamic.yml",
33
36
  "test/fixtures/static.yml",
34
37
  "test/helper.rb",
38
+ "test/test_adapter.rb",
35
39
  "test/test_evaluator.rb",
36
40
  "test/test_konfig.rb",
37
41
  "test/test_store.rb"
@@ -43,6 +47,7 @@ Gem::Specification.new do |s|
43
47
  s.summary = %q{Yaml config file manager for rails 3 apps}
44
48
  s.test_files = [
45
49
  "test/helper.rb",
50
+ "test/test_adapter.rb",
46
51
  "test/test_evaluator.rb",
47
52
  "test/test_konfig.rb",
48
53
  "test/test_store.rb"
@@ -1,8 +1,9 @@
1
1
  require 'konfig/evaluator'
2
2
  require 'konfig/store'
3
+ require 'konfig/adapter'
3
4
 
4
5
  if defined?(::Rails::Railtie)
5
- require 'konfig/railtie'
6
+ require 'konfig/rails/railtie'
6
7
  end
7
8
 
8
9
  # Provides a global accessor for configuration.
@@ -29,4 +30,8 @@ module Konfig
29
30
  (@default_store || self).inspect
30
31
  end
31
32
 
33
+ def self.default_store
34
+ @default_store
35
+ end
36
+
32
37
  end
@@ -0,0 +1,39 @@
1
+
2
+ module Konfig
3
+
4
+ # All adapters are subclassed from Konfig::Adapter
5
+ class Adapter
6
+
7
+ class << self
8
+
9
+ def inherited(child_class)
10
+ children << child_class
11
+ end
12
+
13
+ # Get all child classes
14
+ # @return [Array] an array of child classes
15
+ def children
16
+ @children ||= []
17
+ end
18
+
19
+ # Remove child classes from registry
20
+ def clear_children
21
+ @children = []
22
+ end
23
+
24
+ # Evokes the send method on all child classes,
25
+ # and passes the args along
26
+ def send_to_children(*args)
27
+ children.each { |c| c.send(*args) }
28
+ end
29
+
30
+ # Instanciates all child classes and envokes
31
+ # a method via obj.send
32
+ def create_and_send_to_children(*args)
33
+ children.each { |c| c.new.send(*args) }
34
+ end
35
+
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,22 @@
1
+ module Konfig
2
+ class AssetExpansionAdapter < Adapter
3
+ def adapt(data)
4
+ javascripts(data)
5
+ stylesheets(data)
6
+ end
7
+
8
+ def javascripts(data)
9
+ return unless (d = data[:_javascript_expansions])
10
+ ActionView::Helpers::AssetTagHelper.register_javascript_expansion(d)
11
+ c = d[:_adapted] = true
12
+ Rails.logger.info "[Konfig] Loaded javascript expansions"
13
+ end
14
+
15
+ def stylesheets(data)
16
+ return unless (d = data[:_stylesheet_expansions])
17
+ ActionView::Helpers::AssetTagHelper.register_stylesheet_expansion(d)
18
+ c = d[:_adapted] = true
19
+ Rails.logger.info "[Konfig] Loaded stylesheet expansions"
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,14 @@
1
+ module Konfig
2
+ class SmtpAdapter < Adapter
3
+ def adapt(data)
4
+ begin
5
+ c = data[:_smtp][Rails.env]
6
+ ActionMailer::Base.smtp_settings = c.symbolize_keys
7
+
8
+ c = data[:_smtp][:_adapted] = true
9
+ Rails.logger.info "[Konfig] Loaded SMTP setting"
10
+ rescue
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,49 @@
1
+ require 'konfig'
2
+ require 'rails'
3
+
4
+
5
+ module Konfig
6
+
7
+ # @return [String] returns the path where config files are located
8
+ def self.path
9
+ @path = File.join(Rails.root, "config", "settings")
10
+ end
11
+
12
+ class InitializeKonfig < Rails::Railtie
13
+ initializer "initialize_konfig.configure_rails_initialization" do
14
+
15
+ path = Konfig.path
16
+
17
+ if File.directory?(path)
18
+ load_settings(path)
19
+ else
20
+ Rails.logger.warn "[Konfig Warning] Unable to access #{ path }. Please make sure the directory exists and has the correct permissions."
21
+ end
22
+ end
23
+
24
+ private
25
+
26
+ # Set up the Konfig system
27
+ # @param [String] path the path to the directory containing config files
28
+ def load_settings(path)
29
+
30
+ # Load the data files
31
+ Konfig.load_directory(path)
32
+
33
+ # Load all adapters
34
+ built_in_adapters = File.join(File.dirname(__FILE__), 'adapters', '*.rb')
35
+ require_all built_in_adapters
36
+
37
+ user_adapters = File.join(path, 'adapters', '*_adapter.rb')
38
+ require_all user_adapters
39
+
40
+ # Apply the adapters to the data
41
+ Adapter.create_and_send_to_children :adapt, Konfig.default_store.data
42
+ end
43
+
44
+ def require_all(path)
45
+ Dir[path].each { |file| require file }
46
+ end
47
+
48
+ end
49
+ end
@@ -46,13 +46,17 @@ module Konfig
46
46
  @data.inspect
47
47
  end
48
48
 
49
+ def data
50
+ @data
51
+ end
52
+
49
53
  protected
50
54
 
51
55
  def process(piece, evaluator)
52
56
  piece.each do |k, v|
53
57
  if v.is_a?(Hash)
54
58
  piece[k] = process(v, evaluator)
55
- elsif v.is_a?(String) && v.strip =~ /\A<<(.*)\z/
59
+ elsif v.is_a?(String) && v.strip =~ /\A`(.*)`\z/
56
60
  piece[k] = evaluator.run($1)
57
61
  end
58
62
  end
@@ -1,8 +1,8 @@
1
- one_plus_one: << 1 + 1
1
+ one_plus_one: ` 1 + 1 `
2
2
  five: 5
3
3
  six: 6
4
- five_times_six: << data[:five] * data['six']
4
+ five_times_six: ` data[:five] * data['six'] `
5
5
 
6
6
  parent:
7
7
  child: 5
8
- child_plus_one: << data[:parent][:child] + 1
8
+ child_plus_one: ` data[:parent][:child] + 1 `
@@ -0,0 +1,63 @@
1
+ require 'helper'
2
+
3
+ class TestEvaluator < Test::Unit::TestCase
4
+
5
+ Konfig::Adapter.clear_children
6
+
7
+ context "Adapter with two subclasses" do
8
+
9
+ setup do
10
+
11
+ class FirstAdapter < Konfig::Adapter
12
+
13
+ def instance_append(value)
14
+ value << 9
15
+ end
16
+
17
+ def self.append(value)
18
+ value << 2
19
+ end
20
+ end
21
+
22
+ class SecondAdapter < Konfig::Adapter
23
+
24
+ def instance_append(value)
25
+ value << 8
26
+ end
27
+
28
+ def self.append(value)
29
+ value << 4
30
+ end
31
+ end
32
+ end
33
+
34
+ should "have two registered subclasses" do
35
+ assert_equal 2, Konfig::Adapter.children.size
36
+ end
37
+
38
+ should "have the correct subclasses in registry" do
39
+ assert_equal FirstAdapter, Konfig::Adapter.children[0]
40
+ assert_equal SecondAdapter, Konfig::Adapter.children[1]
41
+ end
42
+
43
+ should "not have bleed over to child classes" do
44
+ assert_equal 0, FirstAdapter.children.size
45
+ assert_equal 0, SecondAdapter.children.size
46
+ end
47
+
48
+ should "be able to send method calls to children" do
49
+ values = []
50
+ Konfig::Adapter.send_to_children(:append, values)
51
+ assert_equal [2, 4], values
52
+ end
53
+
54
+ should "be able to instanciate all children, and send" do
55
+ values = []
56
+ Konfig::Adapter.create_and_send_to_children(:instance_append, values)
57
+ assert_equal [9, 8], values
58
+ end
59
+
60
+ end
61
+
62
+ end
63
+
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 1
8
- - 1
9
- version: 0.1.1
7
+ - 2
8
+ - 0
9
+ version: 0.2.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-05-27 00:00:00 -05:00
17
+ date: 2010-05-28 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -47,13 +47,17 @@ files:
47
47
  - VERSION
48
48
  - konfig.gemspec
49
49
  - lib/konfig.rb
50
+ - lib/konfig/adapter.rb
50
51
  - lib/konfig/evaluator.rb
51
52
  - lib/konfig/helpers.rb
52
- - lib/konfig/railtie.rb
53
+ - lib/konfig/rails/adapters/asset_expansion_adapter.rb
54
+ - lib/konfig/rails/adapters/smtp_adapter.rb
55
+ - lib/konfig/rails/railtie.rb
53
56
  - lib/konfig/store.rb
54
57
  - test/fixtures/dynamic.yml
55
58
  - test/fixtures/static.yml
56
59
  - test/helper.rb
60
+ - test/test_adapter.rb
57
61
  - test/test_evaluator.rb
58
62
  - test/test_konfig.rb
59
63
  - test/test_store.rb
@@ -89,6 +93,7 @@ specification_version: 3
89
93
  summary: Yaml config file manager for rails 3 apps
90
94
  test_files:
91
95
  - test/helper.rb
96
+ - test/test_adapter.rb
92
97
  - test/test_evaluator.rb
93
98
  - test/test_konfig.rb
94
99
  - test/test_store.rb
@@ -1,19 +0,0 @@
1
- require 'konfig'
2
- require 'rails'
3
-
4
- module Konfig
5
- class InitializeKonfig < Rails::Railtie
6
- initializer "initialize_konfig.configure_rails_initialization" do
7
-
8
- path = File.join(Rails.root, "config", "settings")
9
- Konfig.load_directory(path)
10
-
11
- begin
12
- c = Konfig[:email][:smtp][Rails.env]
13
- ActionMailer::Base.smtp_settings = c.symbolize_keys
14
- rescue
15
- end
16
-
17
- end
18
- end
19
- end