Permalink
Browse files

+ exact first -> Results::ExactFirst, + TODOs

  • Loading branch information...
1 parent c4987cb commit 33e4acbe129e87cef441099c4bf252366075153d @floere committed Dec 5, 2011
@@ -148,7 +148,6 @@ def self.load_framework_internals
# Wrappers.
#
load_relative 'wrappers/category/location'
- load_relative 'wrappers/category/exact_first'
load_relative 'wrappers/bundle/delegators'
load_relative 'wrappers/bundle/wrapper'
@@ -238,6 +237,7 @@ def self.load_user_interface
# Results.
#
load_relative 'results'
+ load_relative 'results/exact_first'
# Search.
#
@@ -0,0 +1,53 @@
+# encoding: utf-8
+#
+module Picky
+
+ class Results
+
+ # This index combines an exact and partial index.
+ # It serves to order the results such that exact hits are found first.
+ #
+ module ExactFirst
+
+ # Installs the exact first on the given category
+ # or on the categories of the index, if an index is given.
+ #
+ # THINK Can we unextend in the case it is an index?
+ #
+ def self.extended index_or_category
+ if index_or_category.respond_to? :categories
+ extend_each_of index_or_category.categories
+ index_or_category
+ end
+ end
+ def self.extend_each_of categories
+ categories.categories.each { |category| category.extend self }
+ end
+
+ # Overrides the original method.
+ #
+ def ids token
+ text = token.text
+ if token.partial?
+ exact.ids(text) | partial.ids(text)
+ else
+ exact.ids text
+ end
+ end
+
+ # Overrides the original method.
+ #
+ def weight token
+ text = token.text
+ if token.partial?
+ [exact.weight(text), partial.weight(text)].compact.max
+ else
+ exact.weight text
+ end
+ end
+
+ end
+
+ end
+
+end
@@ -1,94 +0,0 @@
-# encoding: utf-8
-#
-module Picky
-
- # Rename Picky::ExactFirst?
- #
- module Wrappers
-
- module Category
-
- # This index combines an exact and partial index.
- # It serves to order the results such that exact hits are found first.
- #
- class ExactFirst
-
- # TODO Refactor! (Subclass Picky::Category? Or better yet, extend Picky::Category?)
- #
- delegate :add,
- :qualifiers,
- :exact,
- :partial,
- :replace,
-
- :identifier,
- :name,
-
- :index,
- :category,
- :dump,
- :load,
-
- :empty,
- :prepared_index_file,
- :tokenizer,
- :cache,
- :from,
-
- :bundle_for,
- :build_realtime_mapping,
-
- :to => :@category
-
- def initialize category
- @category = category
- @exact = category.exact
- @partial = category.partial
- end
-
- def self.wrap index_or_category
- if index_or_category.respond_to? :categories
- wrap_each_of index_or_category.categories
- index_or_category
- else
- new index_or_category
- end
- end
- def self.wrap_each_of categories
- actual_categories = categories.categories
- categories.clear_categories
-
- actual_categories.each do |category|
- categories << new(category)
- end
- end
-
- def ids token
- text = token.text
- if token.partial?
- @exact.ids(text) | @partial.ids(text)
- else
- @exact.ids text
- end
- end
-
- def weight token
- text = token.text
- if token.partial?
- [@exact.weight(text), @partial.weight(text)].compact.max
- else
- @exact.weight text
- end
- end
-
- def combination_for token
- weight(token) && Query::Combination.new(token, self)
- end
-
- end
-
- end
-
- end
-
-end
@@ -93,7 +93,4 @@ def initialize id, title, author
instance_eval &its
end
- # TODO dump/load backend.
- #
-
end
@@ -57,7 +57,7 @@ def initialize id, title, author
data.add Book.new(2, 'title', 'author')
data.add Book.new(3, 'title', 'author')
- books.search('title').ids.should == ['3', '2', '1'] # TODO Should be ['3', '2', '1']
+ books.search('title').ids.should == ['3', '2', '1']
data.remove '1'
@@ -23,7 +23,27 @@
normal = Picky::Search.new index
normal.search("disco").ids.should == [2, 1] # 2 was added later.
- index = Picky::Wrappers::Category::ExactFirst.wrap index
+ index.extend Picky::Results::ExactFirst
+
+ exact_first = Picky::Search.new index
+ exact_first.search("disco").ids.should == [1, 2] # Exact first.
+ exact_first.search("disc").ids.should == [2, 1] # Not exact, so not first.
+ end
+
+ it 'handles extending single categories' do
+ index = Picky::Index.new :exact_first
+ category = index.category :text, partial: Picky::Partial::Substring.new(from: 1)
+
+ require 'ostruct'
+ exact = OpenStruct.new id: 1, text: "disco"
+ partial = OpenStruct.new id: 2, text: "discofox"
+ index.add exact
+ index.add partial
+
+ normal = Picky::Search.new index
+ normal.search("disco").ids.should == [2, 1] # 2 was added later.
+
+ category.extend Picky::Results::ExactFirst
exact_first = Picky::Search.new index
exact_first.search("disco").ids.should == [1, 2] # Exact first.
@@ -45,7 +65,7 @@
normal.search("disco").ids.should == [1, 2] # Ordering with which it was added.
- data = Picky::Wrappers::Category::ExactFirst.wrap data
+ data.extend Picky::Results::ExactFirst
exact_first = Picky::Search.new data
Picky::Indexes.index_for_tests
@@ -2,35 +2,35 @@
require 'spec_helper'
describe Picky::Cores do
-
+
describe ".forked" do
context 'without fork' do
before(:each) do
Picky::Cores.stub! :fork? => false
end
it 'should not fork' do
Process.should_receive(:fork).never
-
+
described_class.forked([1,2]) do |element|
-
+
end
end
it 'should yield the two elements' do
result = []
-
+
described_class.forked([1,2]) do |element|
result << element
end
-
+
result.should == [1,2]
end
it 'should yield the two elements' do
result = []
-
+
described_class.forked([1,2], randomly: true) do |element|
result << element
end
-
+
# This test remains like this because I
# like the stupidity of it.
#
@@ -49,21 +49,21 @@
context "with array" do
context "with block" do
it "runs ok" do
- # TODO Problematic test. Should not raise the first time
+ # THINK Problematic test? Should it not raise the first time?
#
Process.should_receive(:wait).once.and_raise Errno::ECHILD.new
-
+
described_class.forked([1, 2]) do |e|
end
end
it "yields the elements" do
result = []
-
+
described_class.forked([1, 2]) do |e|
result << e
end
-
+
result.should == [1, 2]
end
it 'should not fork with amount option' do
@@ -105,7 +105,7 @@
end
end
end
-
+
describe 'fork?' do
context 'with forking capabilities' do
before(:all) do
@@ -122,7 +122,7 @@
end
end
end
-
+
describe 'number_of_cores' do
before(:each) do
@linux = mock(:linux).as_null_object
@@ -146,17 +146,17 @@
end
it 'should return whatever darwin returns' do
@darwin.stub! :call => '1234'
-
+
described_class.number_of_cores.should == 1234
end
it 'should call darwin' do
@darwin.should_receive(:call).once
-
+
described_class.number_of_cores
end
it 'should not call linux' do
@linux.should_receive(:call).never
-
+
described_class.number_of_cores
end
end
@@ -166,20 +166,20 @@
end
it 'should return whatever linux returns' do
@linux.stub! :call => '1234'
-
+
described_class.number_of_cores.should == 1234
end
it 'should call linux' do
@linux.should_receive(:call).once
-
+
described_class.number_of_cores
end
it 'should not call darwin' do
@darwin.should_receive(:call).never
-
+
described_class.number_of_cores
end
end
end
-
+
end
Oops, something went wrong.

0 comments on commit 33e4acb

Please sign in to comment.