Class: WaxTasks::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/wax_tasks/index.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Index

Returns a new instance of Index.


8
9
10
11
12
13
14
15
16
17
# File 'lib/wax_tasks/index.rb', line 8

def initialize(config)
  @config      = config
  @collections = config.dig 'collections'
  @path        = config.dig 'index'

  raise WaxTasks::Error::NoSearchCollections if @collections.nil?
  raise WaxTasks::Error::InvalidConfig if @path.nil?

  @records = records
end

Instance Attribute Details

#collectionsObject (readonly)

Returns the value of attribute collections


6
7
8
# File 'lib/wax_tasks/index.rb', line 6

def collections
  @collections
end

#pathObject (readonly)

Returns the value of attribute path


6
7
8
# File 'lib/wax_tasks/index.rb', line 6

def path
  @path
end

Instance Method Details

#recordsObject


21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/wax_tasks/index.rb', line 21

def records
  lunr_id = 0
  @collections.flat_map do |collection|
    collection.records_from_pages.each.flat_map do |r|
      r.keep_only collection.search_fields
      r.set 'lunr_id', lunr_id
      r.lunr_normalize_values
      lunr_id += 1
      r
    end
  end
end

#write_to(dir) ⇒ Object


36
37
38
39
40
41
42
43
# File 'lib/wax_tasks/index.rb', line 36

def write_to(dir)
  file_path = WaxTasks::Utils.safe_join dir, @path
  FileUtils.mkdir_p File.dirname(file_path)
  File.open(file_path, 'w') do |f|
    f.puts "---\nlayout: none\n---\n"
    f.puts JSON.pretty_generate(@records.map(&:hash))
  end
end