Class: Dnif::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/dnif/configuration.rb

Class Method Summary collapse

Class Method Details

.generate(config_path) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/dnif/configuration.rb', line 7

def self.generate(config_path)
  template = ERB.new(File.read(config_path))
  output = template.result(binding)

  # TODO turn "db/sphinx" and "config/sphinx" configurable
  FileUtils.mkdir_p(File.join(Dnif.root_path, "db", "sphinx", Dnif.environment))
  path = File.join(Dnif.root_path, "config", "sphinx", Dnif.environment + ".conf")
  File.open(path, "w") do |f|
    f.puts output
  end
  path
end

.options_for(heading, path) ⇒ Object

Code extracted from Ultrasphinx Configuration file parser.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/dnif/configuration.rb', line 29

def self.options_for(heading, path)
  # Evaluate ERB
  template = ERB.new(File.open(path) {|f| f.read})
  contents = template.result(binding)

  # Find the correct heading.
  section = contents[/^#{heading.gsub('/', '__')}\s*?\{(.*?)\}/m, 1]

  if section
    # Strip comments and leading/trailing whitespace
    section.gsub!(/^\s*(.*?)\s*(?:#.*)?$/, '\1')

    # Convert to a hash
    {}.tap do |options|
      lines = section.split(/\n+/)
      while line = lines.shift
        if line =~ /(.*?)\s*=\s*(.*)/
          key, value = $1, [$2]
          value << (line = lines.shift) while line =~ /\\$/
          options[key] = value.join("\n    ")
        end
      end
    end
  else
    # XXX Is it safe to raise here?
    puts "warning; heading #{heading} not found in #{path}; it may be corrupted. "
    {}    
  end    
end

.sourcesObject



20
21
22
23
24
25
# File 'lib/dnif/configuration.rb', line 20

def self.sources
  ActiveRecord::Base.indexes.keys.each do |class_name|
    name = class_name.underscore.pluralize + "_main"
    yield name, class_name
  end
end