sanultari-config 0.2.0 → 0.2.1

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.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- ZenTest (4.8.1)
4
+ ZenTest (4.8.2)
5
5
  autotest-growl (0.2.16)
6
6
  diff-lcs (1.1.3)
7
7
  git (1.2.5)
@@ -10,7 +10,7 @@ GEM
10
10
  git (>= 1.2.5)
11
11
  rake
12
12
  rdoc
13
- json (1.7.3)
13
+ json (1.7.5)
14
14
  rake (0.9.2.2)
15
15
  rcov (1.0.0)
16
16
  rdoc (3.12)
@@ -20,10 +20,10 @@ GEM
20
20
  rspec-core (~> 2.11.0)
21
21
  rspec-expectations (~> 2.11.0)
22
22
  rspec-mocks (~> 2.11.0)
23
- rspec-core (2.11.0)
24
- rspec-expectations (2.11.1)
23
+ rspec-core (2.11.1)
24
+ rspec-expectations (2.11.3)
25
25
  diff-lcs (~> 1.1.3)
26
- rspec-mocks (2.11.1)
26
+ rspec-mocks (2.11.3)
27
27
  yard (0.8.2.1)
28
28
 
29
29
  PLATFORMS
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.1
data/fixture/tree.yml CHANGED
@@ -1,4 +1,6 @@
1
+ ---
1
2
  a: test
2
3
  test:
3
4
  a: confirm
4
5
  b: cancel
6
+ to_ary: {}
@@ -21,14 +21,24 @@ module SanUltari
21
21
  # Config 객체 초기화
22
22
  #
23
23
  # @param [String] path 읽어 들일 설정 파일의 위치. 넘기지 않으면 아무것도 설정하지 않는다.
24
- def init! path = nil
24
+ def init! path = nil, default = nil
25
+ @default = default unless default == nil
26
+
25
27
  return nil if path == nil
26
28
  if @name == nil
27
29
  @name = File.basename path, '.yml'
28
30
  @path = File.expand_path File.dirname(path), @path
29
31
  end
30
- config_hash = YAML.load_file make_path
31
- from_hash(config_hash)
32
+
33
+ load_defaults if default_available?
34
+
35
+ if File.exist? path
36
+ abs_path = make_path
37
+ config_hash = YAML.load_file(abs_path)
38
+ from_hash(config_hash)
39
+ end
40
+ ensure
41
+ self.save path
32
42
  end
33
43
 
34
44
  # Config 객체를 지정된 위치에 YAML 포맷으로 덤프한다.
@@ -40,7 +50,7 @@ module SanUltari
40
50
  def save path = nil
41
51
  @name = 'config' if @name == nil
42
52
  path = make_path if path == nil
43
-
53
+
44
54
  File.open(make_path(path), 'w') do |f|
45
55
  YAML.dump(to_hash, f)
46
56
  end
@@ -57,9 +67,9 @@ module SanUltari
57
67
  t_value = value
58
68
  if value.instance_of? Hash
59
69
  t_value = Config.new key
60
- t_value.from_hash value
70
+ t_value.from_hash value
61
71
  end
62
-
72
+
63
73
  @store.send("#{key}=".to_sym, t_value)
64
74
  end
65
75
  end
@@ -94,5 +104,19 @@ module SanUltari
94
104
  def method_missing(method_name, *args, &block)
95
105
  @store.public_send method_name, *args, &block
96
106
  end
107
+
108
+ def load_defaults
109
+ return unless default_available?
110
+ default_hash = @default.to_hash
111
+ default_hash.keys.each do |key|
112
+ self.public_send "#{key}=".to_sym, default_hash[key]
113
+ end
114
+ end
115
+
116
+ def default_available?
117
+ @default != nil
118
+ end
119
+
120
+ private :load_defaults, :default_available?
97
121
  end
98
122
  end
data/spec/config_spec.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
  require 'tempfile'
3
+ require 'fileutils'
3
4
 
4
5
  describe "SanUltari::Config" do
5
6
  before :all do
@@ -7,6 +8,11 @@ describe "SanUltari::Config" do
7
8
  Dir.chdir File.expand_path('../fixture', File.dirname(__FILE__))
8
9
  end
9
10
 
11
+ before :each do
12
+ @backup = File.expand_path 'fixture.bak', File.dirname(Dir.getwd)
13
+ FileUtils.cp_r Dir.getwd, @backup
14
+ end
15
+
10
16
  before :each do
11
17
  @fixture = SanUltari::Config.new
12
18
  end
@@ -20,7 +26,7 @@ describe "SanUltari::Config" do
20
26
 
21
27
  it "should create object without yaml file" do
22
28
  @fixture.init!
23
- @fixture.name.should be_nil
29
+ @fixture.name.should eql 'config'
24
30
  @fixture.path.should eql File.expand_path('.')
25
31
  end
26
32
 
@@ -61,6 +67,22 @@ describe "SanUltari::Config" do
61
67
  tmp_file.unlink
62
68
  end
63
69
 
70
+ it "should load default" do
71
+ config = SanUltari::Config.new
72
+ config.test = 'a'
73
+ config.test2 = 'b'
74
+ config.a.alpha = 'omega'
75
+ @fixture.init! '/tmp/default.yml', config
76
+ end
77
+
78
+ after :each do
79
+ temp = Dir.getwd
80
+ Dir.chdir '..'
81
+ FileUtils.cp_r Dir["#{@backup}/*"], temp, remove_destination: true
82
+ FileUtils.rmtree @backup
83
+ Dir.chdir temp
84
+ end
85
+
64
86
  after :all do
65
87
  Dir.chdir @origin
66
88
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sanultari-config
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
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: 2012-08-01 00:00:00.000000000 Z
12
+ date: 2012-10-11 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: ZenTest
@@ -199,7 +199,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
199
199
  version: '0'
200
200
  segments:
201
201
  - 0
202
- hash: 1017306160802311613
202
+ hash: 187515124130919482
203
203
  required_rubygems_version: !ruby/object:Gem::Requirement
204
204
  none: false
205
205
  requirements: