Class: BlogTools::Commands::Lists
- Inherits:
-
Thor
- Object
- Thor
- BlogTools::Commands::Lists
- Defined in:
- lib/blog-tools/commands/lists.rb
Overview
BlogTools::Commands::Lists handles post lists
List handles all of the subcommands dealing with making, editing, and creating lists of blog post ideas
Instance Method Summary collapse
-
#add(list, post_name) ⇒ Nil
Adds a post to a list.
-
#create(name) ⇒ Nil
Creates a new list.
-
#delete(list) ⇒ Nil
Deletes a list.
-
#initialize(*args) ⇒ Lists
constructor
Initializes the
lists
command with stored list data. -
#remove(list, post_name) ⇒ Nil
Removes a post from a list.
-
#show(list) ⇒ Nil
Shows all posts that are in a list.
-
#update(list, post_name) ⇒ Nil
Updates aspects of a post in a list.
Constructor Details
#initialize(*args) ⇒ Lists
Initializes the lists
command with stored list data.
This constructor is automatically called by Thor when the lists
subcommand is invoked.
It loads existing lists from storage into the @lists
instance variable.
30 31 32 33 |
# File 'lib/blog-tools/commands/lists.rb', line 30 def initialize(*args) super @lists = Storage.read_lists || {} end |
Instance Method Details
#add(list, post_name) ⇒ Nil
Adds a post to a list
This command takes the name of a list and the name of a post to be added. Assuming that the list exists, it will add the post to the list, and write the result to the list configuration file
134 135 136 137 138 139 140 |
# File 'lib/blog-tools/commands/lists.rb', line 134 def add(list, post_name) return puts '[!] List not found'.colorize(:red) unless @lists[list] @lists[list][:posts][post_name] = { completed: false, in_progress: false } Storage.write_lists(@lists) puts "[✓] Added #{post_name} to #{list} list".colorize(:green) end |
#create(name) ⇒ Nil
Creates a new list
This command takes in a name for a list, and then creates a new list in the lists configuration file.
115 116 117 118 119 |
# File 'lib/blog-tools/commands/lists.rb', line 115 def create(name) @lists[name] = { posts: {} } Storage.write_lists(@lists) puts "[✓] Created list: #{name}".colorize(:green) end |
#delete(list) ⇒ Nil
Deletes a list
This command takes in the name of a list and deletes it from the lists
configuration file. It requires user input to confirm the deletion of
the list. Once the user gives confirmation, it edits the @lists
variable and writes it back to the lists configuration file.
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/blog-tools/commands/lists.rb', line 158 def delete(list) return puts '[!] List not found'.colorize(:red) unless @lists[list] puts "[?] Are you sure you want to delete the '#{list}' list?".colorize(:yellow) print "[?] This action cannot be undone. Proceed? (y/N)\n> ".colorize(:yellow) input = $stdin.gets.chomp.strip case input.downcase when 'y' @lists.delete(list) Storage.write_lists(@lists) puts "[✓] Deleted '#{list}' list".colorize(:green) when 'n', '' puts '[i] Cancelled deletion.'.colorize(:blue) else puts '[!] Invalid input. Not deleting.'.colorize(:red) end end |
#remove(list, post_name) ⇒ Nil
Removes a post from a list
This command take in both a list name and a post name, and then removes
the post from the list. It requires user confirmation in order to make
the deletion. Once the user confirms, it edits the @lists
variable,
and writes it to the lists configuration file.
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/blog-tools/commands/lists.rb', line 194 def remove(list, post_name) return puts '[!] List not found'.colorize(:red) unless @lists[list] puts "[?] Are you sure you want to delete the post '#{post_name}'?".colorize(:yellow) print "[?] This action cannot be undone. Proceed? (y/N)\n> ".colorize(:yellow) input = $stdin.gets.chomp.strip case input.downcase when 'y' @lists[list][:posts].delete(post_name) Storage.write_lists(@lists) puts "[✓] Deleted '#{post_name}' post".colorize(:green) when 'n', '' puts '[i] Cancelled deletion.'.colorize(:blue) else puts '[!] Invalid input. Not deleting.'.colorize(:red) end end |
#show(list) ⇒ Nil
Shows all posts that are in a list
This command reads the list from the @lists
variable, and then shows it
You can customize what it shows (completed/uncompleted), and if it shows
the status of the post.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/blog-tools/commands/lists.rb', line 79 def show(list) return puts '[!] List not found'.colorize(:red) unless @lists[list] puts list.upcase @lists[list][:posts].each do |item, data| next if [:completed] && !data[:completed] next if [:in_progress] && !data[:in_progress] if [:status] status = if data[:completed] '[✓]' elsif data[:in_progress] '[~]' else '[ ]' end puts "- #{status} #{item}" else puts "- #{item}" end end end |
#update(list, post_name) ⇒ Nil
Updates aspects of a post in a list
This command takes a list and post, and requires a flag to be set to run. If no flag is set it will return. The set flag and the parameter with the flag are added into the lists configuration file under the post.
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
# File 'lib/blog-tools/commands/lists.rb', line 244 def update(list, post_name) unless [:completed] || [:in_progress] || [:tags] || [:path] return puts '[!] Please specify a status. Type "blog-tools lists help update" for more info.'.colorize(:red) end return puts '[!] List not found'.colorize(:red) unless @lists[list] return puts '[!] Post not found'.colorize(:red) unless @lists[list][:posts].key?(post_name) post = @lists[list][:posts][post_name] if [:completed] if post[:completed] puts '[!] Post already marked as complete'.colorize(:redd) else post[:completed] = true puts "[✓] Marked '#{post_name}' as complete".colorize(:green) end end if [:in_progress] if post[:in_progress] puts '[!] Post already marked as in progress'.colorize(:red) else post[:in_progress] = true puts "[✓] Marked '#{post_name}' as in progress".colorize(:green) end end post[:tags] = [:tags] if [:tags] puts "Added the following tags to the post: #{[:tags]}" if [:tags] post[:path] = [:path] if [:path] puts "The post content is located at: #{[:path]}" if [:path] Storage.write_lists(@lists) nil end |