Class: Indexmap::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/indexmap/output.rb

Constant Summary collapse

VALID_FORMATS =
%i[index single_file].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration:) ⇒ Output

Returns a new instance of Output.



9
10
11
# File 'lib/indexmap/output.rb', line 9

def initialize(configuration:)
  @configuration = configuration
end

Instance Attribute Details

#base_urlObject



13
14
15
# File 'lib/indexmap/output.rb', line 13

def base_url
  resolve(@base_url) || configuration.base_url
end

#entriesObject



17
18
19
20
21
# File 'lib/indexmap/output.rb', line 17

def entries
  resolved_entries = resolve(@entries)

  Array(resolved_entries.nil? ? configuration.entries : resolved_entries)
end

#formatObject



23
24
25
26
# File 'lib/indexmap/output.rb', line 23

def format
  value = resolve(@format) || configuration.format
  value.nil? ? :index : value.to_sym
end

#index_filenameObject



28
29
30
# File 'lib/indexmap/output.rb', line 28

def index_filename
  resolve(@index_filename) || configuration.index_filename
end

#public_pathObject



32
33
34
35
# File 'lib/indexmap/output.rb', line 32

def public_path
  value = resolve(@public_path) || configuration.public_path
  Pathname(value)
end

#sectionsObject



37
38
39
40
41
# File 'lib/indexmap/output.rb', line 37

def sections
  resolved_sections = resolve(@sections)

  Array(resolved_sections.nil? ? configuration.sections : resolved_sections)
end

Instance Method Details

#writerObject

Raises:



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/indexmap/output.rb', line 43

def writer
  raise ConfigurationError, "Indexmap base_url is not configured" if base_url.to_s.strip.empty?

  unless VALID_FORMATS.include?(format)
    raise ConfigurationError, "Indexmap format must be one of: #{VALID_FORMATS.join(", ")}"
  end

  if format == :single_file
    raise ConfigurationError, "Indexmap entries are not configured" if entries.empty?
  elsif sections.empty?
    raise ConfigurationError, "Indexmap sections are not configured" if sections.empty?
  end

  Writer.new(
    entries: entries,
    format: format,
    sections: sections,
    public_path: public_path,
    base_url: base_url,
    index_filename: index_filename
  )
end