configue 0.1.4 → 0.1.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +20 -12
- data/configue.gemspec +2 -2
- data/lib/configue/array_node.rb +1 -1
- data/lib/configue/criteria.rb +5 -5
- data/lib/configue/hash_node.rb +11 -11
- data/lib/configue/importer.rb +1 -2
- data/lib/configue/merger.rb +8 -4
- data/lib/configue/node.rb +13 -10
- data/lib/configue/source_loader.rb +4 -4
- data/lib/configue/yaml_loader.rb +4 -1
- data/spec/basic_spec.rb +84 -7
- data/spec/erb_spec.rb +20 -0
- data/spec/samples/{array_records_conf.yml → array_records.yml} +0 -0
- data/spec/samples/multiple_array_records/friends.yml +26 -0
- data/spec/samples/multiple_array_records/users.yml +30 -0
- data/spec/samples/single_erb_yaml/conf.yml +11 -0
- data/spec/samples/single_erb_yaml_conf.rb +5 -0
- metadata +14 -6
- data/spec/samples/array_records_conf.rb +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7b5c480c38747c62db01402d5a22485adb0e5ce3
|
4
|
+
data.tar.gz: a5f756b18f7ec40aa762969b51c3f8e05784db38
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f9a48b2d04625e6f7f9af57bf0c0f26d71e550af73d88130ec2208203da21855e66307fffb7de032ec38792ae87ef6ca6f0b753f8e06c65f2aae1779a864af4f
|
7
|
+
data.tar.gz: 41c6f83925d0ff7a78e9df3dcd5e7772f411a4de9fa8302c7f41cab05e6026e68317c74b401beed6c5ec0860dfe4cef34a94a69261aa7a2e579341102817fbc5
|
data/README.md
CHANGED
@@ -20,30 +20,28 @@ end
|
|
20
20
|
### Write your settings in YAML file
|
21
21
|
```yaml
|
22
22
|
# config/accounts/admin_users.yml
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
- sneezy
|
23
|
+
accounts:
|
24
|
+
admin_users:
|
25
|
+
- grumpy
|
26
|
+
- sneezy
|
28
27
|
```
|
29
28
|
|
30
29
|
You can make multiple settings files.
|
31
30
|
|
32
31
|
```yaml
|
33
32
|
# config/accounts/test_users.yml
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
- dopey
|
33
|
+
accounts:
|
34
|
+
test_users:
|
35
|
+
- sleepy
|
36
|
+
- dopey
|
39
37
|
```
|
40
38
|
|
41
39
|
### Access your settings
|
42
40
|
```
|
43
|
-
>> MyConf.
|
41
|
+
>> MyConf.accounts.admin_users
|
44
42
|
=> ["grumpy", "sneezy"]
|
45
43
|
|
46
|
-
>> MyConf.
|
44
|
+
>> MyConf.accounts.test_users
|
47
45
|
=> ["sleepy", "dopey"]
|
48
46
|
```
|
49
47
|
|
@@ -115,6 +113,16 @@ class MyConf < Configue::Container
|
|
115
113
|
...
|
116
114
|
```
|
117
115
|
|
116
|
+
### support ERB tag
|
117
|
+
You can use ERB tags in YAML files.
|
118
|
+
|
119
|
+
```yaml
|
120
|
+
foo:
|
121
|
+
baa:
|
122
|
+
baz:
|
123
|
+
- <%= ENV['foo'] %>
|
124
|
+
```
|
125
|
+
|
118
126
|
### import_config
|
119
127
|
You can import a configuration into an object as one of its attributes without
|
120
128
|
defining a class for that.
|
data/configue.gemspec
CHANGED
@@ -12,8 +12,8 @@ Gem::Specification.new do |s|
|
|
12
12
|
s.files = `git ls-files`.split("\n")
|
13
13
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
14
14
|
s.require_paths = ["lib"]
|
15
|
-
s.version = "0.1.
|
16
|
-
s.license = "MIT"
|
15
|
+
s.version = "0.1.5"
|
16
|
+
s.license = "MIT"
|
17
17
|
|
18
18
|
s.add_development_dependency 'rspec'
|
19
19
|
end
|
data/lib/configue/array_node.rb
CHANGED
data/lib/configue/criteria.rb
CHANGED
@@ -2,24 +2,24 @@
|
|
2
2
|
|
3
3
|
module Configue
|
4
4
|
class Criteria
|
5
|
-
def initialize(
|
6
|
-
@
|
5
|
+
def initialize(container, *path)
|
6
|
+
@container = container
|
7
7
|
@path = path
|
8
8
|
end
|
9
9
|
|
10
10
|
def [](key)
|
11
|
-
self.class.new(@
|
11
|
+
self.class.new(@container, *@path, key.to_s)
|
12
12
|
end
|
13
13
|
|
14
14
|
def retrieve
|
15
|
-
@path.each.inject(@
|
15
|
+
@path.each.inject(@container) do |h, key|
|
16
16
|
return nil unless h.respond_to?(:has_key?) and h.has_key?(key)
|
17
17
|
h[key]
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
21
|
def exist?
|
22
|
-
@path.each.inject(@
|
22
|
+
@path.each.inject(@container) do |h, key|
|
23
23
|
return false unless h.respond_to?(:has_key?) and h.has_key?(key)
|
24
24
|
h[key]
|
25
25
|
end
|
data/lib/configue/hash_node.rb
CHANGED
@@ -4,21 +4,21 @@ module Configue
|
|
4
4
|
module HashNode
|
5
5
|
def [](key)
|
6
6
|
k = key.to_s
|
7
|
-
v = @
|
8
|
-
@
|
9
|
-
@
|
7
|
+
v = @container[k]
|
8
|
+
@container[k] = self.class.new(v) if node?(v)
|
9
|
+
@container[k]
|
10
10
|
end
|
11
11
|
|
12
12
|
def fetch(key)
|
13
13
|
k = key.to_s
|
14
|
-
v = @
|
15
|
-
@
|
16
|
-
@
|
14
|
+
v = @container[key]
|
15
|
+
@container[k] = self.class.new(v) if node?(v)
|
16
|
+
@container.fetch(k)
|
17
17
|
end
|
18
18
|
|
19
19
|
def key?(key)
|
20
20
|
k = key.to_s
|
21
|
-
@
|
21
|
+
@container.key?(k)
|
22
22
|
end
|
23
23
|
alias_method :has_key?, :key?
|
24
24
|
alias_method :include?, :key?
|
@@ -26,16 +26,16 @@ module Configue
|
|
26
26
|
|
27
27
|
def assoc(key)
|
28
28
|
k = key.to_s
|
29
|
-
@
|
29
|
+
@container.assoc(k)
|
30
30
|
end
|
31
31
|
|
32
32
|
def to_hash
|
33
|
-
@
|
33
|
+
@container.dup
|
34
34
|
end
|
35
35
|
|
36
36
|
def values_at(*keys)
|
37
37
|
ks = keys.map {|k| k.to_s }
|
38
|
-
@
|
38
|
+
@container.values_at(*ks)
|
39
39
|
end
|
40
40
|
|
41
41
|
[ :all?,
|
@@ -61,7 +61,7 @@ module Configue
|
|
61
61
|
:values,
|
62
62
|
].each do |m|
|
63
63
|
define_method(m, ->(*args, &block) {
|
64
|
-
@
|
64
|
+
@container.__send__(m, *args, &block)
|
65
65
|
})
|
66
66
|
end
|
67
67
|
end
|
data/lib/configue/importer.rb
CHANGED
data/lib/configue/merger.rb
CHANGED
@@ -11,11 +11,15 @@ module Configue
|
|
11
11
|
}
|
12
12
|
private_constant :MERGER
|
13
13
|
|
14
|
-
def merge(
|
15
|
-
return
|
16
|
-
return
|
14
|
+
def merge(container1, container2)
|
15
|
+
return container2 unless container1
|
16
|
+
return container1 unless container2
|
17
17
|
|
18
|
-
|
18
|
+
if container1.is_a?(Array) and container2.is_a?(Array)
|
19
|
+
container1.concat(container2)
|
20
|
+
else
|
21
|
+
container1.merge(container2, &MERGER)
|
22
|
+
end
|
19
23
|
end
|
20
24
|
|
21
25
|
module_function :merge
|
data/lib/configue/node.rb
CHANGED
@@ -4,14 +4,17 @@ require 'configue/array_node'
|
|
4
4
|
|
5
5
|
module Configue
|
6
6
|
class Node
|
7
|
-
def initialize(
|
8
|
-
raise TypeError unless
|
7
|
+
def initialize(object)
|
8
|
+
raise TypeError unless object.respond_to?(:[])
|
9
9
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
10
|
+
if object.is_a? self.class
|
11
|
+
node_type = object.instance_variable_get(:@node_type)
|
12
|
+
end
|
13
|
+
|
14
|
+
if object.is_a? Hash or node_type == :hash
|
15
|
+
setup_as_hash_node(object)
|
16
|
+
elsif object.is_a? Array or node_type == :array
|
17
|
+
setup_as_array_node(object)
|
15
18
|
end
|
16
19
|
self
|
17
20
|
end
|
@@ -26,7 +29,7 @@ module Configue
|
|
26
29
|
|
27
30
|
def setup_as_hash_node(hash)
|
28
31
|
sig = class << self; self; end
|
29
|
-
@
|
32
|
+
@container = hash.each.inject({}) do |h, (k, v)|
|
30
33
|
sig.__send__(:define_method, k, ->{ self[k] })
|
31
34
|
h[k.to_s] = node?(v) ? self.class.new(v) : v; h
|
32
35
|
end
|
@@ -36,8 +39,8 @@ module Configue
|
|
36
39
|
|
37
40
|
def setup_as_array_node(array)
|
38
41
|
sig = class << self; self; end
|
39
|
-
@
|
40
|
-
@
|
42
|
+
@container = array
|
43
|
+
@container = array.map {|x| self.class.new(x) } if array.is_a? Array
|
41
44
|
sig.__send__(:include, ArrayNode)
|
42
45
|
@node_type = :array
|
43
46
|
end
|
@@ -15,7 +15,7 @@ module Configue
|
|
15
15
|
@sources.each do |src|
|
16
16
|
src.each {|k, v| __send__("load_#{k}", v) }
|
17
17
|
end
|
18
|
-
@
|
18
|
+
@container
|
19
19
|
end
|
20
20
|
|
21
21
|
protected
|
@@ -30,15 +30,15 @@ module Configue
|
|
30
30
|
if @namespace and source[@namespace]
|
31
31
|
namespaced_hash(source)
|
32
32
|
else
|
33
|
-
@
|
33
|
+
@container = Merger.merge(@container, source)
|
34
34
|
end
|
35
35
|
end
|
36
36
|
|
37
37
|
def namespaced_hash(hash)
|
38
38
|
if @basespace and hash.key?(@basespace)
|
39
|
-
@
|
39
|
+
@container = Merger.merge(@container, @basespace => hash[@basespace])
|
40
40
|
end
|
41
|
-
@
|
41
|
+
@container = Merger.merge(@container, @namespace => hash[@namespace])
|
42
42
|
end
|
43
43
|
end
|
44
44
|
end
|
data/lib/configue/yaml_loader.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
# coding: utf-8
|
2
2
|
require "yaml"
|
3
|
+
require "erb"
|
3
4
|
|
4
5
|
module Configue
|
5
6
|
class YamlLoader
|
@@ -8,7 +9,9 @@ module Configue
|
|
8
9
|
end
|
9
10
|
|
10
11
|
def load(path)
|
11
|
-
|
12
|
+
buf = open(path).read
|
13
|
+
return {} if buf.empty?
|
14
|
+
YAML.load(ERB.new(buf).result)
|
12
15
|
end
|
13
16
|
end
|
14
17
|
end
|
data/spec/basic_spec.rb
CHANGED
@@ -64,43 +64,120 @@ describe "Configue::Container" do
|
|
64
64
|
end
|
65
65
|
|
66
66
|
context "when loading a yaml file with an array of records" do
|
67
|
-
|
67
|
+
let(:config) do
|
68
|
+
source_file = File.expand_path("../samples/array_records.yml", __FILE__)
|
69
|
+
Class.new(Configue::Container) {
|
70
|
+
config.source_file source_file
|
71
|
+
}
|
72
|
+
end
|
68
73
|
|
69
74
|
describe "first record" do
|
70
75
|
describe ".has_key?" do
|
71
76
|
context 'when parameter is "name"' do
|
72
77
|
it "returns true" do
|
73
|
-
expect(
|
78
|
+
expect(config.first).to have_key("name")
|
74
79
|
end
|
75
80
|
end
|
76
81
|
|
77
82
|
context 'when parameter is "email"' do
|
78
83
|
it "returns true" do
|
79
|
-
expect(
|
84
|
+
expect(config.first).to have_key("email")
|
80
85
|
end
|
81
86
|
end
|
82
87
|
|
83
88
|
context "when parameter is :name" do
|
84
89
|
it "returns true" do
|
85
|
-
expect(
|
90
|
+
expect(config.first).to have_key(:name)
|
86
91
|
end
|
87
92
|
end
|
88
93
|
|
89
94
|
context "when parameter is :email" do
|
90
95
|
it "returns true" do
|
91
|
-
expect(
|
96
|
+
expect(config.first).to have_key(:email)
|
92
97
|
end
|
93
98
|
end
|
94
99
|
|
95
100
|
context 'when parameter is "foo"' do
|
96
101
|
it "returns false" do
|
97
|
-
expect(
|
102
|
+
expect(config.first).not_to have_key("foo")
|
98
103
|
end
|
99
104
|
end
|
100
105
|
|
101
106
|
context "when parameter is :foo" do
|
102
107
|
it "returns false" do
|
103
|
-
expect(
|
108
|
+
expect(config.first).not_to have_key(:foo)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
context "when loading the sample multiple yaml files with an array of records" do
|
116
|
+
let(:config) do
|
117
|
+
source_dir = File.expand_path("../samples/multiple_array_records", __FILE__)
|
118
|
+
Class.new(Configue::Container) {
|
119
|
+
config.source_dir source_dir
|
120
|
+
}
|
121
|
+
end
|
122
|
+
|
123
|
+
describe ".size" do
|
124
|
+
it "returns 2" do
|
125
|
+
expect(config.size).to eq 2
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
describe ".first" do
|
130
|
+
it "can respond to a `keys' message" do
|
131
|
+
expect(config.first).to respond_to(:keys)
|
132
|
+
end
|
133
|
+
|
134
|
+
describe ".keys" do
|
135
|
+
it "returns 'name' and 'fields'" do
|
136
|
+
expect(config.first.keys).to match_array(["name", "fields"])
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
describe ".name" do
|
141
|
+
it "returns String value" do
|
142
|
+
expect(config.first.name).to be_instance_of(String)
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
describe ".fields" do
|
147
|
+
describe ".first" do
|
148
|
+
describe ".keys" do
|
149
|
+
it "returns `name', `type', `default', `null' and `option'" do
|
150
|
+
target = config.first.fields.first.keys
|
151
|
+
expect(target).to match_array(%w[name type default null option])
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
describe ".select" do
|
159
|
+
context "when select records of which `name' is `friends'" do
|
160
|
+
let(:friends_record) do
|
161
|
+
config.select {|item| item.name == 'friends' }
|
162
|
+
end
|
163
|
+
|
164
|
+
it "returns an array that has only one record" do
|
165
|
+
expect(friends_record.size).to eq 1
|
166
|
+
end
|
167
|
+
|
168
|
+
describe ".first" do
|
169
|
+
it "returns a hash node that has `name' and `fields' key" do
|
170
|
+
expect(friends_record.first.keys).to match_array(%w[name fields])
|
171
|
+
end
|
172
|
+
|
173
|
+
describe ".fields" do
|
174
|
+
let(:friends_fields) do
|
175
|
+
friends_record.first.fields
|
176
|
+
end
|
177
|
+
|
178
|
+
it "return an array that has 4 records" do
|
179
|
+
expect(friends_fields.size).to eq 4
|
180
|
+
end
|
104
181
|
end
|
105
182
|
end
|
106
183
|
end
|
data/spec/erb_spec.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require File.expand_path("../spec_helper", __FILE__)
|
3
|
+
|
4
|
+
describe "Configue::Container" do
|
5
|
+
context "when ERB tags are in the setting file" do
|
6
|
+
require File.expand_path("../samples/single_erb_yaml_conf", __FILE__)
|
7
|
+
|
8
|
+
context "1st level" do
|
9
|
+
it "has keys `foo', `baa', `baz'" do
|
10
|
+
expect(SingleErbYamlConf.keys).to eq ["foo", "baa", "baz"]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context "2nd level" do
|
15
|
+
it "has an array `pee', `kaa', `boo' as `foo'" do
|
16
|
+
expect(SingleErbYamlConf.foo).to eq ["pee", "kaa", "boo"]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
File without changes
|
@@ -0,0 +1,26 @@
|
|
1
|
+
- name: friends
|
2
|
+
fields:
|
3
|
+
- name: id
|
4
|
+
type: Bignum
|
5
|
+
default: null
|
6
|
+
"null": false
|
7
|
+
option:
|
8
|
+
- primary key
|
9
|
+
- auto increment
|
10
|
+
- name: subject_friend_id
|
11
|
+
type: Bignum
|
12
|
+
default: null
|
13
|
+
"null": false
|
14
|
+
option:
|
15
|
+
- foreign key: users
|
16
|
+
- name: object_friend_id
|
17
|
+
type: Bignum
|
18
|
+
default: null
|
19
|
+
"null": false
|
20
|
+
option:
|
21
|
+
- foreign key: users
|
22
|
+
- name: status
|
23
|
+
type: integer
|
24
|
+
default: 0
|
25
|
+
"null": false
|
26
|
+
option:
|
@@ -0,0 +1,30 @@
|
|
1
|
+
- name: users
|
2
|
+
fields:
|
3
|
+
- name: id
|
4
|
+
type: Bignum
|
5
|
+
default: null
|
6
|
+
"null": false
|
7
|
+
option:
|
8
|
+
- primary key
|
9
|
+
- auto increment
|
10
|
+
- name: name
|
11
|
+
type: string
|
12
|
+
default: null
|
13
|
+
"null": false
|
14
|
+
option:
|
15
|
+
- name: email
|
16
|
+
type: string
|
17
|
+
default: null
|
18
|
+
"null": false
|
19
|
+
option:
|
20
|
+
- unique
|
21
|
+
- name: password
|
22
|
+
type: string
|
23
|
+
default: null
|
24
|
+
"null": false
|
25
|
+
option:
|
26
|
+
- name: salt
|
27
|
+
type: string
|
28
|
+
default: null
|
29
|
+
"null": false
|
30
|
+
option:
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: configue
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- hosim
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-08-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -51,13 +51,15 @@ files:
|
|
51
51
|
- spec/basic_spec.rb
|
52
52
|
- spec/config_spec.rb
|
53
53
|
- spec/configue/merger_spec.rb
|
54
|
+
- spec/erb_spec.rb
|
54
55
|
- spec/namespace_spec.rb
|
55
|
-
- spec/samples/
|
56
|
-
- spec/samples/array_records_conf.yml
|
56
|
+
- spec/samples/array_records.yml
|
57
57
|
- spec/samples/base_namespace_conf.rb
|
58
58
|
- spec/samples/config/admin.yml
|
59
59
|
- spec/samples/config/test.yml
|
60
60
|
- spec/samples/config_conf.rb
|
61
|
+
- spec/samples/multiple_array_records/friends.yml
|
62
|
+
- spec/samples/multiple_array_records/users.yml
|
61
63
|
- spec/samples/multiple_yaml/foo/baa.yml
|
62
64
|
- spec/samples/multiple_yaml/foo/bar/ding.yml
|
63
65
|
- spec/samples/multiple_yaml/foo/bar/tick.yml
|
@@ -69,6 +71,8 @@ files:
|
|
69
71
|
- spec/samples/namespace_conf.rb
|
70
72
|
- spec/samples/signs/config.yml
|
71
73
|
- spec/samples/signs_conf.rb
|
74
|
+
- spec/samples/single_erb_yaml/conf.yml
|
75
|
+
- spec/samples/single_erb_yaml_conf.rb
|
72
76
|
- spec/samples/single_source_conf.rb
|
73
77
|
- spec/samples/single_yaml/conf.yml
|
74
78
|
- spec/samples/single_yaml_conf.rb
|
@@ -107,13 +111,15 @@ test_files:
|
|
107
111
|
- spec/basic_spec.rb
|
108
112
|
- spec/config_spec.rb
|
109
113
|
- spec/configue/merger_spec.rb
|
114
|
+
- spec/erb_spec.rb
|
110
115
|
- spec/namespace_spec.rb
|
111
|
-
- spec/samples/
|
112
|
-
- spec/samples/array_records_conf.yml
|
116
|
+
- spec/samples/array_records.yml
|
113
117
|
- spec/samples/base_namespace_conf.rb
|
114
118
|
- spec/samples/config/admin.yml
|
115
119
|
- spec/samples/config/test.yml
|
116
120
|
- spec/samples/config_conf.rb
|
121
|
+
- spec/samples/multiple_array_records/friends.yml
|
122
|
+
- spec/samples/multiple_array_records/users.yml
|
117
123
|
- spec/samples/multiple_yaml/foo/baa.yml
|
118
124
|
- spec/samples/multiple_yaml/foo/bar/ding.yml
|
119
125
|
- spec/samples/multiple_yaml/foo/bar/tick.yml
|
@@ -125,6 +131,8 @@ test_files:
|
|
125
131
|
- spec/samples/namespace_conf.rb
|
126
132
|
- spec/samples/signs/config.yml
|
127
133
|
- spec/samples/signs_conf.rb
|
134
|
+
- spec/samples/single_erb_yaml/conf.yml
|
135
|
+
- spec/samples/single_erb_yaml_conf.rb
|
128
136
|
- spec/samples/single_source_conf.rb
|
129
137
|
- spec/samples/single_yaml/conf.yml
|
130
138
|
- spec/samples/single_yaml_conf.rb
|