Class: Spreen::Wiki::Application
- Inherits:
-
Object
- Object
- Spreen::Wiki::Application
- Defined in:
- lib/spreen/wiki/application.rb,
sig/generated/spreen/wiki/application.rbs
Overview
Base class for the wiki-organising commands. Loads the wiki tree under
base_path, groups it by Owner or Category in either English or Japanese,
and exposes the resulting maps to its subclasses. Grouping criteria,
languages and labels are resolved through Configuration and can be
extended via a .spreen.yml file.
Direct Known Subclasses
Home, Sidebar, UnknownWikiCountListExporter, UnknownWikiListExporterForLLM
Defined Under Namespace
Classes: NotImplementedError
Instance Attribute Summary collapse
-
#base_path ⇒ Object
readonly
Returns the value of attribute base_path.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#excluded_paths ⇒ Object
readonly
Returns the value of attribute excluded_paths.
-
#group_by ⇒ Object
readonly
Returns the value of attribute group_by.
-
#home_overflow ⇒ Object
readonly
Returns the value of attribute home_overflow.
-
#language ⇒ Object
readonly
Returns the value of attribute language.
-
#path_to_home ⇒ Object
readonly
Returns the value of attribute path_to_home.
-
#path_to_sidebar ⇒ Object
readonly
Returns the value of attribute path_to_sidebar.
-
#paths_to_wikis ⇒ Object
readonly
Returns the value of attribute paths_to_wikis.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(base_path: Dir.pwd, group_by: 'Owner', language: 'English', home_overflow: 'false', config: nil) ⇒ Application
constructor
A new instance of Application.
- #namespace_for(target_path) ⇒ String
- #no_declaration ⇒ String
- #owned_wiki_maps ⇒ Hash[String, Array[String]]
- #parse_home_overflow(raw) ⇒ bool, String
- #plain_wiki_maps ⇒ Hash[String, Array[String]]
- #populate_namespace(target_path, named, undeclared) ⇒ void
- #run ⇒ Object
- #target_paths ⇒ Array[String]
- #target_regexp ⇒ Regexp
- #validate! ⇒ void
- #validate_home_overflow! ⇒ void
- #wiki_maps_with_namespace ⇒ Hash[String, Array[String]]
Constructor Details
#initialize(base_path: Dir.pwd, group_by: 'Owner', language: 'English', home_overflow: 'false', config: nil) ⇒ Application
Returns a new instance of Application.
35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/spreen/wiki/application.rb', line 35 def initialize(base_path: Dir.pwd, group_by: 'Owner', language: 'English', home_overflow: 'false', config: nil, **) @base_path = base_path @group_by = group_by @language = language @home_overflow = parse_home_overflow(home_overflow) @config = config || Configuration.new(base_path:, **) @path_to_home = File.join(base_path, 'Home.md') @path_to_sidebar = File.join(base_path, '_Sidebar.md') @paths_to_wikis = Dir[File.join(base_path, '**', '*.md')] @excluded_paths = @config.excluded_dirs.flat_map { |dir| Dir[File.join(base_path, dir, '**', '*.md')] } end |
Instance Attribute Details
#base_path ⇒ Object (readonly)
Returns the value of attribute base_path.
63 64 65 |
# File 'lib/spreen/wiki/application.rb', line 63 def base_path @base_path end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
63 64 65 |
# File 'lib/spreen/wiki/application.rb', line 63 def config @config end |
#excluded_paths ⇒ Object (readonly)
Returns the value of attribute excluded_paths.
63 64 65 |
# File 'lib/spreen/wiki/application.rb', line 63 def excluded_paths @excluded_paths end |
#group_by ⇒ Object (readonly)
Returns the value of attribute group_by.
63 64 65 |
# File 'lib/spreen/wiki/application.rb', line 63 def group_by @group_by end |
#home_overflow ⇒ Object (readonly)
Returns the value of attribute home_overflow.
63 64 65 |
# File 'lib/spreen/wiki/application.rb', line 63 def home_overflow @home_overflow end |
#language ⇒ Object (readonly)
Returns the value of attribute language.
63 64 65 |
# File 'lib/spreen/wiki/application.rb', line 63 def language @language end |
#path_to_home ⇒ Object (readonly)
Returns the value of attribute path_to_home.
63 64 65 |
# File 'lib/spreen/wiki/application.rb', line 63 def path_to_home @path_to_home end |
#path_to_sidebar ⇒ Object (readonly)
Returns the value of attribute path_to_sidebar.
63 64 65 |
# File 'lib/spreen/wiki/application.rb', line 63 def @path_to_sidebar end |
#paths_to_wikis ⇒ Object (readonly)
Returns the value of attribute paths_to_wikis.
63 64 65 |
# File 'lib/spreen/wiki/application.rb', line 63 def paths_to_wikis @paths_to_wikis end |
Class Method Details
.run(base_path: Dir.pwd, group_by: 'Owner', language: 'English', home_overflow: 'false') ⇒ Object
22 23 24 25 26 |
# File 'lib/spreen/wiki/application.rb', line 22 def self.run(base_path: Dir.pwd, group_by: 'Owner', language: 'English', home_overflow: 'false', **) instance = new(base_path:, group_by:, language:, home_overflow:, **) instance.validate! instance.run end |
Instance Method Details
#namespace_for(target_path) ⇒ String
133 134 135 136 137 138 |
# File 'lib/spreen/wiki/application.rb', line 133 def namespace_for(target_path) declaration = File.open(target_path, &:gets) return no_declaration unless declaration&.start_with?(target_regexp) declaration.chomp.gsub(target_regexp, '') end |
#no_declaration ⇒ String
105 106 107 |
# File 'lib/spreen/wiki/application.rb', line 105 def no_declaration @no_declaration ||= config.no_declaration(group_by, language) end |
#owned_wiki_maps ⇒ Hash[String, Array[String]]
141 142 143 |
# File 'lib/spreen/wiki/application.rb', line 141 def owned_wiki_maps @owned_wiki_maps ||= wiki_maps_with_namespace.select { |namespace, _| namespace.include?('@') } end |
#parse_home_overflow(raw) ⇒ bool, String
82 83 84 85 86 87 88 |
# File 'lib/spreen/wiki/application.rb', line 82 def parse_home_overflow(raw) case raw when 'true' then true when 'false' then false else raw end end |
#plain_wiki_maps ⇒ Hash[String, Array[String]]
146 147 148 |
# File 'lib/spreen/wiki/application.rb', line 146 def plain_wiki_maps @plain_wiki_maps ||= wiki_maps_with_namespace.reject { |namespace, _| namespace.include?('@') } end |
#populate_namespace(target_path, named, undeclared) ⇒ void
This method returns an undefined value.
123 124 125 126 127 128 129 |
# File 'lib/spreen/wiki/application.rb', line 123 def populate_namespace(target_path, named, undeclared) return unless File.exist?(target_path) namespace = namespace_for(target_path) bucket = namespace == no_declaration ? undeclared : named bucket[namespace] << File.basename(target_path) end |
#run ⇒ Object
57 58 59 |
# File 'lib/spreen/wiki/application.rb', line 57 def run raise NotImplementedError, 'This method must be implemented in each subclass.' end |
#target_paths ⇒ Array[String]
91 92 93 94 95 96 97 |
# File 'lib/spreen/wiki/application.rb', line 91 def target_paths @target_paths ||= paths_to_wikis.reject do |path_to_wiki| path_to_wiki == path_to_home || path_to_wiki == || excluded_paths.include?(path_to_wiki) end end |
#target_regexp ⇒ Regexp
100 101 102 |
# File 'lib/spreen/wiki/application.rb', line 100 def target_regexp @target_regexp ||= config.target_regexp(group_by) end |
#validate! ⇒ void
This method returns an undefined value.
49 50 51 52 53 54 |
# File 'lib/spreen/wiki/application.rb', line 49 def validate! raise ArgumentError, "Invalid group_by: `#{group_by}`" unless config.group_bys.include?(group_by) raise ArgumentError, "Invalid language: `#{language}`" unless config.languages(group_by).include?(language) validate_home_overflow! end |
#validate_home_overflow! ⇒ void
This method returns an undefined value.
74 75 76 77 78 |
# File 'lib/spreen/wiki/application.rb', line 74 def validate_home_overflow! return if [true, false].include?(home_overflow) raise ArgumentError, "Invalid home_overflow: `#{home_overflow}` must be boolean" end |
#wiki_maps_with_namespace ⇒ Hash[String, Array[String]]
110 111 112 113 114 115 116 117 |
# File 'lib/spreen/wiki/application.rb', line 110 def wiki_maps_with_namespace @wiki_maps_with_namespace ||= begin named = Hash.new { |hash, namespace| hash[namespace] = [] } #: Hash[String, Array[String]] undeclared = Hash.new { |hash, namespace| hash[namespace] = [] } #: Hash[String, Array[String]] target_paths.each { |target_path| populate_namespace(target_path, named, undeclared) } named.sort.to_h.merge(undeclared) end end |