configa 0.0.6 → 0.0.8

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.
@@ -1,3 +1,3 @@
1
1
  module Configa
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.8"
3
3
  end
data/lib/configa.rb CHANGED
@@ -2,6 +2,9 @@ require "configa/version"
2
2
  require "yaml"
3
3
 
4
4
  module Configa
5
+ class UnknownEnvironment < StandardError; end
6
+ class UnknownKey < StandardError; end
7
+
5
8
  extend self
6
9
 
7
10
  def new(path, opts={})
@@ -18,7 +21,7 @@ module Configa
18
21
 
19
22
  @default_env = opts[:env]
20
23
 
21
- parser
24
+ parser(@base_env)
22
25
  end
23
26
 
24
27
  def to_s
@@ -26,12 +29,12 @@ module Configa
26
29
  end
27
30
 
28
31
  def parser(env=nil)
29
- env ||= @base_env
30
- env = env.to_s
31
32
  load_yaml(env)
32
33
  load_yaml(@default_env.to_s) if @default_env
34
+
33
35
  @yaml = merge_yamls
34
36
  @yaml = @yaml[@default_env.to_s] || {} if @default_env
37
+
35
38
  @yaml = magic(@yaml)
36
39
  end
37
40
 
@@ -45,13 +48,13 @@ module Configa
45
48
  end
46
49
 
47
50
  def merge_yamls
48
- ymls = @yamls.dup
49
- base = ymls.delete(@base_env)
50
- yaml = base.dup
51
- ymls.each do |env, data|
52
- yaml[env] = base.merge data
51
+ base = @yamls[@base_env]
52
+ yaml = dup(base)
53
+ @yamls.each do |env, data|
54
+ next if env == @base_env
55
+ yaml[env.dup] = dup(base.merge data)
53
56
  end
54
- yaml = merge_yaml(yaml)
57
+ merge_yaml(yaml)
55
58
  yaml
56
59
  end
57
60
 
@@ -60,7 +63,7 @@ module Configa
60
63
  yaml.each do |k,v|
61
64
  next unless Hash === v
62
65
  cache[k] ||= {}
63
- cache[k] = cache[k].merge v if cache[k] != v
66
+ cache[k] = v if cache[k] != v
64
67
  end
65
68
  yaml.each do |k,v|
66
69
  next unless Hash === v
@@ -107,15 +110,19 @@ module Configa
107
110
  path = File.join(@base_dir, name.to_s + @base_extname)
108
111
  unless @yaml[name.to_s] || @yamls[name.to_s]
109
112
  if File.exist?(path)
110
- parser(name)
113
+ parser(name.to_s)
111
114
  end
112
115
  end
113
116
  @yaml.send(name, *args, &blk)
114
117
  rescue
115
118
  raise Configa::UnknownEnvironment, "Unknown environment '#{name}', and file '#{path}' doesn't exist also"
116
119
  end
117
- end
118
120
 
119
- class UnknownEnvironment < StandardError; end
120
- class UnknownKey < StandardError; end
121
+ private
122
+
123
+ def dup(hash)
124
+ marshal = Marshal.dump(hash)
125
+ Marshal.load(marshal)
126
+ end
127
+ end
121
128
  end
data/spec/base.yml CHANGED
@@ -8,4 +8,8 @@ tarantool:
8
8
  host: localhost
9
9
  port: 13013
10
10
 
11
- storage: "tmp"
11
+ storage: "tmp"
12
+
13
+ common:
14
+ tarantool:
15
+ space: 3
@@ -1,6 +1,8 @@
1
1
  # encoding: utf-8
2
2
  require 'spec_helper'
3
3
 
4
+ # To be refactored ;)
5
+
4
6
  describe Configa do
5
7
 
6
8
  describe Configa::MagicContainer do
@@ -74,6 +76,7 @@ describe Configa do
74
76
  @config.development.users.tarantool.space.must_equal 1
75
77
  @config.development.users.tarantool.port.must_equal 13013
76
78
  @config.development.users.tarantool.host.must_equal "212.11.3.1"
79
+ @config.development.common.tarantool.host.must_equal "212.11.3.1"
77
80
  end
78
81
  end
79
82
 
@@ -92,4 +95,39 @@ describe Configa do
92
95
  @config.production.mysql(:username, :database, hash: true).must_equal({ username: "admin", database: "mysql_prod" })
93
96
  end
94
97
  end
98
+
99
+ describe "simple_base" do
100
+ before do
101
+ path = File.expand_path("../../simple_base.yml", __FILE__)
102
+ @config = Configa.new(path)
103
+ end
104
+
105
+ it "should read base properties and raise error on non existing one" do
106
+ @config.tarantool.type.must_equal :block
107
+ @config.common.tarantool.space.must_equal 3
108
+ @config.common.tarantool.type.must_equal :block
109
+ proc{ @config.common.tarantool.host }.must_raise Configa::UnknownKey
110
+ end
111
+
112
+ it "should load simple_dev env without rewriting base properties" do
113
+ @config.simple_dev.common.tarantool.space.must_equal 3
114
+ @config.common.tarantool.type.must_equal :block
115
+ proc{ @config.common.tarantool.host }.must_raise Configa::UnknownKey
116
+ @config.simple_dev.common.tarantool.type.must_equal :em
117
+ end
118
+ end
119
+
120
+ describe Configa::MagicContainer do
121
+ before do
122
+ path = File.expand_path("../../simple_base.yml", __FILE__)
123
+ @container = Configa::MagicContainer.new(path)
124
+ end
125
+
126
+ it "should parse second file without rewriting data in base" do
127
+ yamls_was = @container.instance_variable_get(:"@yamls")
128
+ @container.parser('simple_dev')
129
+ yamls_now = @container.instance_variable_get(:"@yamls")
130
+ yamls_was["simple_base"].must_equal yamls_now["simple_base"]
131
+ end
132
+ end
95
133
  end
@@ -0,0 +1,6 @@
1
+ tarantool:
2
+ type: :block
3
+
4
+ common:
5
+ tarantool:
6
+ space: 3
@@ -0,0 +1,3 @@
1
+ tarantool:
2
+ type: :em
3
+ host: localhost
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: configa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -31,6 +31,8 @@ files:
31
31
  - spec/configa/configa_spec.rb
32
32
  - spec/development.yml
33
33
  - spec/production.yml
34
+ - spec/simple_base.yml
35
+ - spec/simple_dev.yml
34
36
  - spec/spec_helper.rb
35
37
  homepage: ''
36
38
  licenses: []
@@ -62,4 +64,6 @@ test_files:
62
64
  - spec/configa/configa_spec.rb
63
65
  - spec/development.yml
64
66
  - spec/production.yml
67
+ - spec/simple_base.yml
68
+ - spec/simple_dev.yml
65
69
  - spec/spec_helper.rb