Class: UserConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/user_config.rb,
lib/user_config/version.rb

Defined Under Namespace

Classes: DirectoryExistenceError, YAMLFile

Constant Summary collapse

VERSION =
"0.0.3"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory_name, opts = {}) ⇒ UserConfig

directory_name is a name of a configuration directory. opts is root directory, of which default value is ENV. opts is a file permission of the configuration directory, of which default value is 0700. If opts is true and the directory exists then an error raises.



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/user_config.rb', line 29

def initialize(directory_name, opts = {})
  @directory = File.expand_path(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

#directoryObject (readonly)

Returns the value of attribute directory.



21
22
23
# File 'lib/user_config.rb', line 21

def directory
  @directory
end

Class Method Details

.default(path, default_hash) ⇒ Object



17
18
19
# File 'lib/user_config.rb', line 17

def self.default(path, default_hash)
  self.default_value[path] = default_hash
end

.default_valueObject



10
11
12
13
14
15
# File 'lib/user_config.rb', line 10

def self.default_value
  unless @default_value
    @default_value = {}
  end
  @default_value
end

Instance Method Details

#all_file_pathsObject

Return an array of paths of all cached files.



90
91
92
93
94
# File 'lib/user_config.rb', line 90

def all_file_paths
  @file.map do |ary|
    file_path(ary[0])
  end
end

#create(path, value = nil) ⇒ Object

Save the configuration file of path. If we set a hash to value then its value is saved to the new configuration file.



65
66
67
68
# File 'lib/user_config.rb', line 65

def create(path, value = nil)
  yaml_file = load_file(path, true, value)
  yaml_file.save
end

#delete(path) ⇒ Object

Delete a file of path.



111
112
113
114
115
116
117
# File 'lib/user_config.rb', line 111

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.

Returns:

  • (Boolean)


105
106
107
108
# File 'lib/user_config.rb', line 105

def exist?(path)
  fpath = file_path(path)
  File.exist?(fpath) ? fpath : false
end

#file_path(path, create_directory = nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/user_config.rb', line 42

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 directory dir. If opts is true, return an array of absolute paths.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/user_config.rb', line 121

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) ⇒ Object Also known as: []

Load the configuration file of path.



83
84
85
# File 'lib/user_config.rb', line 83

def load(path)
  @file[path] || load_file(path)
end

#make_directory(path, opts = {}) ⇒ Object

Make directory under the configuration directory.



71
72
73
74
75
76
77
78
79
80
# File 'lib/user_config.rb', line 71

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 file of path with mode.



139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/user_config.rb', line 139

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) ⇒ Object

Read file of path and return a string.



155
156
157
158
159
160
161
162
# File 'lib/user_config.rb', line 155

def read(path)
  fpath = file_path(path)
  if File.exist?(fpath)
    File.read(fpath)
  else
    nil
  end
end

#save_allObject

Save all configuration files that have already loaded.



97
98
99
100
101
# File 'lib/user_config.rb', line 97

def save_all
  @file.each_value do |yaml_file|
    yaml_file.save
  end
end