user_config 0.0.3 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +35 -4
- data/lib/user_config.rb +56 -21
- data/lib/user_config/version.rb +1 -1
- data/spec/user_config_spec.rb +15 -0
- metadata +8 -8
- data/VERSION +0 -1
data/README.md
CHANGED
@@ -4,18 +4,38 @@ user_config is a library to manage configuration files in user's home directory
|
|
4
4
|
for ruby libraries or applications.
|
5
5
|
The format of a configuration file is yaml.
|
6
6
|
|
7
|
+
## Behavior
|
8
|
+
|
9
|
+
The class UserConfig manages values of configuration,
|
10
|
+
which is handled as a set of hashes and is saved to yaml files
|
11
|
+
in specified directory under user's home directory.
|
12
|
+
|
7
13
|
## Examples
|
8
14
|
|
9
|
-
###
|
15
|
+
### Simple usage
|
16
|
+
|
17
|
+
uconf = UserConfig.new(".some_config")
|
18
|
+
|
19
|
+
The directory ~/.some\_config is created in the directory ENV['HOME'].
|
20
|
+
To set the value "world" for the key "hello" of ~/.some\_config/file.yaml,
|
21
|
+
|
22
|
+
uconf["file.yaml"]["hello"] = "world"
|
23
|
+
|
24
|
+
The value is not saved to the file. To do that,
|
25
|
+
|
26
|
+
uconf["file.yaml"].save
|
10
27
|
|
11
|
-
|
12
|
-
|
28
|
+
### Create initial configuration files
|
29
|
+
|
30
|
+
If we want to create initial files with default values,
|
31
|
+
we use the method UserConfig#default.
|
13
32
|
|
14
33
|
UserConfig.default('conf.yaml', { 'key1' => 'val1', 'key2' => 'val2' })
|
15
34
|
uconf = UserConfig.new('.some_config')
|
16
35
|
uconf.create('conf.yaml')
|
17
36
|
|
18
|
-
|
37
|
+
The file ~/.some_config'/confi.yaml is created.
|
38
|
+
To create another file 'conf2.yaml',
|
19
39
|
|
20
40
|
UserConfig.default('conf2.yaml', { 'hello' => 'world'})
|
21
41
|
uconf.create('conf2.yaml')
|
@@ -45,6 +65,17 @@ If we modify some files, we can save all files by UserConfig#save_all.
|
|
45
65
|
yaml2['new_key'] = 'ABCDE'
|
46
66
|
uconf.save_all
|
47
67
|
|
68
|
+
### Behavior like a hash
|
69
|
+
|
70
|
+
In internal of UserConfig::YAMLFile, the methods of Hash is called except for some methods.
|
71
|
+
|
72
|
+
uconf = UserConfig.new('.some_config')
|
73
|
+
yaml = uconf['conf.yaml']
|
74
|
+
yaml.each do |key, val|
|
75
|
+
...
|
76
|
+
end
|
77
|
+
yaml.empty?
|
78
|
+
|
48
79
|
## Contributing to user_config
|
49
80
|
|
50
81
|
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
data/lib/user_config.rb
CHANGED
@@ -7,6 +7,8 @@ class UserConfig
|
|
7
7
|
class DirectoryExistenceError < StandardError
|
8
8
|
end
|
9
9
|
|
10
|
+
# Get default value of current class.
|
11
|
+
# The value is a hash, of which keys are paths of yaml files.
|
10
12
|
def self.default_value
|
11
13
|
unless @default_value
|
12
14
|
@default_value = {}
|
@@ -14,18 +16,21 @@ class UserConfig
|
|
14
16
|
@default_value
|
15
17
|
end
|
16
18
|
|
19
|
+
# Set default values of the specified file of current class.
|
20
|
+
# @param [String] path Relative path of the specified yaml file
|
21
|
+
# @param [Hash] default_hash Default values
|
17
22
|
def self.default(path, default_hash)
|
18
23
|
self.default_value[path] = default_hash
|
19
24
|
end
|
20
25
|
|
21
26
|
attr_reader :directory
|
22
27
|
|
23
|
-
#
|
24
|
-
# opts[:home
|
25
|
-
# opts[:permission
|
26
|
-
#
|
27
|
-
#
|
28
|
-
# then an error raises.
|
28
|
+
# @param [String] directory_name :name of a configuration directory.
|
29
|
+
# @option opts [String] :home Root directory, of which default value is ENV['HOME'].
|
30
|
+
# @option opts [Integer] :permission A file permission of the configuration directory,
|
31
|
+
# of which default value is 0700.
|
32
|
+
# @option opts [boolean] :new_directory
|
33
|
+
# If opts[:new_directory] is true and the directory exists then an error raises.
|
29
34
|
def initialize(directory_name, opts = {})
|
30
35
|
@directory = File.expand_path(File.join(opts[:home] || ENV['HOME'], directory_name))
|
31
36
|
@file = {}
|
@@ -39,6 +44,9 @@ class UserConfig
|
|
39
44
|
end
|
40
45
|
end
|
41
46
|
|
47
|
+
# @param [String] path Relative path of a specified file
|
48
|
+
# @param [boolean] create_directory If the value is true then we create parent directories recursively.
|
49
|
+
# @return [String] An absolute path of a specified file
|
42
50
|
def file_path(path, create_directory = nil)
|
43
51
|
if Pathname(path).absolute?
|
44
52
|
raise ArgumentError, "Path '#{path}' is absolute."
|
@@ -60,14 +68,18 @@ class UserConfig
|
|
60
68
|
end
|
61
69
|
private :load_file
|
62
70
|
|
63
|
-
# Save the configuration file
|
64
|
-
#
|
71
|
+
# Save the configuration file under the directory.
|
72
|
+
# @param [String] path Relative path of target file
|
73
|
+
# @param [Hash] value Values to be saved to the file
|
65
74
|
def create(path, value = nil)
|
66
75
|
yaml_file = load_file(path, true, value)
|
67
76
|
yaml_file.save
|
68
77
|
end
|
69
78
|
|
70
79
|
# Make directory under the configuration directory.
|
80
|
+
# @param [String] path Relative path of a directory
|
81
|
+
# @param [Hash] opts Options
|
82
|
+
# @option opts [Integer] :mode Permission for a directory to be created
|
71
83
|
def make_directory(path, opts = {})
|
72
84
|
fpath = file_path(path)
|
73
85
|
unless File.exist?(fpath)
|
@@ -79,7 +91,9 @@ class UserConfig
|
|
79
91
|
fpath
|
80
92
|
end
|
81
93
|
|
82
|
-
# Load the configuration file
|
94
|
+
# Load the configuration file.
|
95
|
+
# @param [String] path Relative path of a file to be loaded
|
96
|
+
# @return [hash]
|
83
97
|
def load(path)
|
84
98
|
@file[path] || load_file(path)
|
85
99
|
end
|
@@ -116,8 +130,10 @@ class UserConfig
|
|
116
130
|
end
|
117
131
|
end
|
118
132
|
|
119
|
-
# List files in directory
|
120
|
-
#
|
133
|
+
# List files in the specified directory.
|
134
|
+
# @param [String] dir A directory in which you want to list files.
|
135
|
+
# @param [Hash] opts Options
|
136
|
+
# @option opts [boolean] :absolute If the value is true then we get an array of absolute paths.
|
121
137
|
def list_in_directory(dir, opts = {})
|
122
138
|
fpath = file_path(dir)
|
123
139
|
if File.directory?(fpath)
|
@@ -135,7 +151,9 @@ class UserConfig
|
|
135
151
|
end
|
136
152
|
end
|
137
153
|
|
138
|
-
# Open file
|
154
|
+
# Open a file.
|
155
|
+
# @param [String] path A path of the file
|
156
|
+
# @param [String] mode A mode
|
139
157
|
def open(path, mode, &block)
|
140
158
|
full_path = file_path(path, true)
|
141
159
|
f = Kernel.open(full_path, mode)
|
@@ -151,7 +169,9 @@ class UserConfig
|
|
151
169
|
end
|
152
170
|
end
|
153
171
|
|
154
|
-
# Read
|
172
|
+
# Read a file
|
173
|
+
# @param [String] path A relative path of a file
|
174
|
+
# @return [String] Strings of the file
|
155
175
|
def read(path)
|
156
176
|
fpath = file_path(path)
|
157
177
|
if File.exist?(fpath)
|
@@ -164,33 +184,40 @@ class UserConfig
|
|
164
184
|
class YAMLFile
|
165
185
|
attr_reader :path
|
166
186
|
|
167
|
-
#
|
168
|
-
#
|
169
|
-
#
|
187
|
+
# @param [String] path A path of yaml file, which saves pairs of key and value.
|
188
|
+
# @param [Hash] default A hash saving default value.
|
189
|
+
# @param [Hash] opts Options
|
190
|
+
# @option opts [booean] If the value is true then values of an instance are merged with default values.
|
170
191
|
def initialize(path, default, opts = {})
|
171
192
|
@path = path
|
172
193
|
@default = default
|
173
|
-
@cache =
|
194
|
+
@cache = load_yaml_file
|
174
195
|
if opts[:merge]
|
175
196
|
@cache.merge!(@default)
|
176
197
|
end
|
177
198
|
end
|
178
199
|
|
179
|
-
def
|
200
|
+
def load_yaml_file
|
180
201
|
if File.exist?(path)
|
181
202
|
YAML.load_file(path)
|
182
203
|
else
|
183
204
|
{}
|
184
205
|
end
|
185
206
|
end
|
186
|
-
private :
|
207
|
+
private :load_yaml_file
|
187
208
|
|
188
209
|
def to_yaml
|
189
210
|
YAML.dump(@cache)
|
190
211
|
end
|
191
212
|
|
192
|
-
|
193
|
-
|
213
|
+
# @param [boolean] merge If the value is true then we merges default values and cached values.
|
214
|
+
# @return [Hash] A hash created by merging default values and cached values.
|
215
|
+
def to_hash(merge = false)
|
216
|
+
if merge
|
217
|
+
@default.merge(@cache)
|
218
|
+
else
|
219
|
+
@cache
|
220
|
+
end
|
194
221
|
end
|
195
222
|
|
196
223
|
# Save cached values to the path of file.
|
@@ -218,5 +245,13 @@ class UserConfig
|
|
218
245
|
def set?(key)
|
219
246
|
@cache.has_key?(key)
|
220
247
|
end
|
248
|
+
|
249
|
+
def method_missing(method_name, *args, &block)
|
250
|
+
if Hash.method_defined?(method_name)
|
251
|
+
to_hash(true).__send__(method_name, *args, &block)
|
252
|
+
else
|
253
|
+
super
|
254
|
+
end
|
255
|
+
end
|
221
256
|
end
|
222
257
|
end
|
data/lib/user_config/version.rb
CHANGED
data/spec/user_config_spec.rb
CHANGED
@@ -202,6 +202,21 @@ describe UserConfig do
|
|
202
202
|
UserConfig.default_value.object_id.should_not == UserConfigCustom.default_value.object_id
|
203
203
|
end
|
204
204
|
|
205
|
+
context "when calling methods of Hash" do
|
206
|
+
before(:all) do
|
207
|
+
UserConfig.default("to_test_hash", { :a => "A", :b => "B" })
|
208
|
+
end
|
209
|
+
|
210
|
+
it "should be empty." do
|
211
|
+
subject["empty_conf"].empty?.should be_true
|
212
|
+
end
|
213
|
+
|
214
|
+
it "should return merged hash" do
|
215
|
+
subject["to_test_hash"][:b] = "BBB"
|
216
|
+
subject["to_test_hash"].to_hash(true).should == { :a => "A", :b => "BBB" }
|
217
|
+
end
|
218
|
+
end
|
219
|
+
|
205
220
|
after(:all) do
|
206
221
|
FileUtils.rm_r(@home)
|
207
222
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: user_config
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
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:
|
12
|
+
date: 2012-01-05 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
16
|
-
requirement: &
|
16
|
+
requirement: &11654660 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ! '>='
|
@@ -21,10 +21,10 @@ dependencies:
|
|
21
21
|
version: '0'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements: *
|
24
|
+
version_requirements: *11654660
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: yard
|
27
|
-
requirement: &
|
27
|
+
requirement: &11654240 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,7 +32,7 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *11654240
|
36
36
|
description: The library creates, saves, and loads configuration files, which are
|
37
37
|
in a user's home directory or a specified directory.
|
38
38
|
email:
|
@@ -48,7 +48,6 @@ files:
|
|
48
48
|
- LICENSE.txt
|
49
49
|
- README.md
|
50
50
|
- Rakefile
|
51
|
-
- VERSION
|
52
51
|
- lib/user_config.rb
|
53
52
|
- lib/user_config/version.rb
|
54
53
|
- spec/spec_helper.rb
|
@@ -76,8 +75,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
75
|
version: '0'
|
77
76
|
requirements: []
|
78
77
|
rubyforge_project: user_config
|
79
|
-
rubygems_version: 1.8.
|
78
|
+
rubygems_version: 1.8.10
|
80
79
|
signing_key:
|
81
80
|
specification_version: 3
|
82
81
|
summary: Management of configuration files in a user's home directory
|
83
82
|
test_files: []
|
83
|
+
has_rdoc:
|
data/VERSION
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
0.0.2
|