rock_config 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -1,2 +1,7 @@
1
1
  #!/usr/bin/env rake
2
2
  require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task :default => :spec
@@ -0,0 +1,21 @@
1
+ module RockConfig
2
+ class Config
3
+ def initialize(hash)
4
+ @hash = hash
5
+ end
6
+
7
+ def method_missing(*args, &block)
8
+ if value = @hash[args.first.to_s]
9
+ if Hash === value
10
+ Config.new(value)
11
+ else
12
+ value
13
+ end
14
+ end
15
+ end
16
+
17
+ def raw
18
+ @hash.dup
19
+ end
20
+ end
21
+ end
@@ -1,10 +1,10 @@
1
1
  module RockConfig
2
2
  class Configuration
3
- attr_accessor :scanned_directories
3
+ attr_accessor :scanned_directories, :config_loaders
4
4
 
5
5
  def initialize
6
- default_directory = File.join(Dir.pwd, "config")
7
- @scanned_directories = [default_directory]
6
+ @scanned_directories = []
7
+ @config_loaders = []
8
8
  end
9
9
  end
10
10
  end
@@ -1,5 +1,3 @@
1
- require "rock_config/scanner"
2
-
3
1
  module RockConfig
4
2
  class Manager
5
3
  def initialize(configuration, scanner = Scanner)
@@ -9,9 +7,7 @@ module RockConfig
9
7
 
10
8
  def fetch(config_name, environment)
11
9
  if config = @configs[config_name]
12
- config.for_environment environment
13
- else
14
- yield if block_given?
10
+ config.send environment
15
11
  end
16
12
  end
17
13
 
@@ -22,5 +18,6 @@ module RockConfig
22
18
  end
23
19
  end
24
20
  end
21
+
25
22
  end
26
23
  end
@@ -5,22 +5,15 @@ module RockConfig
5
5
  end
6
6
 
7
7
  def find(file_name)
8
- full_file_name = resolve_full_file_name(file_name)
9
-
10
8
  @configuration.scanned_directories.each do |directory|
11
- file = File.join(directory, full_file_name)
12
- if File.exists? file
13
- return File.read file
9
+ @configuration.config_loaders.each do |loader|
10
+ if config = loader.find_at(directory, file_name)
11
+ return config
12
+ end
14
13
  end
15
14
  end
16
15
 
17
- nil
18
- end
19
-
20
- private
21
-
22
- def resolve_full_file_name(file_name)
23
- "#{file_name}.yml"
16
+ raise ConfigNotFound, "Config not found"
24
17
  end
25
18
  end
26
19
  end
@@ -1,3 +1,3 @@
1
1
  module RockConfig
2
- VERSION = "0.0.0"
2
+ VERSION = "0.0.1"
3
3
  end
@@ -0,0 +1,31 @@
1
+ require "yaml"
2
+
3
+ module RockConfig
4
+ class YamlLoader
5
+ def find_at(directory, file_name)
6
+ path = full_path(directory, file_name)
7
+
8
+ if readable?(path)
9
+ return Config.new(load_yaml_from(path))
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def full_path(directory, file_name)
16
+ File.join(directory, full_name(file_name))
17
+ end
18
+
19
+ def full_name(file_name)
20
+ "#{file_name}.yml"
21
+ end
22
+
23
+ def readable?(path)
24
+ File.exists?(path) && File.readable?(path)
25
+ end
26
+
27
+ def load_yaml_from(path)
28
+ YAML.load_file(path)
29
+ end
30
+ end
31
+ end
data/lib/rock_config.rb CHANGED
@@ -2,15 +2,16 @@ require "rock_config/version"
2
2
  require "rock_config/environment_detector"
3
3
  require "rock_config/configuration"
4
4
  require "rock_config/manager"
5
+ require "rock_config/scanner"
6
+ require "rock_config/yaml_loader"
7
+ require "rock_config/config"
5
8
 
6
9
  module RockConfig
7
10
  class ConfigNotFound < Exception; end
8
11
 
9
12
  class << self
10
13
  def for(config_name, environment = detect_environment)
11
- manager.fetch(config_name, environment) do
12
- raise ConfigNotFound, "Config not found"
13
- end
14
+ manager.fetch(config_name, environment)
14
15
  end
15
16
 
16
17
  def configure
@@ -28,7 +29,16 @@ module RockConfig
28
29
  end
29
30
 
30
31
  def configuration
31
- @configuration ||= Configuration.new
32
+ @configuration ||= initialize_configuration
33
+ end
34
+
35
+ def initialize_configuration
36
+ configuration = Configuration.new
37
+
38
+ configuration.scanned_directories << File.join(Dir.pwd, "config")
39
+ configuration.config_loaders << YamlLoader.new
40
+
41
+ configuration
32
42
  end
