Class: ConstConf::DirPlugin::ConfigDir
- Inherits:
-
Object
- Object
- ConstConf::DirPlugin::ConfigDir
- Defined in:
- lib/const_conf/dir_plugin.rb
Overview
A configuration directory handler that provides methods for deriving directory paths, joining paths, and reading file contents from a specified directory structure.
The ConfigDir class is designed to work with the XDG Base Directory Specification and supports reading files from directories with optional environment variable configuration for the root path. It provides a clean interface for accessing configuration files in a structured way.
Instance Method Summary collapse
-
#initialize(name, root_path: nil, env_var:, env_var_name: nil) ⇒ ConfigDir
constructor
Initializes a new instance with a name and environment variable configuration.
-
#join(path) ⇒ Pathname
(also: #+)
Joins the directory path with the given path and returns the combined result.
-
#read(path, default: nil, required: false) {|io| ... } ⇒ String?
Reads the content of a file at the given path within the configuration directory.
-
#to_s ⇒ String
Returns the string representation of the configuration directory path.
Constructor Details
#initialize(name, root_path: nil, env_var:, env_var_name: nil) ⇒ ConfigDir
Initializes a new instance with a name and environment variable configuration.
directory path look up
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/const_conf/dir_plugin.rb', line 61 def initialize(name, root_path: nil, env_var:, env_var_name: nil) !env_var.nil? && !env_var_name.nil? and raise ArgumentError, "need either the value of an env_var or the name env_var_name of an env_var" if env_var.nil? && !env_var_name.nil? env_var = ENV[env_var_name] end root_path ||= env_var @directory_path = derive_directory_path(name, root_path) end |
Instance Method Details
#join(path) ⇒ Pathname Also known as: +
Joins the directory path with the given path and returns the combined result.
85 86 87 |
# File 'lib/const_conf/dir_plugin.rb', line 85 def join(path) @directory_path + path end |
#read(path, default: nil, required: false) {|io| ... } ⇒ String?
Reads the content of a file at the given path within the configuration directory.
If the file exists, it returns the file’s content as a string encoded in UTF-8. If a block is given and the file exists, it opens the file and yields to the block. If the file does not exist and a default value is provided, it returns the default. If a block is given and the file does not exist, it yields a StringIO object containing the default value to the block.
directory does not exist
the file does not exist
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/const_conf/dir_plugin.rb', line 110 def read(path, default: nil, required: false, &block) full_path = join(path) if File.exist?(full_path) if block File.open(full_path, &block) else File.read(full_path, encoding: 'UTF-8') end else required and raise ConstConf::RequiredValueNotConfigured, "require file at #{full_path.to_s.inspect}" if default && block block.(StringIO.new(default)) else default end end end |
#to_s ⇒ String
Returns the string representation of the configuration directory path.
75 76 77 |
# File 'lib/const_conf/dir_plugin.rb', line 75 def to_s @directory_path.to_s end |