Class: Gromit::Reindexer

Inherits:
Object
  • Object
show all
Defined in:
lib/gromit/reindexer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Reindexer

Returns a new instance of Reindexer.



41
42
43
# File 'lib/gromit/reindexer.rb', line 41

def initialize
  @redis ||= Gromit::MarkdownParser.redis
end

Instance Attribute Details

#redis ⇒ Object

Returns the value of attribute redis.



9
10
11
# File 'lib/gromit/reindexer.rb', line 9

def redis
  @redis
end

Class Method Details

.invoke ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/gromit/reindexer.rb', line 12

def invoke
  options = { drop: false }
  OptionParser.new do |opts|
    opts.banner = "Usage: reindexer.rb [options]"

    opts.on('-s', '--source SOURCE_DIR', 'Source directory') do |source_dir|
      options[:source_dir] = source_dir
    end

    opts.on('-d', '--drop', 'Drop and create index before reindexing') do
      options[:drop] = true
    end

  end.parse!

  # Validate the presence of source and destination directories
  unless options[:source_dir]
    puts "Error: source directory (-s or --source) must be specified."
    exit 1
  end

  # Instantiate the ToMkDocs class and perform the conversion
  reindexer = Gromit::Reindexer.new
  reindexer.run(options[:source_dir], drop: options[:drop])

  puts "Reindexer completed successfully."
end

Instance Method Details

#run(directory = nil, drop: false) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/gromit/reindexer.rb', line 45

def run(directory = nil, drop: false)
  gromit = Gromit::Search.new

  if drop
    gromit.recreate_index
  end

  #TODO do we really want david's examples as the default here?
  directory ||= ENV.fetch("DOCS_DIRECTORY") { "/Users/david/development/docs/examples" }
  sections = Gromit::MarkdownParser.process(directory)
  sections.each do |section|
    puts "indexing: #{section[:file]} section: #{section[:section_title]}"
    data = section.transform_keys(&:to_s)
    id = data['id']
    gromit.redis.json_set("item:#{id}", Rejson::Path.root_path, data)
  end
end