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.4"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of UserConfig.

Parameters:

  • directory_name (String)

    :name of a configuration directory.

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :home (String)

    Root directory, of which default value is ENV.

  • :permission (Integer)

    A file permission of the configuration directory, of which default value is 0700.

  • :new_directory (boolean)

    If opts is true and the directory exists then an error raises.


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.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


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.

Parameters:

  • path (String)

    Relative path of the specified yaml file

  • default_hash (Hash)

    Default values


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

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

.default_valueObject

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_pathsObject

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.

Parameters:

  • path (String)

    Relative path of target file

  • value (Hash) (defaults to: nil)

    Values to be saved to the file


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.

Returns:

  • (Boolean)

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.

Parameters:

  • path (String)

    Relative path of a specified file

  • create_directory (boolean) (defaults to: nil)

    If the value is true then we create parent directories recursively.

Returns:

  • (String)

    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.

Parameters:

  • dir (String)

    A directory in which you want to list files.

  • opts (Hash) (defaults to: {})

    Options

Options Hash (opts):

  • :absolute (boolean)

    If the value is true then we get an array of absolute paths.


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.

Parameters:

  • path (String)

    Relative path of a file to be loaded

Returns:

  • (hash)

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.

Parameters:

  • path (String)

    Relative path of a directory

  • opts (Hash) (defaults to: {})

    Options

Options Hash (opts):

  • :mode (Integer)

    Permission for a directory to be created


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.

Parameters:

  • path (String)

    A path of the file

  • mode (String)

    A mode


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

Parameters:

  • path (String)

    A relative path of a file

Returns:

  • (String)

    Strings of the 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_allObject

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