endeco 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +5 -0
- data/README.md +2 -0
- data/lib/endeco.rb +23 -8
- data/lib/endeco/config.rb +11 -2
- data/lib/endeco/version.rb +1 -1
- data/spec/lib/endeco_spec.rb +47 -27
- metadata +11 -5
data/.travis.yml
ADDED
data/README.md
CHANGED
data/lib/endeco.rb
CHANGED
@@ -10,18 +10,23 @@ module Endeco
|
|
10
10
|
end
|
11
11
|
|
12
12
|
def self.[](key, options = {})
|
13
|
-
safe_key = key.sub(
|
14
|
-
|
15
|
-
|
16
|
-
if File.exists? fullpath
|
17
|
-
Cache[safe_key] = File.read fullpath
|
13
|
+
safe_key = key.sub(/[!\?]$/, '')
|
14
|
+
value = if Cache.enable and !options[:force] and Cache.key?(safe_key)
|
15
|
+
Cache[safe_key]
|
18
16
|
else
|
19
|
-
|
20
|
-
|
17
|
+
fullpath = expand_path safe_key
|
18
|
+
if exists? safe_key
|
19
|
+
Cache[safe_key] = File.read fullpath
|
21
20
|
else
|
22
|
-
|
21
|
+
if key == safe_key
|
22
|
+
nil
|
23
|
+
else
|
24
|
+
raise Errno::ENOENT, "Errno::ENOENT: No such file or directory - #{fullpath}"
|
25
|
+
end
|
23
26
|
end
|
24
27
|
end
|
28
|
+
value.chomp! if value && !(options[:chomp] === false) && Config.default_chomp
|
29
|
+
value
|
25
30
|
end
|
26
31
|
|
27
32
|
private
|
@@ -29,6 +34,10 @@ module Endeco
|
|
29
34
|
File.expand_path File.join([Config.path, Config.env, name].compact)
|
30
35
|
end
|
31
36
|
|
37
|
+
def self.exists?(name)
|
38
|
+
File.exists? expand_path(name)
|
39
|
+
end
|
40
|
+
|
32
41
|
def self.def_methods(name)
|
33
42
|
module_eval %[
|
34
43
|
def self.#{name}(options = {})
|
@@ -41,5 +50,11 @@ module Endeco
|
|
41
50
|
self[__method__.to_s, options]
|
42
51
|
end
|
43
52
|
], __FILE__, __LINE__
|
53
|
+
|
54
|
+
module_eval %[
|
55
|
+
def self.#{name}?
|
56
|
+
exists? "#{name}"
|
57
|
+
end
|
58
|
+
], __FILE__, __LINE__
|
44
59
|
end
|
45
60
|
end
|
data/lib/endeco/config.rb
CHANGED
@@ -1,7 +1,6 @@
|
|
1
1
|
module Endeco
|
2
2
|
module Config
|
3
3
|
@@path = nil
|
4
|
-
@@env = nil
|
5
4
|
def self.path
|
6
5
|
@@path
|
7
6
|
end
|
@@ -10,6 +9,7 @@ module Endeco
|
|
10
9
|
@@path = new_path
|
11
10
|
end
|
12
11
|
|
12
|
+
@@env = nil
|
13
13
|
def self.env
|
14
14
|
@@env
|
15
15
|
end
|
@@ -17,5 +17,14 @@ module Endeco
|
|
17
17
|
def self.env=(new_env)
|
18
18
|
@@env = new_env
|
19
19
|
end
|
20
|
+
|
21
|
+
@@default_chomp = false
|
22
|
+
def self.default_chomp=(new_value)
|
23
|
+
@@default_chomp = new_value
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.default_chomp
|
27
|
+
@@default_chomp
|
28
|
+
end
|
20
29
|
end
|
21
|
-
end
|
30
|
+
end
|
data/lib/endeco/version.rb
CHANGED
data/spec/lib/endeco_spec.rb
CHANGED
@@ -12,31 +12,35 @@ describe Endeco do
|
|
12
12
|
FileUtils.rm_rf File.expand_path(File.join(Endeco::Config.path))
|
13
13
|
end
|
14
14
|
|
15
|
-
|
15
|
+
shared_context 'file prepared' do
|
16
16
|
before do
|
17
17
|
open File.join(Endeco::Config.path, Endeco::Config.env, 'test_var'), 'w' do |f|
|
18
|
-
f <<
|
18
|
+
f << "hoge\n"
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
22
|
after do
|
23
23
|
File.unlink File.join(Endeco::Config.path, Endeco::Config.env, 'test_var')
|
24
24
|
end
|
25
|
+
end
|
26
|
+
|
27
|
+
context 'file exists' do
|
28
|
+
include_context 'file prepared'
|
25
29
|
|
26
30
|
it 'can read file contents through filename method' do
|
27
|
-
Endeco.test_var.should ==
|
31
|
+
Endeco.test_var.should == "hoge\n"
|
28
32
|
end
|
29
33
|
|
30
34
|
it 'can read file contents through filename method with bang' do
|
31
|
-
Endeco.test_var!.should ==
|
35
|
+
Endeco.test_var!.should == "hoge\n"
|
32
36
|
end
|
33
37
|
|
34
38
|
it 'can read file contents through brace with filename key' do
|
35
|
-
Endeco['test_var'].should ==
|
39
|
+
Endeco['test_var'].should == "hoge\n"
|
36
40
|
end
|
37
41
|
|
38
42
|
it 'can read file contents through brace with filename key with bang' do
|
39
|
-
Endeco['test_var!'].should ==
|
43
|
+
Endeco['test_var!'].should == "hoge\n"
|
40
44
|
end
|
41
45
|
end
|
42
46
|
|
@@ -46,7 +50,7 @@ describe Endeco do
|
|
46
50
|
end
|
47
51
|
|
48
52
|
it 'raise Errno::ENOENT through filename method with bang' do
|
49
|
-
expect{ Endeco.test_var! }.
|
53
|
+
expect{ Endeco.test_var! }.to raise_error(Errno::ENOENT)
|
50
54
|
end
|
51
55
|
|
52
56
|
it 'return nil through brace with filename key' do
|
@@ -54,23 +58,15 @@ describe Endeco do
|
|
54
58
|
end
|
55
59
|
|
56
60
|
it 'raise Errno::ENOENT through brace with filename key with bang' do
|
57
|
-
expect{ Endeco['test_var!'] }.
|
61
|
+
expect{ Endeco['test_var!'] }.to raise_error(Errno::ENOENT)
|
58
62
|
end
|
59
63
|
end
|
60
64
|
|
61
65
|
context 'cache disable' do
|
62
|
-
|
63
|
-
open File.join(Endeco::Config.path, Endeco::Config.env, 'test_var'), 'w' do |f|
|
64
|
-
f << 'hoge'
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
after do
|
69
|
-
File.unlink File.join(Endeco::Config.path, Endeco::Config.env, 'test_var')
|
70
|
-
end
|
66
|
+
include_context 'file prepared'
|
71
67
|
|
72
68
|
it 'read variable is not cached' do
|
73
|
-
Endeco.test_var!.should ==
|
69
|
+
Endeco.test_var!.should == "hoge\n"
|
74
70
|
open File.join(Endeco::Config.path, Endeco::Config.env, 'test_var'), 'w' do |f|
|
75
71
|
f << 'fuga'
|
76
72
|
end
|
@@ -81,29 +77,53 @@ describe Endeco do
|
|
81
77
|
context 'cache enable' do
|
82
78
|
before do
|
83
79
|
Endeco::Cache.enable = true
|
84
|
-
open File.join(Endeco::Config.path, Endeco::Config.env, 'test_var'), 'w' do |f|
|
85
|
-
f << 'hoge'
|
86
|
-
end
|
87
80
|
end
|
88
81
|
|
89
|
-
|
90
|
-
File.unlink File.join(Endeco::Config.path, Endeco::Config.env, 'test_var')
|
91
|
-
end
|
82
|
+
include_context 'file prepared'
|
92
83
|
|
93
84
|
it 'read variable is cached' do
|
94
|
-
Endeco.test_var!.should ==
|
85
|
+
Endeco.test_var!.should == "hoge\n"
|
95
86
|
open File.join(Endeco::Config.path, Endeco::Config.env, 'test_var'), 'w' do |f|
|
96
87
|
f << 'fuga'
|
97
88
|
end
|
98
|
-
Endeco.test_var!.should ==
|
89
|
+
Endeco.test_var!.should == "hoge\n"
|
99
90
|
end
|
100
91
|
|
101
92
|
it 'force read cached variable with :force option' do
|
102
|
-
Endeco.test_var!.should ==
|
93
|
+
Endeco.test_var!.should == "hoge\n"
|
103
94
|
open File.join(Endeco::Config.path, Endeco::Config.env, 'test_var'), 'w' do |f|
|
104
95
|
f << 'fuga'
|
105
96
|
end
|
106
97
|
Endeco.test_var!(:force => true).should == 'fuga'
|
107
98
|
end
|
108
99
|
end
|
100
|
+
|
101
|
+
context 'enable default chomp' do
|
102
|
+
include_context 'file prepared'
|
103
|
+
|
104
|
+
before do
|
105
|
+
Endeco::Config.default_chomp = true
|
106
|
+
end
|
107
|
+
|
108
|
+
it 'remove line feed' do
|
109
|
+
Endeco.test_var.should eq 'hoge'
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'disable chomp with option' do
|
113
|
+
Endeco.test_var(:chomp => false).should eq "hoge\n"
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
describe '? method' do
|
118
|
+
subject { Endeco.test_var? }
|
119
|
+
|
120
|
+
context 'file exists' do
|
121
|
+
include_context 'file prepared'
|
122
|
+
it { should be_true }
|
123
|
+
end
|
124
|
+
|
125
|
+
context 'file not exists' do
|
126
|
+
it { should be_false }
|
127
|
+
end
|
128
|
+
end
|
109
129
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: endeco
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-10-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement:
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,7 +21,12 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
25
30
|
description: Environment Dependent Configuration
|
26
31
|
email:
|
27
32
|
- hawk@at-exit.com
|
@@ -31,6 +36,7 @@ extra_rdoc_files: []
|
|
31
36
|
files:
|
32
37
|
- .gitignore
|
33
38
|
- .rspec
|
39
|
+
- .travis.yml
|
34
40
|
- Gemfile
|
35
41
|
- Guardfile
|
36
42
|
- History.txt
|
@@ -64,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
64
70
|
version: '0'
|
65
71
|
requirements: []
|
66
72
|
rubyforge_project: endeco
|
67
|
-
rubygems_version: 1.8.
|
73
|
+
rubygems_version: 1.8.24
|
68
74
|
signing_key:
|
69
75
|
specification_version: 3
|
70
76
|
summary: Environment Dependent Configuration
|