Class: SpreenWiki::Application

Inherits:
Object
  • Object
show all
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.

Defined Under Namespace

Classes: NotImplementedError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_path: Dir.pwd, group_by: 'Owner', language: 'English', home_overflow: 'false', config: nil) ⇒ Application

Returns a new instance of Application.

Parameters:

  • base_path: (String) (defaults to: Dir.pwd)
  • group_by: (String) (defaults to: 'Owner')
  • language: (String) (defaults to: 'English')
  • home_overflow: (bool, String) (defaults to: 'false')
  • config: (Configuration, nil) (defaults to: nil)
  • (Object)


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_pathObject (readonly)

Returns the value of attribute base_path.

Returns:

  • (Object)


62
63
64
# File 'lib/spreen_wiki/application.rb', line 62

def base_path
  @base_path
end

#configObject (readonly)

Returns the value of attribute config.

Returns:

  • (Object)


62
63
64
# File 'lib/spreen_wiki/application.rb', line 62

def config
  @config
end

#excluded_pathsObject (readonly)

Returns the value of attribute excluded_paths.

Returns:

  • (Object)


62
63
64
# File 'lib/spreen_wiki/application.rb', line 62

def excluded_paths
  @excluded_paths
end

#group_byObject (readonly)

Returns the value of attribute group_by.

Returns:

  • (Object)


62
63
64
# File 'lib/spreen_wiki/application.rb', line 62

def group_by
  @group_by
end

#home_overflowObject (readonly)

Returns the value of attribute home_overflow.

Returns:

  • (Object)


62
63
64
# File 'lib/spreen_wiki/application.rb', line 62

def home_overflow
  @home_overflow
end

#languageObject (readonly)

Returns the value of attribute language.

Returns:

  • (Object)


62
63
64
# File 'lib/spreen_wiki/application.rb', line 62

def language
  @language
end

#path_to_homeObject (readonly)

Returns the value of attribute path_to_home.

Returns:

  • (Object)


62
63
64
# File 'lib/spreen_wiki/application.rb', line 62

def path_to_home
  @path_to_home
end

#path_to_sidebarObject (readonly)

Returns the value of attribute path_to_sidebar.

Returns:

  • (Object)


62
63
64
# File 'lib/spreen_wiki/application.rb', line 62

def path_to_sidebar
  @path_to_sidebar
end

#paths_to_wikisObject (readonly)

Returns the value of attribute paths_to_wikis.

Returns:

  • (Object)


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

Parameters:

  • base_path: (String) (defaults to: Dir.pwd)
  • group_by: (String) (defaults to: 'Owner')
  • language: (String) (defaults to: 'English')
  • home_overflow: (bool, String) (defaults to: 'false')
  • (Object)

Returns:

  • (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

Parameters:

  • target_path (String)

Returns:

  • (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_declarationString

Returns:

  • (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_mapsHash[String, Array[String]]

Returns:

  • (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

Parameters:

  • raw (bool, String)

Returns:

  • (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_mapsHash[String, Array[String]]

Returns:

  • (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.

Parameters:

  • target_path (String)
  • named (Hash[String, Array[String]])
  • undeclared (Hash[String, Array[String]])


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

#runObject

Returns:

  • (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_pathsArray[String]

Returns:

  • (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 == path_to_sidebar ||
      excluded_paths.include?(path_to_wiki)
  end
end

#target_regexpRegexp

Returns:

  • (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_namespaceHash[String, Array[String]]

Returns:

  • (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