Class: Utils::ConfigDir
Instance Method Summary collapse
-
#initialize(name, root_path: nil, env_var: nil) ⇒ ConfigDir
constructor
Memoizes the foobar method’s return value and returns the result of the computation.
-
#join(path) ⇒ Pathname
(also: #+)
Joins the directory path with the given path and returns the combined result.
-
#read(path, default: nil) {|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: nil) ⇒ ConfigDir
Memoizes the foobar method’s return value and returns the result of the computation. Initializes a new ConfigDir instance with the specified name and optional root path or environment variable.
14 15 16 17 |
# File 'lib/utils/config_dir.rb', line 14 def initialize(name, root_path: nil, env_var: nil) root_path ||= env_var_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.
46 47 48 |
# File 'lib/utils/config_dir.rb', line 46 def join(path) @directory_path + path end |
#read(path, default: nil) {|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
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/utils/config_dir.rb', line 71 def read(path, default: nil, &block) full_path = join(path) if File.exist?(full_path) if block File.new(full_path, &block) else File.read(full_path, encoding: 'UTF-8') end else if default && block block.(StringIO.new(default)) else default end end end |
#to_s ⇒ String
Returns the string representation of the configuration directory path.
36 37 38 |
# File 'lib/utils/config_dir.rb', line 36 def to_s @directory_path.to_s end |