Class: Spreen::Wiki::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)


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

Returns the value of attribute base_path.

Returns:

  • (Object)


63
64
65
# File 'lib/spreen/wiki/application.rb', line 63

def base_path
  @base_path
end

#configObject (readonly)

Returns the value of attribute config.

Returns:

  • (Object)


63
64
65
# File 'lib/spreen/wiki/application.rb', line 63

def config
  @config
end

#excluded_pathsObject (readonly)

Returns the value of attribute excluded_paths.

Returns:

  • (Object)


63
64
65
# File 'lib/spreen/wiki/application.rb', line 63

def excluded_paths
  @excluded_paths
end

#group_byObject (readonly)

Returns the value of attribute group_by.

Returns:

  • (Object)


63
64
65
# File 'lib/spreen/wiki/application.rb', line 63

def group_by
  @group_by
end

#home_overflowObject (readonly)

Returns the value of attribute home_overflow.

Returns:

  • (Object)


63
64
65
# File 'lib/spreen/wiki/application.rb', line 63

def home_overflow
  @home_overflow
end

#languageObject (readonly)

Returns the value of attribute language.

Returns:

  • (Object)


63
64
65
# File 'lib/spreen/wiki/application.rb', line 63

def language
  @language
end

#path_to_homeObject (readonly)

Returns the value of attribute path_to_home.

Returns:

  • (Object)


63
64
65
# File 'lib/spreen/wiki/application.rb', line 63

def path_to_home
  @path_to_home
end

#path_to_sidebarObject (readonly)

Returns the value of attribute path_to_sidebar.

Returns:

  • (Object)


63
64
65
# File 'lib/spreen/wiki/application.rb', line 63

def path_to_sidebar
  @path_to_sidebar
end

#paths_to_wikisObject (readonly)

Returns the value of attribute paths_to_wikis.

Returns:

  • (Object)


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

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)


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

Parameters:

  • target_path (String)

Returns:

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

Returns:

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

Returns:

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

Parameters:

  • raw (bool, String)

Returns:

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

Returns:

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

Parameters:

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


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

#runObject

Returns:

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

Returns:

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

#target_regexpRegexp

Returns:

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

Returns:

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