Module: BlogTools::Storage
- Defined in:
- lib/blog-tools/storage.rb
Overview
BlogTools::Storage handles paths and file initialization for blog-tools configuration and templates.
It is the namespace for all of the locations of files and directories the CLI accesses and writes to, as well as setting up the environment upon first run or if the configuration directory was deleted.
Constant Summary collapse
- CONFIG_DIR =
The location of the configuration directory. You can set your own with the
BLOG_TOOLS_DIR
environment variable, otherwise it will be located at~/.config/blog-tools/
ENV['BLOG_TOOLS_DIR'] || File.join(Dir.home, '.config', 'blog-tools')
- LISTS_FILE =
The location of the file used for storing lists. It is named
lists.yml
and put inside the configuration directory. File.join(CONFIG_DIR, 'lists.yml')
- CONFIG_FILE =
The locaion of the default configurations file. It is inside the configuration directory
File.join(CONFIG_DIR, 'config.yml')
- TEMPLATES_DIR =
The directory where
blog-tools
looks to find templates for generating posts. It is in the configuration directory. File.join(CONFIG_DIR, 'templates/')
- DEFAULT_TEMPLATE_FILE =
This is the location of the default template file used.
File.join(TEMPLATES_DIR, 'post.md')
- DEFAULT_TEMPLATE =
This is the markdown for the default configuration file. It is put into the file when
BlogTools::Storage.create_default_template
is run <<~'MARKDOWN' --- title: "<%= title %>" date: <%= date %> author: <%= author %> tags: [<%= tags.map { |t| "\"#{t}\"" }.join(", ") %>] --- # <%= title %> <%= content %> MARKDOWN
Class Method Summary collapse
-
.create_config_file ⇒ nil
Creates a default configuration file if one does not already exist.
-
.create_default_template ⇒ nil
Creates the default template file if the templates directory is empty.
-
.create_directories ⇒ Array
This method creates the configuration and templates directories.
-
.create_lists_file ⇒ Array<String>?
Creates an empty lists file if it doesn’t exist.
-
.read_lists ⇒ Hash
Reads and parses the lists file, returning its contents as a hash.
-
.setup! ⇒ Nil
Ensure required directories and files exist.
-
.write_lists(data) ⇒ Integer
Writes the given data to the lists file in YAML format.
Class Method Details
.create_config_file ⇒ nil
Creates a default configuration file if one does not already exist.
If the config file is missing, this method will: - Print a warning message. - Create the config directory if needed. - Write a default config YAML file.
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/blog-tools/storage.rb', line 116 def self.create_config_file return if File.exist?(CONFIG_FILE) FileUtils.mkdir_p(File.dirname(CONFIG_FILE)) puts '[!] No configuration file found, generating now...'.colorize(:red) default_config = { 'author' => ENV['USER'] || 'changeme', 'default_template' => 'post.md', 'tags' => ['blog'] } File.write(CONFIG_FILE, default_config.to_yaml) end |
.create_default_template ⇒ nil
Creates the default template file if the templates directory is empty.
Writes the default post template to post.md
and prints a success message.
137 138 139 140 141 142 |
# File 'lib/blog-tools/storage.rb', line 137 def self.create_default_template return unless Dir.empty?(TEMPLATES_DIR) File.write(DEFAULT_TEMPLATE_FILE, DEFAULT_TEMPLATE) puts "[+] Created default template: #{DEFAULT_TEMPLATE_FILE}" end |
.create_directories ⇒ Array
This method creates the configuration and templates directories.
The directories are located at ~/.config/blog-tools
, and at
~/.config/blog-tools/templates
73 74 75 76 |
# File 'lib/blog-tools/storage.rb', line 73 def self.create_directories FileUtils.mkdir_p(CONFIG_DIR) FileUtils.mkdir_p(TEMPLATES_DIR) end |
.create_lists_file ⇒ Array<String>?
Creates an empty lists file if it doesn’t exist.
81 82 83 |
# File 'lib/blog-tools/storage.rb', line 81 def self.create_lists_file FileUtils.touch(LISTS_FILE) unless File.exist?(LISTS_FILE) end |
.read_lists ⇒ Hash
Reads and parses the lists file, returning its contents as a hash.
If the file doesn’t exist or contains invalid YAML, returns an empty hash.
98 99 100 101 102 103 104 105 106 |
# File 'lib/blog-tools/storage.rb', line 98 def self.read_lists return {} unless File.exist?(LISTS_FILE) data = YAML.safe_load_file(LISTS_FILE, permitted_classes: [Symbol, Date, Time], aliases: true) data.is_a?(Hash) ? data : {} rescue Psych::SyntaxError => e warn "[!] Failed to parse lists.yml: #{e.}".colorize(:red) {} end |
.setup! ⇒ Nil
Ensure required directories and files exist.
This method is used to completely setup the configuration environment
It creates the ~/.config/blog-tools
directory, and the files
and templates that are needed for the tool to run.
60 61 62 63 64 65 |
# File 'lib/blog-tools/storage.rb', line 60 def self.setup! create_directories create_lists_file create_config_file create_default_template end |
.write_lists(data) ⇒ Integer
Writes the given data to the lists file in YAML format.
89 90 91 |
# File 'lib/blog-tools/storage.rb', line 89 def self.write_lists(data) File.write(LISTS_FILE, data.to_yaml) end |