Class: UserConfig
- Inherits:
-
Object
- Object
- UserConfig
- Defined in:
- lib/user_config.rb,
lib/user_config/version.rb
Defined Under Namespace
Classes: DirectoryExistenceError, YAMLFile
Constant Summary collapse
- VERSION =
"0.0.4"
Instance Attribute Summary collapse
-
#directory ⇒ Object
readonly
Returns the value of attribute directory.
Class Method Summary collapse
-
.default(path, default_hash) ⇒ Object
Set default values of the specified file of current class.
-
.default_value ⇒ Object
Get default value of current class.
Instance Method Summary collapse
-
#all_file_paths ⇒ Object
Return an array of paths of all cached files.
-
#create(path, value = nil) ⇒ Object
Save the configuration file under the directory.
-
#delete(path) ⇒ Object
Delete a file of
path
. -
#exist?(path) ⇒ Boolean
Return full path if
path
exists under the configuration directory. -
#file_path(path, create_directory = nil) ⇒ String
An absolute path of a specified file.
-
#initialize(directory_name, opts = {}) ⇒ UserConfig
constructor
A new instance of UserConfig.
-
#list_in_directory(dir, opts = {}) ⇒ Object
List files in the specified directory.
-
#load(path) ⇒ hash
(also: #[])
Load the configuration file.
-
#make_directory(path, opts = {}) ⇒ Object
Make directory under the configuration directory.
-
#open(path, mode, &block) ⇒ Object
Open a file.
-
#read(path) ⇒ String
Read a file.
-
#save_all ⇒ Object
Save all configuration files that have already loaded.
Constructor Details
#initialize(directory_name, opts = {}) ⇒ UserConfig
Returns a new instance of UserConfig.
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/user_config.rb', line 34 def initialize(directory_name, opts = {}) @directory = File.(File.join(opts[:home] || ENV['HOME'], directory_name)) @file = {} if File.exist?(@directory) if opts[:new_directory] raise UserConfig::DirectoryExistenceError, "Can not create new directory: #{@directory}" end else FileUtils.mkdir_p(@directory) FileUtils.chmod(opts[:permission] || 0700, @directory) end end |
Instance Attribute Details
#directory ⇒ Object (readonly)
Returns the value of attribute directory.
26 27 28 |
# File 'lib/user_config.rb', line 26 def directory @directory end |
Class Method Details
.default(path, default_hash) ⇒ Object
Set default values of the specified file of current class.
22 23 24 |
# File 'lib/user_config.rb', line 22 def self.default(path, default_hash) self.default_value[path] = default_hash end |
.default_value ⇒ Object
Get default value of current class. The value is a hash, of which keys are paths of yaml files.
12 13 14 15 16 17 |
# File 'lib/user_config.rb', line 12 def self.default_value unless @default_value @default_value = {} end @default_value end |
Instance Method Details
#all_file_paths ⇒ Object
Return an array of paths of all cached files.
104 105 106 107 108 |
# File 'lib/user_config.rb', line 104 def all_file_paths @file.map do |ary| file_path(ary[0]) end end |
#create(path, value = nil) ⇒ Object
Save the configuration file under the directory.
74 75 76 77 |
# File 'lib/user_config.rb', line 74 def create(path, value = nil) yaml_file = load_file(path, true, value) yaml_file.save end |
#delete(path) ⇒ Object
Delete a file of path
.
125 126 127 128 129 130 131 |
# File 'lib/user_config.rb', line 125 def delete(path) if path.size > 0 FileUtils.rm_r(file_path(path)) else raise ArgumentError, "Path string is empty." end end |
#exist?(path) ⇒ Boolean
Return full path if path
exists under the configuration directory. Otherwise, false.
119 120 121 122 |
# File 'lib/user_config.rb', line 119 def exist?(path) fpath = file_path(path) File.exist?(fpath) ? fpath : false end |
#file_path(path, create_directory = nil) ⇒ String
Returns An absolute path of a specified file.
50 51 52 53 54 55 56 57 58 59 |
# File 'lib/user_config.rb', line 50 def file_path(path, create_directory = nil) if Pathname(path).absolute? raise ArgumentError, "Path '#{path}' is absolute." end fpath = File.join(@directory, path) if create_directory && !File.exist?((dir = File.dirname(fpath))) FileUtils.mkdir_p(dir) end fpath end |
#list_in_directory(dir, opts = {}) ⇒ Object
List files in the specified directory.
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/user_config.rb', line 137 def list_in_directory(dir, opts = {}) fpath = file_path(dir) if File.directory?(fpath) files = Dir.entries(fpath).delete_if do |d| /^\.+$/ =~ d end.sort if opts[:absolute] files.map! do |path| File.join(fpath, path) end end files else nil end end |
#load(path) ⇒ hash Also known as: []
Load the configuration file.
97 98 99 |
# File 'lib/user_config.rb', line 97 def load(path) @file[path] || load_file(path) end |
#make_directory(path, opts = {}) ⇒ Object
Make directory under the configuration directory.
83 84 85 86 87 88 89 90 91 92 |
# File 'lib/user_config.rb', line 83 def make_directory(path, opts = {}) fpath = file_path(path) unless File.exist?(fpath) FileUtils.mkdir_p(fpath) end if opts[:mode] FileUtils.chmod(fpath, opts[:mode]) end fpath end |
#open(path, mode, &block) ⇒ Object
Open a file.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/user_config.rb', line 157 def open(path, mode, &block) full_path = file_path(path, true) f = Kernel.open(full_path, mode) if block_given? begin yield(f) ensure f.close end full_path else f end end |
#read(path) ⇒ String
Read a file
175 176 177 178 179 180 181 182 |
# File 'lib/user_config.rb', line 175 def read(path) fpath = file_path(path) if File.exist?(fpath) File.read(fpath) else nil end end |
#save_all ⇒ Object
Save all configuration files that have already loaded.
111 112 113 114 115 |
# File 'lib/user_config.rb', line 111 def save_all @file.each_value do |yaml_file| yaml_file.save end end |