33
43
 
34
44
  def manager
@@ -0,0 +1,54 @@
1
+ require "spec_helper"
2
+
3
+ module RockConfig
4
+ describe Config do
5
+ it "accepts a hash" do
6
+ hash = {foo: "bar"}
7
+
8
+ config = Config.new(hash)
9
+ config.raw.should eq(hash)
10
+ end
11
+
12
+ it "returns config for given environment" do
13
+ subhash_dev = {"a" => 123}
14
+ subhash_prod = {"a" => 321}
15
+ hash = {
16
+ "development" => subhash_dev,
17
+ "production" => subhash_prod
18
+ }
19
+
20
+ config = Config.new(hash)
21
+ config.development.raw.should eq(subhash_dev)
22
+ config.production.raw.should eq(subhash_prod)
23
+ end
24
+
25
+ it "returns nil when asked for non existing environment" do
26
+ config = Config.new({})
27
+ config.for_environment("I do not exist").should be_nil
28
+ end
29
+
30
+ it "returns correct values when asked for" do
31
+ hash = {
32
+ "development" => {
33
+ "host" => "localhost"
34
+ }
35
+ }
36
+
37
+ config = Config.new(hash)
38
+ config.development.host.should eq("localhost")
39
+ end
40
+
41
+ it "returns correct nested values when asked for" do
42
+ hash = {
43
+ "development" => {
44
+ "elastic" => {
45
+ "host" => "localhost"
46
+ }
47
+ }
48
+ }
49
+
50
+ config = Config.new(hash)
51
+ config.development.elastic.host.should eq("localhost")
52
+ end
53
+ end
54
+ end
@@ -2,16 +2,18 @@ require "spec_helper"
2
2
 
3
3
  module RockConfig
4
4
  describe Configuration do
5
- it "has default scanned directory" do
6
- configuration = Configuration.new
7
- configuration.scanned_directories.count.should eq 1
8
- end
9
-
10
5
  it "accepts scanned directories" do
11
6
  configuration = Configuration.new
12
7
 
13
8
  configuration.scanned_directories << "/custom/path"
14
9
  configuration.scanned_directories.should include "/custom/path"
15
10
  end
11
+
12
+ it "accepts config loaders" do
13
+ configuration = Configuration.new
14
+
15
+ configuration.config_loaders << "duck"
16
+ configuration.config_loaders.should include "duck"
17
+ end
16
18
  end
17
19
  end
@@ -0,0 +1,8 @@
1
+ development
2
+ magic_number: 213
3
+
4
+ test:
5
+ magic_number: 214
6
+
7
+ production:
8
+ magic_number: 215
data/spec/manager_spec.rb CHANGED
@@ -2,35 +2,30 @@ require "spec_helper"
2
2
 
3
3
  module RockConfig
4
4
  describe Manager do
5
- before do
6
- @configuration = Configuration.new
7
- end
5
+ let(:configuration) { Configuration.new }
8
6
 
9
7
  it "returns config if found" do
10
- result = double("Config")
11
- result.should_receive(:for_environment).with("development").and_return("yay")
8
+ result = mock("Config")
9
+ result.should_receive(:send).with("development") { "yay" }
12
10
 
13
- scanner = double("Scanner")
14
- scanner.should_receive(:new).with(@configuration).and_return(scanner)
15
- scanner.should_receive(:find).with("sample").and_return(result)
11
+ scanner = mock("Scanner")
12
+ scanner.should_receive(:new) .with(configuration) { scanner }
13
+ scanner.should_receive(:find).with("sample") { result }
16
14
 
17
- manager = Manager.new(@configuration, scanner)
15
+ manager = Manager.new(configuration, scanner)
18
16
  manager_result = manager.fetch "sample", "development"
19
17
 
20
18
  manager_result.should eq("yay")
21
19
  end
22
20
 
23
- it "yields if config not found" do
24
- scanner = double("Scanner")
25
- scanner.should_receive(:new).with(@configuration).and_return(scanner)
26
- scanner.should_receive(:find).with("sample").and_return(nil)
27
-
28
- manager = Manager.new(@configuration, scanner)
29
- lambda do
30
- manager_result = manager.fetch "sample", "development" do
31
- raise Exception
32
- end
33
- end.should raise_error(Exception)
21
+ it "returns nul if config not found" do
22
+ scanner = mock("Scanner")
23
+ scanner.should_receive(:new) .with(configuration) { scanner }
24
+ scanner.should_receive(:find).with("sample") { nil }
25
+
26
+ manager = Manager.new(configuration, scanner)
27
+
28
+ manager.fetch("sample", "development").should be_nil
34
29
  end
