Class: WritersRoom::Commands::Element
- Inherits:
-
Thor
- Object
- Thor
- WritersRoom::Commands::Element
- Defined in:
- lib/writers_room/cli/element.rb
Overview
Generic element management subcommand. A single class handles all element types, registered dynamically per medium.
Class Method Summary collapse
-
.for_type(type_name) ⇒ Object
Factory method to create a subclass for a specific element type.
Instance Method Summary collapse
- #create(name) ⇒ Object
- #list ⇒ Object
- #show(slug) ⇒ Object
- #status(slug, new_status) ⇒ Object
- #version(slug) ⇒ Object
Class Method Details
.for_type(type_name) ⇒ Object
Factory method to create a subclass for a specific element type. Assigns the class to a constant under Commands so Thor can display a human-readable name instead of an anonymous class reference.
153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/writers_room/cli/element.rb', line 153 def self.for_type(type_name) const_name = type_name.to_s.split("_").map(&:capitalize).join if Commands.const_defined?(const_name, false) return Commands.const_get(const_name, false) end klass = Class.new(self) do namespace type_name.to_s end klass.instance_variable_set(:@element_type_name, type_name.to_s) Commands.const_set(const_name, klass) klass end |
Instance Method Details
#create(name) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/writers_room/cli/element.rb', line 23 def create(name) collection = build_collection el = collection.create(name, metadata: { "status" => [:status] }, body: [:body]) say "Created #{element_type}: #{el.name} (#{el.path})", :green rescue WritersRoom::Error => e say "Error: #{e.message}", :red exit 1 end |
#list ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/writers_room/cli/element.rb', line 36 def list collection = build_collection elements = collection.list if elements.empty? say "No #{element_type} found.", :yellow return end elements.each do |el| status_tag = el[:status] ? " [#{el[:status]}]" : "" say " #{el[:name]}#{status_tag} (#{el[:slug]})", :white end end |
#show(slug) ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/writers_room/cli/element.rb', line 52 def show(slug) collection = build_collection el = collection.find_by_slug(slug) || collection.find_by_name_or_alias(slug) unless el say "Not found: #{slug}", :red exit 1 end say "#{el.name}", :green say " Slug: #{el.slug}", :white say " Status: #{el.status || 'none'}", :white say " Aliases: #{el.aliases.join(', ')}", :white unless el.aliases.empty? say " Version: #{el.version}", :white if el.version say " Path: #{el.path}", :white say "" unless el.body.empty? say el.body unless el.body.empty? end |
#status(slug, new_status) ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/writers_room/cli/element.rb', line 98 def status(slug, new_status) collection = build_collection el = collection.find_by_slug(slug) unless el say "Not found: #{slug}", :red exit 1 end el.[:status] = new_status el.save say "Updated #{el.name} status to: #{new_status}", :green end |
#version(slug) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/writers_room/cli/element.rb', line 76 def version(slug) collection = build_collection el = collection.find_by_slug(slug) unless el say "Not found: #{slug}", :red exit 1 end versioner = WritersRoom::Versioner.new(File.dirname(el.path), slug) based_on = versioner.latest_version ? File.basename(versioner.latest_version) : nil new_version = versioner.create_new_version( metadata: { "name" => el.name, "status" => [:status] }, based_on: based_on ) say "Created version #{new_version.version} of #{el.name}", :green say " Path: #{new_version.path}", :white end |