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)


59
60
61
# File 'lib/xapit/config.rb', line 59

def breadcrumb_facets?
  @options[:breadcrumb_facets]
end

.databaseObject

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



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

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

.default_optionsObject



24
25
26
27
28
29
30
31
# File 'lib/xapit/config.rb', line 24

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

.indexerObject



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

def indexer
  @options[:indexer]
end

.pathObject

The configured path to the database.



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

def path
  @options[:database_path]
end

.query_parserObject



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

def query_parser
  @options[:query_parser]
end

.remove_databaseObject

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



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

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

Setup configuration options. The following options are supported.

:database_path: Where the database is stored. :stemming: The language to use for stemming, defaults to “english”. :spelling: True or false to enable/disable spelling, defaults to true. :indexer: Class to handle the indexing, defaults to SimpleIndexer. :query_parser: Class to handle the parsing, defaults to ClassicQueryParser. :breadcrumb_facets: Use breadcrumb mode for applied facets. See Collection#applied_facet_options for details.



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

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)


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

def setup?
  @options
end

.spelling?Boolean

Returns:

  • (Boolean)


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

def spelling?
  @options[:spelling]
end

.stemmingObject



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

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.



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

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