Class: UserConfig

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

Defined Under Namespace

Classes: YAMLFile

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.



23
24
25
26
27
28
29
30
# File 'lib/user_config.rb', line 23

def initialize(directory_name, opts = {})
  @directory = File.expand_path(File.join(opts[:home] || ENV['HOME'], directory_name))
  @file = {}
  unless File.exist?(@directory)
    FileUtils.mkdir_p(@directory)
    FileUtils.chmod(opts[:permission] || 0700, @directory)
  end
end

Instance Attribute Details

#directoryObject (readonly)

Returns the value of attribute directory.



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

def directory
  @directory
end

Class Method Details

.default(path, default_hash) ⇒ Object



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

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

.default_valueObject



6
7
8
9
10
11
# File 'lib/user_config.rb', line 6

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.



76
77
78
79
80
# File 'lib/user_config.rb', line 76

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.



51
52
53
54
# File 'lib/user_config.rb', line 51

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

#delete(path) ⇒ Object

Delete a file of path.



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

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)


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

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

#file_path(path) ⇒ Object



32
33
34
35
36
37
# File 'lib/user_config.rb', line 32

def file_path(path)
  if Pathname(path).absolute?
    raise ArgumentError, "Path '#{path}' is absolute."
  end
  File.join(@directory, path)
end

#list_in_directory(dir, opts = {}) ⇒ Object

List files in directory dir. If opts is true, return an array of absolute paths.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/user_config.rb', line 107

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.



69
70
71
# File 'lib/user_config.rb', line 69

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

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

Make directory under the configuration directory.



57
58
59
60
61
62
63
64
65
66
# File 'lib/user_config.rb', line 57

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.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/user_config.rb', line 125

def open(path, mode, &block)
  fpath = file_path(path)
  unless File.exist?((dir = File.dirname(fpath)))
    FileUtils.mkdir_p(dir)
  end
  f = Kernel.open(fpath, mode)
  if block_given?
    begin
      yield(f)
    ensure
      f.close
    end
  else
    f
  end
end

#read(path) ⇒ Object

Read file of path and return a string.



143
144
145
146
147
148
149
150
# File 'lib/user_config.rb', line 143

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.



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

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