Skip to content

Commit

Permalink
Load the default settings and set the path
Browse files Browse the repository at this point in the history
  • Loading branch information
eddloschi committed Nov 13, 2013
1 parent 93ec5df commit 26c36d0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 18 deletions.
9 changes: 7 additions & 2 deletions lib/mongoid/giza/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class Configuration < Riddle::Configuration
# Creates the configuration instance
def initialize
super
source = Riddle::Configuration::XMLSource.new("source", :xmlpipe2)
@index = Riddle::Configuration::Index.new("index", source)
source = Riddle::Configuration::XMLSource.new(:source, :xmlpipe2)
@index = Riddle::Configuration::Index.new(:index, source)
end
##
# Loads a YAML file with settings defined.
Expand All @@ -37,10 +37,15 @@ def load(path)
def add_index(index)
source = Riddle::Configuration::XMLSource.new(index.name, :xmlpipe2)
riddle_index = Riddle::Configuration::Index.new(index.name, source)
Riddle::Configuration::Index.settings.each do |setting|
value = @index.send("#{setting}")
riddle_index.send("#{setting}=", value)
end
index.settings.each do |setting, value|
method = "#{setting}="
riddle_index.send(method, value) if riddle_index.respond_to?(method)
end
riddle_index.path = File.join(riddle_index.path, index.name.to_s)
@indices << riddle_index
end
end
Expand Down
60 changes: 44 additions & 16 deletions spec/mongoid/giza/configuration_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
require "spec_helper"

describe Mongoid::Giza::Configuration do
let(:config) { Mongoid::Giza::Configuration.instance }
before do
source = double("source")
@default_index = double("default_index")
allow(Riddle::Configuration::Source).to receive(:new).with(:source, :xmlpipe2) { source }
allow(Riddle::Configuration::Index).to receive(:new).with(:index, source) { @default_index }
@config = Mongoid::Giza::Configuration.send(:new)
end

describe "initialize" do
it "should create a Riddle::Configuration::Index for default settings" do
expect(config.instance_variable_get("@index")).to be_a_kind_of(Riddle::Configuration::Index)
expect(@config.instance_variable_get("@index")).to be(@default_index)
end
end

Expand All @@ -17,27 +23,27 @@
it "should load the configuration file" do
expect(file).to receive(:read) { "searchd:\n address: localhost" }
expect(File).to receive(:open).with("giza.yml") { file }
config.load("giza.yml")
@config.load("giza.yml")
end

it "should set settings" do
allow(file).to receive(:read) { "searchd:\n address: localhost" }
file_open
config.load("giza.yml")
expect(config.searchd.address).to eql("localhost")
@config.load("giza.yml")
expect(@config.searchd.address).to eql("localhost")
end

it "should ignore non-existent sections" do
allow(file).to receive(:read) { "miss_section:\n address: localhost" }
file_open
expect { config.load("giza.yml") }.not_to raise_error
expect { @config.load("giza.yml") }.not_to raise_error
end

it "should ignore non-existent settings" do
allow(file).to receive(:read) { "searchd:\n miss_setting: false" }
expect(config.searchd).not_to receive(:method_missing).with(:miss_setting=, false)
expect(@config.searchd).not_to receive(:method_missing).with(:miss_setting=, false)
file_open
config.load("giza.yml")
@config.load("giza.yml")
end
end

Expand All @@ -49,25 +55,47 @@
index
end

let(:riddle_index) { double("riddle_index") }
let(:riddle_index) { double("riddle_index").as_null_object }

let(:source) { double("source").as_null_object }

before do
allow(Riddle::Configuration::Index).to receive(:settings) { [] }
allow(Riddle::Configuration::XMLSource).to receive(:new) { source }
allow(Riddle::Configuration::Index).to receive(:new).with(index.name, source) { riddle_index }
end

it "should add an Riddle::Configuration::Index" do
allow(Riddle::Configuration::XMLSource).to receive(:new) { double("source").as_null_object }
allow(Riddle::Configuration::Index).to receive(:new) { double("riddle_index").as_null_object }
expect { config.add_index(index) }.to change{config.indices.length}.by(1)
expect { @config.add_index(index) }.to change{@config.indices.length}.by(1)
end

it "should create a xmlpipe2 source with the same name of the index" do
expect(Riddle::Configuration::XMLSource).to receive(:new).with(index.name, :xmlpipe2) { double("source").as_null_object }
expect(Riddle::Configuration::XMLSource).to receive(:new).with(index.name, :xmlpipe2) { source }
allow(Riddle::Configuration::Index).to receive(:new) { double("riddle_index").as_null_object }
config.add_index(index)
@config.add_index(index)
end

it "should load the default settings" do
allow(Riddle::Configuration::Index).to receive(:settings) { [:html_strip] }
allow(@default_index).to receive(:html_strip) { 1 }
expect(riddle_index).to receive(:html_strip=).with(1)
@config.add_index(index)
end

it "should apply the settings defined" do
allow(Riddle::Configuration::Index).to receive(:new) { riddle_index }
allow(index).to receive(:settings) { {html_strip: 1} }
expect(riddle_index).to receive("html_strip=").with(1)
config.add_index(index)
expect(riddle_index).to receive(:html_strip=).with(1)
@config.add_index(index)
end

it "should set the path" do
allow(Riddle::Configuration::Index).to receive(:settings) { [:path] }
allow(@default_index).to receive(:path) { "/path/to/index" }
allow(riddle_index).to receive(:path=).with("/path/to/index")
allow(riddle_index).to receive(:path) { "/path/to/index" }
expect(riddle_index).to receive(:path=).with("/path/to/index/#{index.name}")
@config.add_index(index)
end
end
end

0 comments on commit 26c36d0

Please sign in to comment.