configured 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +0 -0
- data/lib/configured/configured.rb +18 -0
- data/lib/configured/settings.rb +21 -0
- data/lib/configured/version.rb +12 -0
- data/lib/configured.rb +3 -0
- data/spec/configured_spec.rb +26 -0
- data/spec/settings_spec.rb +25 -0
- data/spec/spec_helper.rb +15 -0
- metadata +53 -0
data/README.md
ADDED
File without changes
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Configured
|
2
|
+
class << self
|
3
|
+
# Load configuration data stored in YAML.
|
4
|
+
# Filename can be passed as parameter.
|
5
|
+
def in_yaml(filename = "config/database.yml")
|
6
|
+
begin
|
7
|
+
$config_initters ||= {}
|
8
|
+
$config_initters[filename] ||=
|
9
|
+
begin
|
10
|
+
data = YAML::load(File.open filename)
|
11
|
+
Settings.new(data)
|
12
|
+
end
|
13
|
+
rescue
|
14
|
+
raise "Can't find \"#{filename}\" configuration file."
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Configured
|
2
|
+
class Settings
|
3
|
+
# Initialize using required configuration data (YAML::load result, for example).
|
4
|
+
def initialize(configuration_data)
|
5
|
+
@configuration_data = configuration_data
|
6
|
+
end
|
7
|
+
|
8
|
+
# Get settings for the required environment.
|
9
|
+
def for_the(environment)
|
10
|
+
begin
|
11
|
+
settings = @configuration_data[environment] || raise
|
12
|
+
|
13
|
+
settings_hash = {}
|
14
|
+
settings.each_key { |key| settings_hash[key.intern] = settings[key]}
|
15
|
+
settings_hash
|
16
|
+
rescue
|
17
|
+
raise "Can't find \"#{environment}\" section in config."
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/configured.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Configured do
|
4
|
+
describe "Configuration loading" do
|
5
|
+
|
6
|
+
before :each do
|
7
|
+
YAML::stub!(:load)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should load default rails database config file once when it isn't specified in many calls" do
|
11
|
+
File.should_receive(:open).once.with("config/database.yml")
|
12
|
+
2.times { Configured.in_yaml }
|
13
|
+
end
|
14
|
+
|
15
|
+
it "should load specified config file once when it is specified in many calls" do
|
16
|
+
some_config = "test"
|
17
|
+
|
18
|
+
File.should_receive(:open).once.with(some_config)
|
19
|
+
2.times { Configured.in_yaml some_config }
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should raise an exception if there is no such configuration file." do
|
23
|
+
lambda { Configured.in_yaml "bad_file" }.should raise_error
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Configured
|
4
|
+
describe Settings do
|
5
|
+
|
6
|
+
before :each do
|
7
|
+
@test_env_1 = {"setting" => "value", "setting2" => "value2"}
|
8
|
+
@test_env_v = {"setting3" => "value3", "setting4" => "value4"}
|
9
|
+
|
10
|
+
@configuration_fake = {"test" => @test_env_1, "test2" => @test_env_2}
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "#for_the" do
|
14
|
+
it "should return settings from config file as hash, in this format:
|
15
|
+
:setting_name => setting_value for existing environment" do
|
16
|
+
ret = Settings.new(@configuration_fake).for_the("test")
|
17
|
+
ret.should == {:setting => "value", :setting2 => "value2"}
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should raise expception if there is no such environment in config" do
|
21
|
+
lambda {Settings.new(@configuration_fake).for_the("bad-entry")}.should raise_error
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'yaml'
|
3
|
+
|
4
|
+
configured_src = '../../lib/configured'
|
5
|
+
require File.expand_path(configured_src, __FILE__)
|
6
|
+
|
7
|
+
# Rspec settings
|
8
|
+
#RSpec.configure do |config|
|
9
|
+
# config.mock_with :mocha
|
10
|
+
# config.mock_with :flexmock
|
11
|
+
# config.mock_with :rr
|
12
|
+
# config.mock_with :rspec
|
13
|
+
#end
|
14
|
+
|
15
|
+
|
metadata
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: configured
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- nettsundere
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-09-11 00:00:00.000000000Z
|
13
|
+
dependencies: []
|
14
|
+
description: configured. Use your database.yaml in rails initters. For redis, redis-store
|
15
|
+
and e.t.c.
|
16
|
+
email: nettsundere@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/configured.rb
|
22
|
+
- lib/configured/configured.rb
|
23
|
+
- lib/configured/settings.rb
|
24
|
+
- lib/configured/version.rb
|
25
|
+
- spec/configured_spec.rb
|
26
|
+
- spec/settings_spec.rb
|
27
|
+
- spec/spec_helper.rb
|
28
|
+
- README.md
|
29
|
+
homepage: https://github.com/nettsundere/configured
|
30
|
+
licenses: []
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
none: false
|
37
|
+
requirements:
|
38
|
+
- - ! '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
requirements: []
|
48
|
+
rubyforge_project:
|
49
|
+
rubygems_version: 1.8.6
|
50
|
+
signing_key:
|
51
|
+
specification_version: 3
|
52
|
+
summary: configured 1.0.0
|
53
|
+
test_files: []
|