configue 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2c8e92bfb0e0054f24a36d936fe8d6ff0aded7f0
4
- data.tar.gz: f1dd22096a2c57bd9c4e6d2989a0462bf2cbdcc9
3
+ metadata.gz: 7b5c480c38747c62db01402d5a22485adb0e5ce3
4
+ data.tar.gz: a5f756b18f7ec40aa762969b51c3f8e05784db38
5
5
  SHA512:
6
- metadata.gz: 9124dc2d54f63952dd029a593eac75a79852af3beb2bbc8481eee04f20433dab65e359a25351cc0c087a969a00f7e07cb0d5f31657feb5a28342362606760a52
7
- data.tar.gz: 692fbbbded7516817eae301166073c028b6cd706aebe488e3ba7c86d7009c08b5e18cbf4cdcdfb1b13dd12db734750c3ee3ac484fd7b807f0d40be3b99ced95c
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
- config:
24
- accounts:
25
- admin_users:
26
- - grumpy
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
- config:
35
- accounts:
36
- test_users:
37
- - sleepy
38
- - dopey
33
+ accounts:
34
+ test_users:
35
+ - sleepy
36
+ - dopey
39
37
  ```
40
38
 
41
39
  ### Access your settings
42
40
  ```
43
- >> MyConf.config.accounts.admin_users
41
+ >> MyConf.accounts.admin_users
44
42
  => ["grumpy", "sneezy"]
45
43
 
46
- >> MyConf.config.accounts.test_users
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.4"
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
@@ -100,7 +100,7 @@ module Configue
100
100
  :zip,
101
101
  ].each do |m|
102
102
  define_method(m, ->(*args, &block) {
103
- @hash.__send__(m, *args, &block)
103
+ @container.__send__(m, *args, &block)
104
104
  })
105
105
  end
106
106
  end
@@ -2,24 +2,24 @@
2
2
 
3
3
  module Configue
4
4
  class Criteria
5
- def initialize(hash, *path)
6
- @hash = hash
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(@hash, *@path, key.to_s)
11
+ self.class.new(@container, *@path, key.to_s)
12
12
  end
13
13
 
14
14
  def retrieve
15
- @path.each.inject(@hash) do |h, key|
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(@hash) do |h, key|
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
@@ -4,21 +4,21 @@ module Configue
4
4
  module HashNode
5
5
  def [](key)
6
6
  k = key.to_s
7
- v = @hash[k]
8
- @hash[k] = self.class.new(v) if node?(v)
9
- @hash[k]
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 = @hash[key]
15
- @hash[k] = self.class.new(v) if node?(v)
16
- @hash.fetch(k)
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
- @hash.key?(k)
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
- @hash.assoc(k)
29
+ @container.assoc(k)
30
30
  end
31
31
 
32
32
  def to_hash
33
- @hash.dup
33
+ @container.dup
34
34
  end
35
35
 
36
36
  def values_at(*keys)
37
37
  ks = keys.map {|k| k.to_s }
38
- @hash.values_at(*ks)
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
- @hash.__send__(m, *args, &block)
64
+ @container.__send__(m, *args, &block)
65
65
  })
66
66
  end
67
67
  end
@@ -20,8 +20,7 @@ module Configue
20
20
  #
21
21
  def import_config(args)
22
22
  raise ArgumentError unless args.respond_to?(:[])
23
- var = args.delete(:as)
24
- raise if var.nil?
23
+ var = args.delete(:as) || 'config'
25
24
 
26
25
  dirs = args.delete(:from_dir)
27
26
  files = args.delete(:from_file)
@@ -11,11 +11,15 @@ module Configue
11
11
  }
12
12
  private_constant :MERGER
13
13
 
14
- def merge(hash1, hash2)
15
- return hash2 unless hash1
16
- return hash1 unless hash2
14
+ def merge(container1, container2)
15
+ return container2 unless container1
16
+ return container1 unless container2
17
17
 
18
- hash1.merge(hash2, &MERGER)
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(hash)
8
- raise TypeError unless hash.respond_to?(:[])
7
+ def initialize(object)
8
+ raise TypeError unless object.respond_to?(:[])
9
9
 
10
- node_type = hash.instance_variable_get(:@node_type) if hash.is_a? self.class
11
- if hash.is_a? Hash or node_type == :hash
12
- setup_as_hash_node(hash)
13
- elsif hash.is_a? Array or node_type == :array
14
- setup_as_array_node(hash)
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
- @hash = hash.each.inject({}) do |h, (k, v)|
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
- @hash = array
40
- @hash = array.map {|x| self.class.new(x) } if array.is_a? Array
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
- @hash
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
- @hash = Merger.merge(@hash, source)
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
- @hash = Merger.merge(@hash, @basespace => hash[@basespace])
39
+ @container = Merger.merge(@container, @basespace => hash[@basespace])
40
40
  end
41
- @hash = Merger.merge(@hash, @namespace => hash[@namespace])
41
+ @container = Merger.merge(@container, @namespace => hash[@namespace])
42
42
  end
43
43
  end
44
44
  end
@@ -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
- YAML.load_file(path)
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
- require File.expand_path("../samples/array_records_conf", __FILE__)
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(ArrayRecordsConf.first).to have_key("name")
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(ArrayRecordsConf.first).to have_key("email")
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(ArrayRecordsConf.first).to have_key(:name)
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(ArrayRecordsConf.first).to have_key(:email)
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(ArrayRecordsConf.first).not_to have_key("foo")
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(ArrayRecordsConf.first).not_to have_key(:foo)
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
@@ -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:
@@ -0,0 +1,11 @@
1
+ foo:
2
+ - <%= 'pee-kaa-boo'.split('-')[0] %>
3
+ - <%= 'pee-kaa-boo'.split('-')[1] %>
4
+ - <%= 'pee-kaa-boo'.split('-')[2] %>
5
+ baa:
6
+ - spam
7
+ - ham
8
+ baz:
9
+ - toto
10
+ - titi
11
+ - tata
@@ -0,0 +1,5 @@
1
+ # coding: utf-8
2
+
3
+ class SingleErbYamlConf < Configue::Container
4
+ config.source_dir "#{File.dirname(__FILE__)}/single_erb_yaml"
5
+ end
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
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-05-25 00:00:00.000000000 Z
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/array_records_conf.rb
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/array_records_conf.rb
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
@@ -1,6 +0,0 @@
1
- # coding: utf-8
2
- require "configue"
3
-
4
- class ArrayRecordsConf < Configue::Container
5
- config.source_file "#{File.dirname(__FILE__)}/array_records_conf.yml"
6
- end