configoro 1.2.3 → 1.2.4
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile +1 -1
- data/Gemfile.lock +64 -53
- data/{README.textile → README.md} +55 -35
- data/Rakefile +15 -14
- data/VERSION +1 -1
- data/configoro.gemspec +8 -7
- data/lib/configoro.rb +22 -10
- data/lib/configoro/base.rb +12 -9
- data/lib/configoro/hash.rb +122 -124
- data/lib/configoro/railtie.rb +1 -1
- data/lib/configoro/simple.rb +23 -0
- data/spec/spec_helper.rb +1 -2
- metadata +7 -6
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,75 +1,80 @@
|
|
1
1
|
GEM
|
2
2
|
remote: https://rubygems.org/
|
3
3
|
specs:
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
rack (~>
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
builder (2.1.2)
|
4
|
+
actionmailer (3.2.13)
|
5
|
+
actionpack (= 3.2.13)
|
6
|
+
mail (~> 2.5.3)
|
7
|
+
actionpack (3.2.13)
|
8
|
+
activemodel (= 3.2.13)
|
9
|
+
activesupport (= 3.2.13)
|
10
|
+
builder (~> 3.0.0)
|
11
|
+
erubis (~> 2.7.0)
|
12
|
+
journey (~> 1.0.4)
|
13
|
+
rack (~> 1.4.5)
|
14
|
+
rack-cache (~> 1.2)
|
15
|
+
rack-test (~> 0.6.1)
|
16
|
+
sprockets (~> 2.2.1)
|
17
|
+
activemodel (3.2.13)
|
18
|
+
activesupport (= 3.2.13)
|
19
|
+
builder (~> 3.0.0)
|
20
|
+
activerecord (3.2.13)
|
21
|
+
activemodel (= 3.2.13)
|
22
|
+
activesupport (= 3.2.13)
|
23
|
+
arel (~> 3.0.2)
|
24
|
+
tzinfo (~> 0.3.29)
|
25
|
+
activeresource (3.2.13)
|
26
|
+
activemodel (= 3.2.13)
|
27
|
+
activesupport (= 3.2.13)
|
28
|
+
activesupport (3.2.13)
|
29
|
+
i18n (= 0.6.1)
|
30
|
+
multi_json (~> 1.0)
|
31
|
+
arel (3.0.2)
|
32
|
+
builder (3.0.4)
|
34
33
|
diff-lcs (1.2.4)
|
35
|
-
erubis (2.
|
36
|
-
abstract (>= 1.0.0)
|
34
|
+
erubis (2.7.0)
|
37
35
|
git (1.2.5)
|
38
|
-
|
36
|
+
hike (1.2.2)
|
37
|
+
i18n (0.6.1)
|
39
38
|
jeweler (1.8.4)
|
40
39
|
bundler (~> 1.0)
|
41
40
|
git (>= 1.2.5)
|
42
41
|
rake
|
43
42
|
rdoc
|
43
|
+
journey (1.0.4)
|
44
44
|
json (1.7.7)
|
45
|
-
mail (2.
|
46
|
-
activesupport (>= 2.3.6)
|
45
|
+
mail (2.5.3)
|
47
46
|
i18n (>= 0.4.0)
|
48
47
|
mime-types (~> 1.16)
|
49
48
|
treetop (~> 1.4.8)
|
50
49
|
mime-types (1.23)
|
50
|
+
multi_json (1.7.2)
|
51
51
|
polyglot (0.3.3)
|
52
|
-
rack (1.
|
53
|
-
rack-
|
54
|
-
rack (>=
|
55
|
-
rack-
|
52
|
+
rack (1.4.5)
|
53
|
+
rack-cache (1.2)
|
54
|
+
rack (>= 0.4)
|
55
|
+
rack-ssl (1.3.3)
|
56
|
+
rack
|
57
|
+
rack-test (0.6.2)
|
56
58
|
rack (>= 1.0)
|
57
|
-
rails (3.
|
58
|
-
actionmailer (= 3.
|
59
|
-
actionpack (= 3.
|
60
|
-
activerecord (= 3.
|
61
|
-
activeresource (= 3.
|
62
|
-
activesupport (= 3.
|
59
|
+
rails (3.2.13)
|
60
|
+
actionmailer (= 3.2.13)
|
61
|
+
actionpack (= 3.2.13)
|
62
|
+
activerecord (= 3.2.13)
|
63
|
+
activeresource (= 3.2.13)
|
64
|
+
activesupport (= 3.2.13)
|
63
65
|
bundler (~> 1.0)
|
64
|
-
railties (= 3.
|
65
|
-
railties (3.
|
66
|
-
actionpack (= 3.
|
67
|
-
activesupport (= 3.
|
66
|
+
railties (= 3.2.13)
|
67
|
+
railties (3.2.13)
|
68
|
+
actionpack (= 3.2.13)
|
69
|
+
activesupport (= 3.2.13)
|
70
|
+
rack-ssl (~> 1.3.2)
|
68
71
|
rake (>= 0.8.7)
|
69
|
-
|
72
|
+
rdoc (~> 3.4)
|
73
|
+
thor (>= 0.14.6, < 2.0)
|
70
74
|
rake (10.0.4)
|
71
|
-
rdoc (
|
75
|
+
rdoc (3.12.2)
|
72
76
|
json (~> 1.4)
|
77
|
+
redcarpet (2.2.2)
|
73
78
|
rspec (2.13.0)
|
74
79
|
rspec-core (~> 2.13.0)
|
75
80
|
rspec-expectations (~> 2.13.0)
|
@@ -78,7 +83,13 @@ GEM
|
|
78
83
|
rspec-expectations (2.13.0)
|
79
84
|
diff-lcs (>= 1.1.3, < 2.0)
|
80
85
|
rspec-mocks (2.13.1)
|
81
|
-
|
86
|
+
sprockets (2.2.2)
|
87
|
+
hike (~> 1.2)
|
88
|
+
multi_json (~> 1.0)
|
89
|
+
rack (~> 1.0)
|
90
|
+
tilt (~> 1.1, != 1.3.0)
|
91
|
+
thor (0.18.1)
|
92
|
+
tilt (1.3.7)
|
82
93
|
treetop (1.4.12)
|
83
94
|
polyglot
|
84
95
|
polyglot (>= 0.3.1)
|
@@ -89,10 +100,10 @@ PLATFORMS
|
|
89
100
|
ruby
|
90
101
|
|
91
102
|
DEPENDENCIES
|
92
|
-
RedCloth
|
93
103
|
activesupport
|
94
104
|
bundler
|
95
105
|
jeweler
|
96
106
|
rails (>= 3.0)
|
107
|
+
redcarpet
|
97
108
|
rspec
|
98
109
|
yard
|
@@ -1,15 +1,18 @@
|
|
1
|
-
|
1
|
+
Configoro
|
2
|
+
=========
|
2
3
|
|
3
|
-
|
4
|
-
| *Version* | 1.1 (May 9, 2011) |
|
5
|
-
| *License* | Released under the MIT license. |
|
4
|
+
**Environment-specific configuration data for Rails apps**
|
6
5
|
|
7
|
-
|
6
|
+
| **Author** | Tim Morgan |
|
7
|
+
| **License** | Released under the MIT license. |
|
8
|
+
|
9
|
+
About
|
10
|
+
-----
|
8
11
|
|
9
12
|
Pretty much every Rails app out there needs to store environment-specific
|
10
13
|
configuration data: API URLs, Memcache settings, AWS keys, etc. The "quick fix"
|
11
|
-
approach is usually to dump this information in, e.g.,
|
12
|
-
constants, like
|
14
|
+
approach is usually to dump this information in, e.g., `development.rb` as
|
15
|
+
constants, like `MAILCHIMP_API_URL`. This creates cluttered and unorganized
|
13
16
|
environment files.
|
14
17
|
|
15
18
|
Configoro creates a configuration object that can be accessed as both a hash and
|
@@ -18,83 +21,100 @@ data.
|
|
18
21
|
|
19
22
|
The data is read from YAML files stored alongside the environment files.
|
20
23
|
|
21
|
-
|
24
|
+
Installation
|
25
|
+
------------
|
22
26
|
|
23
27
|
To use this gem, simply add
|
24
28
|
|
25
|
-
|
29
|
+
```` ruby
|
26
30
|
gem 'configoro'
|
27
|
-
|
31
|
+
````
|
28
32
|
|
29
33
|
to your Gemfile, then run
|
30
34
|
|
31
|
-
|
35
|
+
```` ruby
|
32
36
|
rails generate configoro
|
33
|
-
|
37
|
+
````
|
34
38
|
|
35
39
|
to install some default configuration files. Edit these new files with your
|
36
40
|
configuration data.
|
37
41
|
|
38
|
-
|
42
|
+
Usage
|
43
|
+
-----
|
39
44
|
|
40
|
-
Assume your application namespace is
|
41
|
-
would be if you had created your Rails project using
|
42
|
-
can find your namespace in your
|
45
|
+
Assume your application namespace is `MyApp` (which is what it
|
46
|
+
would be if you had created your Rails project using `rails new my_app`). You
|
47
|
+
can find your namespace in your `config/application.rb` file.
|
43
48
|
|
44
49
|
In this case, you would access your configuration using the
|
45
|
-
|
50
|
+
`MyApp::Configuration` object. You can access it as an indifferent hash
|
46
51
|
|
47
|
-
|
52
|
+
```` ruby
|
48
53
|
MyApp::Configuration[:mailchimp_api_url]
|
49
54
|
MyApp::Configuration['mailchimp_api_url']
|
50
|
-
|
55
|
+
````
|
51
56
|
|
52
57
|
or as a struct.
|
53
58
|
|
54
|
-
|
59
|
+
```` ruby
|
55
60
|
MyApp::Configuration.mailchimp_api_url
|
56
|
-
|
61
|
+
````
|
57
62
|
|
58
63
|
If you include any hashes in your configuration YAML files, they will also be
|
59
64
|
accessible as indifferent hashes or structs:
|
60
65
|
|
61
|
-
|
66
|
+
```` ruby
|
62
67
|
MyApp::Configuration.memcache.timeout
|
63
68
|
MyApp::Configuration[:memcache]['timeout']
|
64
69
|
MyApp::Configuration['memcache'].timeout
|
65
|
-
|
70
|
+
````
|
66
71
|
|
67
|
-
|
72
|
+
Configuration Files
|
73
|
+
-------------------
|
68
74
|
|
69
|
-
Configuration is stored within the
|
70
|
-
app. Files ending in ".yml" are loaded from the
|
75
|
+
Configuration is stored within the `config/environments` directory of your Rails
|
76
|
+
app. Files ending in ".yml" are loaded from the `common/` subdirectory and a
|
71
77
|
subdirectory named after the current environment.
|
72
78
|
|
73
79
|
Configuration files are preprocessed as ERB, in the same way that Rails
|
74
|
-
preprocesses
|
75
|
-
e.g.
|
80
|
+
preprocesses `config/database.yml`. This allows dynamic configuration,
|
81
|
+
e.g. `host: <%= ENV["DB_HOST"] || "localhost" %>`.
|
76
82
|
|
77
83
|
Each file goes into its own hash in the configuration. For example, if you
|
78
|
-
placed a file called
|
84
|
+
placed a file called `memcache.yml` within `config/environments/development`,
|
79
85
|
you would be able to access your Memcache timeout using
|
80
|
-
|
86
|
+
`MyApp::Configuration.memcache.timeout`.
|
81
87
|
|
82
88
|
h3. Custom Configuration Locations
|
83
89
|
|
84
90
|
If you need to do your own configuration loading, you can do so using the
|
85
91
|
{Configoro::Hash#<<} method. For example, you could place the following in a
|
86
|
-
Ruby file under
|
92
|
+
Ruby file under `config/initializers`:
|
87
93
|
|
88
|
-
|
94
|
+
```` ruby
|
89
95
|
MyApp::Configuration << "path/to/additional/yaml_file.yml"
|
90
96
|
MyApp::Configuration << { 'additional' => 'configuration' }
|
91
97
|
|
92
98
|
MyApp::Configuration.additional #=> 'configuration'
|
93
|
-
|
99
|
+
````
|
94
100
|
|
95
101
|
Note that if you pass a path to a YAML file, a key will be created to store the
|
96
102
|
contents of the file, named after the file name. If the key already exists, the
|
97
103
|
new values will be deep-merged into the existing values.
|
98
104
|
|
99
|
-
In the example above, the data in the
|
100
|
-
|
105
|
+
In the example above, the data in the `yaml_file.yml` file can be accessed using
|
106
|
+
`MyApp::Configuration.yaml_file`.
|
107
|
+
|
108
|
+
Other Notes
|
109
|
+
-----------
|
110
|
+
|
111
|
+
If you want to use Configoro outside of Rails or your gemset, you can require
|
112
|
+
the `configoro/simple` file. This file defines a subset of the `Configoro`
|
113
|
+
object you can use to access your configuration. You will need to set
|
114
|
+
{Configoro.paths} manually, and the `Configoro::Hash` object will have the same
|
115
|
+
functionality as a normal Hash, without all the bells and whistles described
|
116
|
+
above.
|
117
|
+
|
118
|
+
If you then "upgrade" your environment to a full-fledged Rails or gem-ified
|
119
|
+
environment (perhaps by running your app's `environment.rb` file, be sure to
|
120
|
+
run {Configoro.reset_paths} before requiring the `configoro` gem file.
|
data/Rakefile
CHANGED
@@ -19,13 +19,13 @@ require 'rake'
|
|
19
19
|
require 'jeweler'
|
20
20
|
Jeweler::Tasks.new do |gem|
|
21
21
|
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
22
|
-
gem.name
|
23
|
-
gem.homepage
|
24
|
-
gem.license
|
25
|
-
gem.summary
|
22
|
+
gem.name = 'configoro'
|
23
|
+
gem.homepage = 'http://github.com/RISCfuture/configoro'
|
24
|
+
gem.license = 'MIT'
|
25
|
+
gem.summary = %Q{Configuration object and YAML-based storage for Rails apps}
|
26
26
|
gem.description = %Q{Creates a YourApp::Configuration object whose methods are generated from environment-specific YAML files.}
|
27
|
-
gem.email
|
28
|
-
gem.authors
|
27
|
+
gem.email = 'git@timothymorgan.info'
|
28
|
+
gem.authors = ['Tim Morgan']
|
29
29
|
# dependencies defined in Gemfile
|
30
30
|
end
|
31
31
|
Jeweler::RubygemsDotOrgTasks.new
|
@@ -44,14 +44,15 @@ task :default => :spec
|
|
44
44
|
|
45
45
|
require 'yard'
|
46
46
|
YARD::Rake::YardocTask.new do |doc|
|
47
|
-
doc.options <<
|
48
|
-
doc.options <<
|
49
|
-
doc.options <<
|
50
|
-
doc.options <<
|
51
|
-
doc.options <<
|
52
|
-
doc.options <<
|
53
|
-
|
54
|
-
|
47
|
+
doc.options << '-m' << 'markdown'
|
48
|
+
doc.options << '-M' << 'redcarpet'
|
49
|
+
doc.options << '--protected'
|
50
|
+
doc.options << '--no-private'
|
51
|
+
doc.options << '-r' << 'README.md'
|
52
|
+
doc.options << '-o' << 'doc'
|
53
|
+
doc.options << '--title' << 'Configoro Documentation'
|
54
|
+
|
55
|
+
doc.files = %w(lib/**/* README.md)
|
55
56
|
end
|
56
57
|
|
57
58
|
desc "Generate API documentation"
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.2.
|
1
|
+
1.2.4
|
data/configoro.gemspec
CHANGED
@@ -5,16 +5,16 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "configoro"
|
8
|
-
s.version = "1.2.
|
8
|
+
s.version = "1.2.4"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Tim Morgan"]
|
12
|
-
s.date = "2013-04-
|
12
|
+
s.date = "2013-04-26"
|
13
13
|
s.description = "Creates a YourApp::Configuration object whose methods are generated from environment-specific YAML files."
|
14
14
|
s.email = "git@timothymorgan.info"
|
15
15
|
s.extra_rdoc_files = [
|
16
16
|
"LICENSE.txt",
|
17
|
-
"README.
|
17
|
+
"README.md"
|
18
18
|
]
|
19
19
|
s.files = [
|
20
20
|
".document",
|
@@ -24,7 +24,7 @@ Gem::Specification.new do |s|
|
|
24
24
|
"Gemfile",
|
25
25
|
"Gemfile.lock",
|
26
26
|
"LICENSE.txt",
|
27
|
-
"README.
|
27
|
+
"README.md",
|
28
28
|
"Rakefile",
|
29
29
|
"VERSION",
|
30
30
|
"configoro.gemspec",
|
@@ -33,6 +33,7 @@ Gem::Specification.new do |s|
|
|
33
33
|
"lib/configoro/base.rb",
|
34
34
|
"lib/configoro/hash.rb",
|
35
35
|
"lib/configoro/railtie.rb",
|
36
|
+
"lib/configoro/simple.rb",
|
36
37
|
"spec/configoro/hash_spec.rb",
|
37
38
|
"spec/configoro_spec.rb",
|
38
39
|
"spec/data/config/environments/common/basic.yml",
|
@@ -59,7 +60,7 @@ Gem::Specification.new do |s|
|
|
59
60
|
s.add_development_dependency(%q<rails>, [">= 3.0"])
|
60
61
|
s.add_development_dependency(%q<rspec>, [">= 0"])
|
61
62
|
s.add_development_dependency(%q<yard>, [">= 0"])
|
62
|
-
s.add_development_dependency(%q<
|
63
|
+
s.add_development_dependency(%q<redcarpet>, [">= 0"])
|
63
64
|
s.add_development_dependency(%q<bundler>, [">= 0"])
|
64
65
|
s.add_development_dependency(%q<jeweler>, [">= 0"])
|
65
66
|
else
|
@@ -67,7 +68,7 @@ Gem::Specification.new do |s|
|
|
67
68
|
s.add_dependency(%q<rails>, [">= 3.0"])
|
68
69
|
s.add_dependency(%q<rspec>, [">= 0"])
|
69
70
|
s.add_dependency(%q<yard>, [">= 0"])
|
70
|
-
s.add_dependency(%q<
|
71
|
+
s.add_dependency(%q<redcarpet>, [">= 0"])
|
71
72
|
s.add_dependency(%q<bundler>, [">= 0"])
|
72
73
|
s.add_dependency(%q<jeweler>, [">= 0"])
|
73
74
|
end
|
@@ -76,7 +77,7 @@ Gem::Specification.new do |s|
|
|
76
77
|
s.add_dependency(%q<rails>, [">= 3.0"])
|
77
78
|
s.add_dependency(%q<rspec>, [">= 0"])
|
78
79
|
s.add_dependency(%q<yard>, [">= 0"])
|
79
|
-
s.add_dependency(%q<
|
80
|
+
s.add_dependency(%q<redcarpet>, [">= 0"])
|
80
81
|
s.add_dependency(%q<bundler>, [">= 0"])
|
81
82
|
s.add_dependency(%q<jeweler>, [">= 0"])
|
82
83
|
end
|
data/lib/configoro.rb
CHANGED
@@ -1,17 +1,29 @@
|
|
1
|
-
|
1
|
+
unless defined?(Rails)
|
2
|
+
raise "Configoro must be run in the context of a Rails environment, or require 'configoro/simple' outside of Rails"
|
3
|
+
end
|
2
4
|
|
3
5
|
require 'erb'
|
4
6
|
require 'yaml'
|
5
7
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
8
|
+
require 'bundler'
|
9
|
+
Bundler.setup
|
10
|
+
|
11
|
+
require 'active_support/hash_with_indifferent_access'
|
12
|
+
require 'active_support/core_ext/hash/deep_merge'
|
10
13
|
|
11
|
-
|
12
|
-
require 'configoro/hash'
|
14
|
+
load 'configoro/base.rb'
|
13
15
|
|
14
|
-
|
15
|
-
|
16
|
-
|
16
|
+
module Configoro
|
17
|
+
|
18
|
+
# undo anything done by configoro/simple...
|
19
|
+
begin
|
20
|
+
remove_const :Hash
|
21
|
+
remove_const :HashWithIndifferentAccess
|
22
|
+
rescue NameError
|
23
|
+
# ignored
|
24
|
+
end
|
17
25
|
end
|
26
|
+
load 'configoro/hash.rb'
|
27
|
+
|
28
|
+
load 'configoro/railtie.rb'
|
29
|
+
load "#{File.dirname __FILE__}/../generators/configoro_generator.rb"
|
data/lib/configoro/base.rb
CHANGED
@@ -3,15 +3,8 @@
|
|
3
3
|
|
4
4
|
module Configoro
|
5
5
|
|
6
|
-
# @return [Module] The Rails application namespace; e.g., @MyApp@ for a Rails
|
7
|
-
# app named @MyApp::Application@.
|
8
|
-
|
9
|
-
def self.namespace
|
10
|
-
Object.const_get Rails.application.class.to_s.split('::').first
|
11
|
-
end
|
12
|
-
|
13
6
|
# Creates the configuration dictionary and stores it under
|
14
|
-
#
|
7
|
+
# `MyApp::Configuration` (assuming an application named `MyApp`).
|
15
8
|
|
16
9
|
def self.initialize
|
17
10
|
namespace.const_set :Configuration, load_environment(Rails.env)
|
@@ -42,6 +35,12 @@ module Configoro
|
|
42
35
|
end
|
43
36
|
end
|
44
37
|
|
38
|
+
# Resets any custom configuration paths set using {.paths}.
|
39
|
+
|
40
|
+
def self.reset_paths
|
41
|
+
remove_instance_variable :@paths
|
42
|
+
end
|
43
|
+
|
45
44
|
# Loads the configuration for an environment and returns it as a {Hash}. Use
|
46
45
|
# this method to access Configoro options outside the context of your Rails
|
47
46
|
# app. You will need to configure paths first (see example).
|
@@ -54,13 +53,17 @@ module Configoro
|
|
54
53
|
# Configoro.load_environment(rails_env) #=> { ... }
|
55
54
|
|
56
55
|
def self.load_environment(env)
|
57
|
-
config = Hash.new
|
56
|
+
config = Configoro::Hash.new
|
58
57
|
load_data config, env
|
59
58
|
config
|
60
59
|
end
|
61
60
|
|
62
61
|
private
|
63
62
|
|
63
|
+
def self.namespace
|
64
|
+
Object.const_get Rails.application.class.to_s.split('::').first
|
65
|
+
end
|
66
|
+
|
64
67
|
def self.load_data(config, env)
|
65
68
|
paths.each do |path|
|
66
69
|
Dir.glob("#{path}/common/*.yml").sort.each { |file| config << file }
|
data/lib/configoro/hash.rb
CHANGED
@@ -1,154 +1,152 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
super
|
1
|
+
module Configoro
|
2
|
+
class Hash < HashWithIndifferentAccess
|
3
|
+
|
4
|
+
# @private
|
5
|
+
def initialize(hsh={})
|
6
|
+
if hsh.kind_of?(::Hash) then
|
7
|
+
super()
|
8
|
+
update hsh
|
9
|
+
else
|
10
|
+
super
|
11
|
+
end
|
13
12
|
end
|
14
|
-
end
|
15
13
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
14
|
+
# Deep-merges additional hash entries into this hash.
|
15
|
+
#
|
16
|
+
# @return [Configoro::Hash] This object.
|
17
|
+
# @overload <<(hash)
|
18
|
+
# Adds the entries from another hash.
|
19
|
+
# @param [::Hash] hash The additional keys to add.
|
20
|
+
# @overload <<(path)
|
21
|
+
# Adds the entries from a YAML file. The entries will be added under a
|
22
|
+
# sub-hash named after the YAML file's name.
|
23
|
+
# @param [String] path The path to a YAML file ending in ".yml".
|
24
|
+
# @raise [ArgumentError] If the filename does not end in ".yml".
|
25
|
+
|
26
|
+
def <<(hsh_or_path)
|
27
|
+
case hsh_or_path
|
28
|
+
when String
|
29
|
+
raise ArgumentError, "Only files ending in .yml can be added" unless File.extname(hsh_or_path) == '.yml'
|
30
|
+
return self unless File.exist?(hsh_or_path)
|
31
|
+
data = load_preprocessed_yaml(hsh_or_path)
|
32
|
+
deep_merge! File.basename(hsh_or_path, ".yml") => data
|
33
|
+
when ::Hash
|
34
|
+
deep_merge! hsh_or_path
|
35
|
+
end
|
37
36
|
end
|
38
|
-
end
|
39
37
|
|
40
|
-
|
38
|
+
alias_method :push, :<<
|
41
39
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
40
|
+
# @private
|
41
|
+
def dup
|
42
|
+
Hash.new(self)
|
43
|
+
end
|
46
44
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
45
|
+
# Recursively merges this hash with another hash. All nested hashes are forced
|
46
|
+
# to be `Configoro::Hash` instances.
|
47
|
+
#
|
48
|
+
# @param [::Hash] other_hash The hash to merge into this one.
|
49
|
+
# @return [Configoro::Hash] This object.
|
50
|
+
|
51
|
+
def deep_merge!(other_hash)
|
52
|
+
other_hash.each_pair do |k, v|
|
53
|
+
tv = self[k]
|
54
|
+
self[k] = if v.kind_of?(::Hash) then
|
55
|
+
if tv.kind_of?(::Hash) then
|
56
|
+
Configoro::Hash.new(tv).deep_merge!(v)
|
57
|
+
else
|
58
|
+
Configoro::Hash.new(v)
|
59
|
+
end
|
59
60
|
else
|
60
|
-
|
61
|
+
v
|
61
62
|
end
|
62
|
-
|
63
|
-
|
64
|
-
end
|
63
|
+
end
|
64
|
+
self
|
65
65
|
end
|
66
|
-
self
|
67
|
-
end
|
68
66
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
67
|
+
# @private
|
68
|
+
#
|
69
|
+
# To optimize access, we create a getter method every time we encounter a
|
70
|
+
# key that exists in the hash. If the key is later deleted, the method will
|
71
|
+
# be removed.
|
74
72
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
73
|
+
def method_missing(meth, *args)
|
74
|
+
if include?(meth.to_s) then
|
75
|
+
if args.empty? then
|
76
|
+
create_getter meth
|
77
|
+
else
|
78
|
+
raise ArgumentError, "wrong number of arguments (#{args.size} for 0)"
|
79
|
+
end
|
80
|
+
elsif meth.to_s =~ /^(.+)\?$/ and include?(root_meth = $1) then
|
81
|
+
if args.empty? then
|
82
|
+
!!create_getter(root_meth) #TODO duplication of logic
|
83
|
+
else
|
84
|
+
raise ArgumentError, "wrong number of arguments (#{args.size} for 0)"
|
85
|
+
end
|
85
86
|
else
|
86
|
-
|
87
|
+
super
|
87
88
|
end
|
88
|
-
else
|
89
|
-
super
|
90
89
|
end
|
91
|
-
end
|
92
90
|
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
91
|
+
# @private
|
92
|
+
def inspect
|
93
|
+
"#{to_hash.inspect}:#{self.class.to_s}"
|
94
|
+
end
|
97
95
|
|
98
|
-
|
96
|
+
protected
|
99
97
|
|
100
|
-
|
101
|
-
|
102
|
-
|
98
|
+
def self.new_from_hash_copying_default(hash)
|
99
|
+
Configoro::Hash.new(hash).tap do |new_hash|
|
100
|
+
new_hash.default = hash.default
|
101
|
+
end
|
103
102
|
end
|
104
|
-
end
|
105
103
|
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
104
|
+
def convert_value(value)
|
105
|
+
if value.is_a?(::Hash)
|
106
|
+
self.class.new_from_hash_copying_default(value)
|
107
|
+
elsif value.is_a?(Array)
|
108
|
+
value.dup.replace(value.map { |e| convert_value(e) })
|
109
|
+
else
|
110
|
+
value
|
111
|
+
end
|
113
112
|
end
|
114
|
-
end
|
115
113
|
|
116
|
-
|
114
|
+
private
|
117
115
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
116
|
+
def create_getter(meth)
|
117
|
+
singleton_class.send(:define_method, meth) do
|
118
|
+
if include?(meth.to_s) then
|
119
|
+
self[meth.to_s]
|
120
|
+
else
|
121
|
+
remove_getter meth
|
122
|
+
end
|
124
123
|
end
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
124
|
+
|
125
|
+
singleton_class.send(:define_method, :"#{meth}?") do
|
126
|
+
if include?(meth.to_s) then
|
127
|
+
!!self[meth.to_s]
|
128
|
+
else
|
129
|
+
remove_getter meth
|
130
|
+
end
|
132
131
|
end
|
133
|
-
end
|
134
|
-
|
135
|
-
self[meth.to_s]
|
136
|
-
end
|
137
132
|
|
138
|
-
|
139
|
-
if methods.include?(meth.to_sym) then
|
140
|
-
instance_eval "undef #{meth.to_sym.inspect}"
|
141
|
-
end
|
142
|
-
|
143
|
-
if methods.include?(:"#{meth}?") then
|
144
|
-
instance_eval "undef #{:"#{meth}?".inspect}"
|
133
|
+
self[meth.to_s]
|
145
134
|
end
|
146
135
|
|
147
|
-
|
148
|
-
|
136
|
+
def remove_getter(meth)
|
137
|
+
if methods.include?(meth.to_sym) then
|
138
|
+
instance_eval "undef #{meth.to_sym.inspect}"
|
139
|
+
end
|
149
140
|
|
150
|
-
|
151
|
-
|
152
|
-
|
141
|
+
if methods.include?(:"#{meth}?") then
|
142
|
+
instance_eval "undef #{:"#{meth}?".inspect}"
|
143
|
+
end
|
153
144
|
|
145
|
+
raise NameError, "undefined local variable or method `#{meth}' for #{self.inspect}"
|
146
|
+
end
|
147
|
+
|
148
|
+
def load_preprocessed_yaml(path)
|
149
|
+
YAML.load(ERB.new(IO.read(path)).result)
|
150
|
+
end
|
151
|
+
end
|
154
152
|
end
|
data/lib/configoro/railtie.rb
CHANGED
@@ -0,0 +1,23 @@
|
|
1
|
+
# A stripped-down version of Configoro that works without any gems.
|
2
|
+
|
3
|
+
require 'erb'
|
4
|
+
require 'yaml'
|
5
|
+
|
6
|
+
load File.join(File.dirname(__FILE__), 'base.rb')
|
7
|
+
|
8
|
+
# @private
|
9
|
+
class Configoro::HashWithIndifferentAccess < ::Hash
|
10
|
+
def deep_merge(other_hash)
|
11
|
+
dup.deep_merge!(other_hash)
|
12
|
+
end
|
13
|
+
|
14
|
+
def deep_merge!(other_hash)
|
15
|
+
other_hash.each_pair do |k, v|
|
16
|
+
tv = self[k]
|
17
|
+
self[k] = tv.is_a?(Hash) && v.is_a?(Hash) ? tv.deep_merge(v) : v
|
18
|
+
end
|
19
|
+
self
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
load File.join(File.dirname(__FILE__), 'hash.rb')
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: configoro
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.4
|
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: 2013-04-
|
12
|
+
date: 2013-04-26 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|
@@ -76,7 +76,7 @@ dependencies:
|
|
76
76
|
- !ruby/object:Gem::Version
|
77
77
|
version: '0'
|
78
78
|
- !ruby/object:Gem::Dependency
|
79
|
-
name:
|
79
|
+
name: redcarpet
|
80
80
|
requirement: !ruby/object:Gem::Requirement
|
81
81
|
none: false
|
82
82
|
requirements:
|
@@ -130,7 +130,7 @@ executables: []
|
|
130
130
|
extensions: []
|
131
131
|
extra_rdoc_files:
|
132
132
|
- LICENSE.txt
|
133
|
-
- README.
|
133
|
+
- README.md
|
134
134
|
files:
|
135
135
|
- .document
|
136
136
|
- .rspec
|
@@ -139,7 +139,7 @@ files:
|
|
139
139
|
- Gemfile
|
140
140
|
- Gemfile.lock
|
141
141
|
- LICENSE.txt
|
142
|
-
- README.
|
142
|
+
- README.md
|
143
143
|
- Rakefile
|
144
144
|
- VERSION
|
145
145
|
- configoro.gemspec
|
@@ -148,6 +148,7 @@ files:
|
|
148
148
|
- lib/configoro/base.rb
|
149
149
|
- lib/configoro/hash.rb
|
150
150
|
- lib/configoro/railtie.rb
|
151
|
+
- lib/configoro/simple.rb
|
151
152
|
- spec/configoro/hash_spec.rb
|
152
153
|
- spec/configoro_spec.rb
|
153
154
|
- spec/data/config/environments/common/basic.yml
|
@@ -174,7 +175,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
174
175
|
version: '0'
|
175
176
|
segments:
|
176
177
|
- 0
|
177
|
-
hash: -
|
178
|
+
hash: -1144284965917323099
|
178
179
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
179
180
|
none: false
|
180
181
|
requirements:
|