Class: Xapit::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/xapit/config.rb

Overview

Singleton class for storing Xapit configuration settings. Currently this only includes the database path.

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/xapit/config.rb', line 5

def options
  @options
end

Class Method Details

Returns:

  • (Boolean)


51
52
53
# File 'lib/xapit/config.rb', line 51

def breadcrumb_facets?
  @options[:breadcrumb_facets]
end

.close_databaseObject

Clear the current database from memory. Unfortunately this is a hack because Xapian doesn’t provide a “close” method on the database. We just have to hope no other references are lying around.



77
78
79
80
81
# File 'lib/xapit/config.rb', line 77

def close_database
  @database = nil
  @writable_database = nil
  GC.start
end

.databaseObject

Fetch Xapian::Database object at configured path. Database is stored in memory.



56
57
58
# File 'lib/xapit/config.rb', line 56

def database
  @writable_database || (@database ||= Xapian::Database.new(path))
end

.default_optionsObject



16
17
18
19
20
21
22
23
# File 'lib/xapit/config.rb', line 16

def default_options
  {
    :indexer => SimpleIndexer,
    :query_parser => ClassicQueryParser,
    :spelling => true,
    :stemming => "english"
  }
end

.indexerObject



39
40
41
# File 'lib/xapit/config.rb', line 39

def indexer
  @options[:indexer]
end

.pathObject

The configured path to the database.



31
32
33
# File 'lib/xapit/config.rb', line 31

def path
  @options[:database_path]
end

.query_parserObject



35
36
37
# File 'lib/xapit/config.rb', line 35

def query_parser
  @options[:query_parser]
end

.remove_databaseObject

Removes the configured database file and clears the stored one in memory.



68
69
70
71
72
# File 'lib/xapit/config.rb', line 68

def remove_database # this can be a bit dangers, maybe do some checking here first?
  FileUtils.rm_rf(path) if File.exist? path
  @database = nil
  @writable_database = nil
end

.setup(options = {}) ⇒ Object

See Xapit#setup



8
9
10
11
12
13
14
# File 'lib/xapit/config.rb', line 8

def setup(options = {})
  if @options && options[:database_path] != @options[:database_path]
    @database = nil
    @writable_database = nil
  end
  @options = options.reverse_merge(default_options)
end

.setup?Boolean

See if setup options are already set.

Returns:

  • (Boolean)


26
27
28
# File 'lib/xapit/config.rb', line 26

def setup?
  @options
end

.spelling?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/xapit/config.rb', line 43

def spelling?
  @options[:spelling]
end

.stemmingObject



47
48
49
# File 'lib/xapit/config.rb', line 47

def stemming
  @options[:stemming]
end

.writable_databaseObject

Fetch Xapian::WritableDatabase object at configured path. Database is stored in memory. Creates the database directory if needed.



62
63
64
65
# File 'lib/xapit/config.rb', line 62

def writable_database
  FileUtils.mkdir_p(File.dirname(path)) unless File.exist?(File.dirname(path))
  @writable_database ||= Xapian::WritableDatabase.new(path, Xapian::DB_CREATE_OR_OPEN)
end