Class: SpreenWiki::Application
- Inherits:
-
Object
- Object
- SpreenWiki::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.
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/spreen_wiki/application.rb', line 34 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.
62 63 64 |
# File 'lib/spreen_wiki/application.rb', line 62 def base_path @base_path end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
62 63 64 |
# File 'lib/spreen_wiki/application.rb', line 62 def config @config end |
#excluded_paths ⇒ Object (readonly)
Returns the value of attribute excluded_paths.
62 63 64 |
# File 'lib/spreen_wiki/application.rb', line 62 def excluded_paths @excluded_paths end |
#group_by ⇒ Object (readonly)
Returns the value of attribute group_by.
62 63 64 |
# File 'lib/spreen_wiki/application.rb', line 62 def group_by @group_by end |
#home_overflow ⇒ Object (readonly)
Returns the value of attribute home_overflow.
62 63 64 |
# File 'lib/spreen_wiki/application.rb', line 62 def home_overflow @home_overflow end |
#language ⇒ Object (readonly)
Returns the value of attribute language.
62 63 64 |
# File 'lib/spreen_wiki/application.rb', line 62 def language @language end |
#path_to_home ⇒ Object (readonly)
Returns the value of attribute path_to_home.
62 63 64 |
# File 'lib/spreen_wiki/application.rb', line 62 def path_to_home @path_to_home end |
#path_to_sidebar ⇒ Object (readonly)
Returns the value of attribute path_to_sidebar.
62 63 64 |
# File 'lib/spreen_wiki/application.rb', line 62 def @path_to_sidebar end |
#paths_to_wikis ⇒ Object (readonly)
Returns the value of attribute paths_to_wikis.
62 63 64 |
# File 'lib/spreen_wiki/application.rb', line 62 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
21 22 23 24 25 |
# File 'lib/spreen_wiki/application.rb', line 21 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
132 133 134 135 136 137 |
# File 'lib/spreen_wiki/application.rb', line 132 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
104 105 106 |
# File 'lib/spreen_wiki/application.rb', line 104 def no_declaration @no_declaration ||= config.no_declaration(group_by, language) end |
#owned_wiki_maps ⇒ Hash[String, Array[String]]
140 141 142 |
# File 'lib/spreen_wiki/application.rb', line 140 def owned_wiki_maps @owned_wiki_maps ||= wiki_maps_with_namespace.select { |namespace, _| namespace.include?('@') } end |
#parse_home_overflow(raw) ⇒ bool, String
81 82 83 84 85 86 87 |
# File 'lib/spreen_wiki/application.rb', line 81 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]]
145 146 147 |
# File 'lib/spreen_wiki/application.rb', line 145 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.
122 123 124 125 126 127 128 |
# File 'lib/spreen_wiki/application.rb', line 122 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
56 57 58 |
# File 'lib/spreen_wiki/application.rb', line 56 def run raise NotImplementedError, 'This method must be implemented in each subclass.' end |
#target_paths ⇒ Array[String]
90 91 92 93 94 95 96 |
# File 'lib/spreen_wiki/application.rb', line 90 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
99 100 101 |
# File 'lib/spreen_wiki/application.rb', line 99 def target_regexp @target_regexp ||= config.target_regexp(group_by) end |
#validate! ⇒ void
This method returns an undefined value.
48 49 50 51 52 53 |
# File 'lib/spreen_wiki/application.rb', line 48 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.
73 74 75 76 77 |
# File 'lib/spreen_wiki/application.rb', line 73 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]]
109 110 111 112 113 114 115 116 |
# File 'lib/spreen_wiki/application.rb', line 109 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 |