user_config 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Rakefile +1 -1
- data/VERSION +1 -1
- data/lib/user_config.rb +81 -8
- data/spec/user_config_spec.rb +89 -11
- data/user_config.gemspec +64 -0
- metadata +5 -4
data/Rakefile
CHANGED
@@ -18,7 +18,7 @@ Jeweler::Tasks.new do |gem|
|
|
18
18
|
gem.homepage = "http://github.com/ytaka/user_config"
|
19
19
|
gem.license = "GPLv3"
|
20
20
|
gem.summary = "Management of configuration files in a user's home directory"
|
21
|
-
gem.description = "The
|
21
|
+
gem.description = "The library creates, saves, and loads configuration files, which are in a user's home directory or a specified directory."
|
22
22
|
gem.email = "d@ytak.info"
|
23
23
|
gem.authors = ["Takayuki YAMAGUCHI"]
|
24
24
|
# dependencies defined in Gemfile
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.1
|
data/lib/user_config.rb
CHANGED
@@ -1,21 +1,27 @@
|
|
1
1
|
require 'fileutils'
|
2
2
|
require 'yaml'
|
3
|
+
require 'pathname'
|
3
4
|
|
4
5
|
class UserConfig
|
5
|
-
|
6
|
+
def self.default_value
|
7
|
+
unless @default_value
|
8
|
+
@default_value = {}
|
9
|
+
end
|
10
|
+
@default_value
|
11
|
+
end
|
6
12
|
|
7
13
|
def self.default(path, default_hash)
|
8
|
-
|
14
|
+
self.default_value[path] = default_hash
|
9
15
|
end
|
10
16
|
|
11
17
|
attr_reader :directory
|
12
18
|
|
13
19
|
# +directory_name+ is a name of a configuration directory.
|
14
|
-
# opts[:
|
20
|
+
# opts[:home] is root directory, of which default value is ENV['HOME'].
|
15
21
|
# opts[:permission] is a file permission of the configuration directory,
|
16
22
|
# of which default value is 0700.
|
17
23
|
def initialize(directory_name, opts = {})
|
18
|
-
@directory = File.expand_path(File.join(opts[:
|
24
|
+
@directory = File.expand_path(File.join(opts[:home] || ENV['HOME'], directory_name))
|
19
25
|
@file = {}
|
20
26
|
unless File.exist?(@directory)
|
21
27
|
FileUtils.mkdir_p(@directory)
|
@@ -24,11 +30,14 @@ class UserConfig
|
|
24
30
|
end
|
25
31
|
|
26
32
|
def file_path(path)
|
33
|
+
if Pathname(path).absolute?
|
34
|
+
raise ArgumentError, "Path '#{path}' is absolute."
|
35
|
+
end
|
27
36
|
File.join(@directory, path)
|
28
37
|
end
|
29
38
|
|
30
39
|
def default_value(path)
|
31
|
-
|
40
|
+
self.class.default_value[path] || {}
|
32
41
|
end
|
33
42
|
private :default_value
|
34
43
|
|
@@ -53,6 +62,7 @@ class UserConfig
|
|
53
62
|
if opts[:mode]
|
54
63
|
FileUtils.chmod(fpath, opts[:mode])
|
55
64
|
end
|
65
|
+
fpath
|
56
66
|
end
|
57
67
|
|
58
68
|
# Load the configuration file of +path+.
|
@@ -62,7 +72,7 @@ class UserConfig
|
|
62
72
|
|
63
73
|
alias_method :[], :load
|
64
74
|
|
65
|
-
# Return an array of paths of all files.
|
75
|
+
# Return an array of paths of all cached files.
|
66
76
|
def all_file_paths
|
67
77
|
@file.map do |ary|
|
68
78
|
file_path(ary[0])
|
@@ -76,6 +86,69 @@ class UserConfig
|
|
76
86
|
end
|
77
87
|
end
|
78
88
|
|
89
|
+
# Return full path if +path+ exists under the configuration directory.
|
90
|
+
# Otherwise, false.
|
91
|
+
def exist?(path)
|
92
|
+
fpath = file_path(path)
|
93
|
+
File.exist?(fpath) ? fpath : false
|
94
|
+
end
|
95
|
+
|
96
|
+
# Delete a file of +path+.
|
97
|
+
def delete(path)
|
98
|
+
if path.size > 0
|
99
|
+
FileUtils.rm_r(file_path(path))
|
100
|
+
else
|
101
|
+
raise ArgumentError, "Path string is empty."
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
# List files in directory +dir+.
|
106
|
+
# If opts[:absolute] is true, return an array of absolute paths.
|
107
|
+
def list_in_directory(dir, opts = {})
|
108
|
+
fpath = file_path(dir)
|
109
|
+
if File.directory?(fpath)
|
110
|
+
files = Dir.entries(fpath).delete_if do |d|
|
111
|
+
/^\.+$/ =~ d
|
112
|
+
end.sort
|
113
|
+
if opts[:absolute]
|
114
|
+
files.map! do |path|
|
115
|
+
File.join(fpath, path)
|
116
|
+
end
|
117
|
+
end
|
118
|
+
files
|
119
|
+
else
|
120
|
+
nil
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
# Open file of +path+ with +mode+.
|
125
|
+
def open(path, mode, &block)
|
126
|
+
fpath = file_path(path)
|
127
|
+
unless File.exist?((dir = File.dirname(fpath)))
|
128
|
+
FileUtils.mkdir_p(dir)
|
129
|
+
end
|
130
|
+
f = Kernel.open(fpath, mode)
|
131
|
+
if block_given?
|
132
|
+
begin
|
133
|
+
yield(f)
|
134
|
+
ensure
|
135
|
+
f.close
|
136
|
+
end
|
137
|
+
else
|
138
|
+
f
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
# Read file of +path+ and return a string.
|
143
|
+
def read(path)
|
144
|
+
fpath = file_path(path)
|
145
|
+
if File.exist?(fpath)
|
146
|
+
File.read(fpath)
|
147
|
+
else
|
148
|
+
nil
|
149
|
+
end
|
150
|
+
end
|
151
|
+
|
79
152
|
class YAMLFile
|
80
153
|
attr_reader :path
|
81
154
|
|
@@ -109,8 +182,8 @@ class UserConfig
|
|
109
182
|
unless File.exist?((dir = File.dirname(path)))
|
110
183
|
FileUtils.mkdir_p(dir)
|
111
184
|
end
|
112
|
-
open(path, 'w') do |f|
|
113
|
-
|
185
|
+
Kernel.open(path, 'w') do |f|
|
186
|
+
YAML.dump(@cache, f)
|
114
187
|
end
|
115
188
|
end
|
116
189
|
|
data/spec/user_config_spec.rb
CHANGED
@@ -1,30 +1,39 @@
|
|
1
1
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
2
|
|
3
|
+
class UserConfigCustom < UserConfig
|
4
|
+
end
|
5
|
+
|
3
6
|
describe UserConfig do
|
4
7
|
before(:all) do
|
5
|
-
@
|
6
|
-
if File.exist?(@
|
7
|
-
raise "Temporary configuration directory '#{@
|
8
|
+
@home = File.join(File.dirname(__FILE__), 'config')
|
9
|
+
if File.exist?(@home)
|
10
|
+
raise "Temporary configuration directory '#{@home}' exists."
|
8
11
|
end
|
9
12
|
end
|
10
13
|
|
11
14
|
subject do
|
12
|
-
UserConfig.new('test', :
|
15
|
+
UserConfig.new('test', :home => @home)
|
13
16
|
end
|
14
17
|
|
15
18
|
it "should return directory" do
|
16
|
-
uconf = UserConfig.new('dir1', :
|
17
|
-
uconf.directory.should == File.expand_path(File.join(@
|
19
|
+
uconf = UserConfig.new('dir1', :home => @home)
|
20
|
+
uconf.directory.should == File.expand_path(File.join(@home, 'dir1'))
|
18
21
|
end
|
19
22
|
|
20
23
|
it "should create directory" do
|
21
|
-
uconf = UserConfig.new('dir2', :
|
22
|
-
File.exist?(File.join(@
|
24
|
+
uconf = UserConfig.new('dir2', :home => @home)
|
25
|
+
File.exist?(File.join(@home, 'dir2')).should be_true
|
23
26
|
end
|
24
27
|
|
25
28
|
it "should return file path under the directory" do
|
26
|
-
uconf = UserConfig.new('dir3', :
|
27
|
-
uconf.file_path('path.yaml').should == File.join(@
|
29
|
+
uconf = UserConfig.new('dir3', :home => @home)
|
30
|
+
uconf.file_path('path.yaml').should == File.join(@home, 'dir3', 'path.yaml')
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should raise error" do
|
34
|
+
lambda do
|
35
|
+
subject.file_path('/abc/def.yaml')
|
36
|
+
end.should raise_error ArgumentError
|
28
37
|
end
|
29
38
|
|
30
39
|
it "should set default value" do
|
@@ -86,7 +95,76 @@ describe UserConfig do
|
|
86
95
|
File.read(full_path).should match(/^third/)
|
87
96
|
end
|
88
97
|
|
98
|
+
it "should return false" do
|
99
|
+
subject.exist?('not_exist/file.yaml').should_not be_true
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should return path" do
|
103
|
+
path = 'exist/file.yaml'
|
104
|
+
subject.create(path)
|
105
|
+
subject.exist?(path).should == File.join(subject.directory, path)
|
106
|
+
end
|
107
|
+
|
108
|
+
it "should delete a file" do
|
109
|
+
path = 'delete/file.yaml'
|
110
|
+
subject.create(path)
|
111
|
+
subject.delete(path)
|
112
|
+
subject.exist?(path).should_not be_true
|
113
|
+
end
|
114
|
+
|
115
|
+
it "should return nil" do
|
116
|
+
subject.list_in_directory('not_exist2/').should be_nil
|
117
|
+
end
|
118
|
+
|
119
|
+
it "should return list of files" do
|
120
|
+
files = ['list/file1.yaml', 'list/file2.yaml']
|
121
|
+
files.each do |path|
|
122
|
+
subject.create(path)
|
123
|
+
end
|
124
|
+
subject.list_in_directory('list').should == files.map do |path|
|
125
|
+
path.sub(/^list\//, '')
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
it "should return list of files of absolute path" do
|
130
|
+
files = ['list2/file1.yaml', 'list2/file2.yaml']
|
131
|
+
files.each do |path|
|
132
|
+
subject.create(path)
|
133
|
+
end
|
134
|
+
subject.list_in_directory('list2', :absolute => true).should == files.map do |path|
|
135
|
+
File.join(subject.directory, path)
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
it "should save a raw file" do
|
140
|
+
path ='save/raw/hello.txt'
|
141
|
+
subject.open(path, 'w') do |f|
|
142
|
+
f.puts 'hello world'
|
143
|
+
end
|
144
|
+
fpath = File.join(subject.directory, path)
|
145
|
+
File.exist?(fpath).should be_true
|
146
|
+
File.read(fpath).strip.should == 'hello world'
|
147
|
+
end
|
148
|
+
|
149
|
+
it "should read a file" do
|
150
|
+
path ='save/raw/hello.txt'
|
151
|
+
subject.open(path, 'w') do |f|
|
152
|
+
f.puts 'hello world'
|
153
|
+
end
|
154
|
+
fpath = File.join(subject.directory, path)
|
155
|
+
subject.read(path).should == File.read(fpath)
|
156
|
+
end
|
157
|
+
|
158
|
+
it "should return nil" do
|
159
|
+
subject.read('not_exist/file.yaml').should be_nil
|
160
|
+
end
|
161
|
+
|
162
|
+
it "should have different default value" do
|
163
|
+
UserConfigCustom.default_value.should be_an_instance_of Hash
|
164
|
+
UserConfig.default_value.object_id.should_not == UserConfigCustom.default_value.object_id
|
165
|
+
end
|
166
|
+
|
89
167
|
after(:all) do
|
90
|
-
FileUtils.rm_r(@
|
168
|
+
FileUtils.rm_r(@home)
|
91
169
|
end
|
92
170
|
end
|
data/user_config.gemspec
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{user_config}
|
8
|
+
s.version = "0.0.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Takayuki YAMAGUCHI"]
|
12
|
+
s.date = %q{2011-07-03}
|
13
|
+
s.description = %q{The library creates, saves, and loads configuration files, which are in a user's home directory or a specified directory.}
|
14
|
+
s.email = %q{d@ytak.info}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.md"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".rspec",
|
22
|
+
"Gemfile",
|
23
|
+
"LICENSE.txt",
|
24
|
+
"README.md",
|
25
|
+
"Rakefile",
|
26
|
+
"VERSION",
|
27
|
+
"lib/user_config.rb",
|
28
|
+
"spec/spec_helper.rb",
|
29
|
+
"spec/user_config_spec.rb",
|
30
|
+
"spec/yaml/test_load.yaml",
|
31
|
+
"spec/yaml_file_spec.rb",
|
32
|
+
"user_config.gemspec"
|
33
|
+
]
|
34
|
+
s.homepage = %q{http://github.com/ytaka/user_config}
|
35
|
+
s.licenses = ["GPLv3"]
|
36
|
+
s.require_paths = ["lib"]
|
37
|
+
s.rubygems_version = %q{1.6.2}
|
38
|
+
s.summary = %q{Management of configuration files in a user's home directory}
|
39
|
+
|
40
|
+
if s.respond_to? :specification_version then
|
41
|
+
s.specification_version = 3
|
42
|
+
|
43
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
44
|
+
s.add_development_dependency(%q<rspec>, [">= 2.6.0"])
|
45
|
+
s.add_development_dependency(%q<yard>, [">= 0.7.2"])
|
46
|
+
s.add_development_dependency(%q<bundler>, [">= 1.0.0"])
|
47
|
+
s.add_development_dependency(%q<jeweler>, [">= 1.6.2"])
|
48
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
49
|
+
else
|
50
|
+
s.add_dependency(%q<rspec>, [">= 2.6.0"])
|
51
|
+
s.add_dependency(%q<yard>, [">= 0.7.2"])
|
52
|
+
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
53
|
+
s.add_dependency(%q<jeweler>, [">= 1.6.2"])
|
54
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
55
|
+
end
|
56
|
+
else
|
57
|
+
s.add_dependency(%q<rspec>, [">= 2.6.0"])
|
58
|
+
s.add_dependency(%q<yard>, [">= 0.7.2"])
|
59
|
+
s.add_dependency(%q<bundler>, [">= 1.0.0"])
|
60
|
+
s.add_dependency(%q<jeweler>, [">= 1.6.2"])
|
61
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: user_config
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Takayuki YAMAGUCHI
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-07-
|
13
|
+
date: 2011-07-03 00:00:00 +09:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
@@ -68,7 +68,7 @@ dependencies:
|
|
68
68
|
type: :development
|
69
69
|
prerelease: false
|
70
70
|
version_requirements: *id005
|
71
|
-
description: The
|
71
|
+
description: The library creates, saves, and loads configuration files, which are in a user's home directory or a specified directory.
|
72
72
|
email: d@ytak.info
|
73
73
|
executables: []
|
74
74
|
|
@@ -90,6 +90,7 @@ files:
|
|
90
90
|
- spec/user_config_spec.rb
|
91
91
|
- spec/yaml/test_load.yaml
|
92
92
|
- spec/yaml_file_spec.rb
|
93
|
+
- user_config.gemspec
|
93
94
|
has_rdoc: true
|
94
95
|
homepage: http://github.com/ytaka/user_config
|
95
96
|
licenses:
|
@@ -104,7 +105,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
104
105
|
requirements:
|
105
106
|
- - ">="
|
106
107
|
- !ruby/object:Gem::Version
|
107
|
-
hash:
|
108
|
+
hash: 2431112138724354384
|
108
109
|
segments:
|
109
110
|
- 0
|
110
111
|
version: "0"
|