35
30
  end
36
31
  end
@@ -0,0 +1,14 @@
1
+ require "spec_helper"
2
+
3
+ module RockConfig
4
+ describe RockConfig do
5
+ it "it loads valid configs" do
6
+ RockConfig.configure do |config|
7
+ config.scanned_directories << File.join(Dir.pwd, "spec", "fixtures")
8
+ config.config_loaders << YamlLoader.new
9
+ end
10
+
11
+ RockConfig.for("database", "development").magic_number.should eq(213)
12
+ end
13
+ end
14
+ end
data/spec/scanner_spec.rb CHANGED
@@ -2,21 +2,24 @@ require "spec_helper"
2
2
 
3
3
  module RockConfig
4
4
  describe Scanner do
5
- it "finds nothing when the target doesnt exist" do
5
+ let(:configuration) do
6
6
  configuration = Configuration.new
7
- scanner = Scanner.new(configuration)
7
+ configuration.scanned_directories << File.join(Dir.pwd, "config")
8
+ configuration.config_loaders << YamlLoader.new
9
+ configuration
10
+ end
8
11
 
9
- scanner.find("database").should be_nil
12
+ let(:scanner) { Scanner.new(configuration) }
13
+ let(:fixtures_path) { File.join(Dir.pwd, "spec", "fixtures") }
14
+
15
+ it "finds nothing when the target doesnt exist" do
16
+ expect do
17
+ scanner.find("database")
18
+ end.to raise_error(ConfigNotFound)
10
19
  end
11
20
 
12
21
  it "scans additional directories" do
13
- fixtures_path = File.join(Dir.pwd, "spec", "fixtures")
14
-
15
- configuration = Configuration.new
16
22
  configuration.scanned_directories << fixtures_path
17
-
18
- scanner = Scanner.new(configuration)
19
-
20
23
  scanner.find("database").should_not be_nil
21
24
  end
22
25
  end
@@ -0,0 +1,23 @@
1
+ require "spec_helper"
2
+
3
+ module RockConfig
4
+ describe YamlLoader do
5
+ it "loads config when given valid path and filename" do
6
+ directory = File.join(Dir.pwd, "spec", "fixtures")
7
+
8
+ loader = YamlLoader.new
9
+ config = loader.find_at(directory, "database")
10
+
11
+ config.should_not be_nil
12
+ end
13
+
14
+ it "throws exception when given valid path to an invalid file" do
15
+ directory = File.join(Dir.pwd, "spec", "fixtures")
16
+ loader = YamlLoader.new
17
+
18
+ expect do
19
+ config = loader.find_at(directory, "database_invalid")
20
+ end.to raise_error(Psych::SyntaxError)
21
+ end
22
+ end
23
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rock_config
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-29 00:00:00.000000000 Z
12
+ date: 2012-11-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
@@ -73,18 +73,24 @@ files:
73
73
  - README.md
74
74
  - Rakefile
75
75
  - lib/rock_config.rb
76
+ - lib/rock_config/config.rb
76
77
  - lib/rock_config/configuration.rb
77
78
  - lib/rock_config/environment_detector.rb
78
79
  - lib/rock_config/manager.rb
79
80
  - lib/rock_config/scanner.rb
80
81
  - lib/rock_config/version.rb
82
+ - lib/rock_config/yaml_loader.rb
81
83
  - rock_config.gemspec
84
+ - spec/config_spec.rb
82
85
  - spec/configuration_spec.rb
83
86
  - spec/environment_detector_spec.rb
84
87
  - spec/fixtures/database.yml
88
+ - spec/fixtures/database_invalid.yml
85
89
  - spec/manager_spec.rb
90
+ - spec/rock_config_spec.rb
86
91
  - spec/scanner_spec.rb
87
92
  - spec/spec_helper.rb
93
+ - spec/yaml_loader_spec.rb
88
94
  homepage: ''
89
95
  licenses: []
90
96
  post_install_message:
@@ -110,10 +116,14 @@ signing_key:
110
116
  specification_version: 3
111
117
  summary: RockConfig allows you to use custom config files easily.
112
118
  test_files:
119
+ - spec/config_spec.rb
113
120
  - spec/configuration_spec.rb
114
121
  - spec/environment_detector_spec.rb
115
122
  - spec/fixtures/database.yml
123
+ - spec/fixtures/database_invalid.yml
116
124
  - spec/manager_spec.rb
125
+ - spec/rock_config_spec.rb
117
126
  - spec/scanner_spec.rb
118
127
  - spec/spec_helper.rb
128
+ - spec/yaml_loader_spec.rb
119
129
  has_rdoc: