Skip to content

Commit

Permalink
+ extracted source setup into a separate helper module - trying to fr…
Browse files Browse the repository at this point in the history
…ee the category from unnecessary methods
  • Loading branch information
floere committed Sep 12, 2012
1 parent ebcfc6d commit 3254f36
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 122 deletions.
8 changes: 4 additions & 4 deletions server/APIs.textile
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,29 @@ Accepted by

* @Picky::Index#backend(backend_instance)@

h2. Sources (Picky::API::Source)
h2. Sources

Has methods

* @each(&block)@ yields @objects@ (that respond to methods listed in the categories, or in the categories' @from@ option)

h2. Weight (Picky::API::Category::Weight)
h2. Weight

Has methods

* @weight_for(amount_of_ids)@ => @float@ (a weight)

Accepted as option @weight@ for the @category@.

h2. Partial (Picky::API::Category::Partial)
h2. Partial

Has methods

* @each_partial(token, &block)@ yields each @partialized token@

Accepted as option @partial@ for the @category@.

h2. Similarity (Picky::API::Category::Similarity)
h2. Similarity

Has methods

Expand Down
35 changes: 0 additions & 35 deletions server/lib/picky/api/source.rb

This file was deleted.

3 changes: 1 addition & 2 deletions server/lib/picky/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module Picky
class Category

include API::Tokenizer
include API::Source

attr_accessor :exact,
:partial
Expand Down Expand Up @@ -44,7 +43,7 @@ def initialize name, index, options = {}
def configure_from options
# Indexing.
#
@source = extract_source options[:source], nil_ok: true
@source = Generators::Source.from options[:source], true
@from = options[:from]
@tokenizer = extract_tokenizer options[:indexing]

Expand Down
2 changes: 1 addition & 1 deletion server/lib/picky/category_indexing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def retrieve
# If we have no explicit source, we'll check the index for one.
#
def source
(@source = extract_source(@source, nil_ok: true)) || @index.source
(@source = Generators::Source.from(@source, true, @index && @index.name)) || @index.source
end

# Return the key format.
Expand Down
30 changes: 30 additions & 0 deletions server/lib/picky/generators/source.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Picky

module Generators

module Source

# Either a thing responding to #each or a block is fine.
#
def self.from thing, nil_ok, index_name = nil, category_name = nil
if thing.respond_to?(:each) || thing.respond_to?(:call)
thing
else
return if nil_ok

specifics = ""
specifics << index_name.to_s if index_name
specifics = "for #{specifics} " unless specifics.empty?

raise ArgumentError.new(<<-ERROR)
The source #{specifics}should respond to either the method #each or
it can be a lambda/block, returning such a source.
ERROR
end
end

end

end

end
10 changes: 7 additions & 3 deletions server/lib/picky/index_indexing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ module Picky
class Index

include API::Tokenizer
include API::Source

include Helpers::Indexing

# Delegators for indexing.
Expand Down Expand Up @@ -86,7 +84,13 @@ def tokenizer
#
def source some_source = nil, &block
some_source ||= block
some_source ? (@source = extract_source(some_source)) : unblock_source
some_source ? (@source = Generators::Source.from(some_source, false, name)) : unblock_source
end
# Get the actual source if it is wrapped in a time
# capsule, i.e. a block/lambda.
#
def unblock_source
@source.respond_to?(:call) ? @source.call : @source
end

# Define a key_format on the index.
Expand Down
2 changes: 1 addition & 1 deletion server/lib/picky/loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ def load_framework_internals
def load_api
load_relative 'api/tokenizer',
'api/tokenizer/character_substituter',
'api/source',
'api/search/boost'
end

Expand All @@ -215,6 +214,7 @@ def load_generators
load_relative 'generators/weights'
load_relative 'generators/partial'
load_relative 'generators/similarity'
load_relative 'generators/source'
load_relative 'generators/aliases'
end

Expand Down
68 changes: 0 additions & 68 deletions server/spec/lib/api/source_spec.rb

This file was deleted.

4 changes: 0 additions & 4 deletions server/spec/lib/generators/partial_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ def each_partial text
* an object that responds to #each_partial(str_or_sym) and yields each partial
ERROR
end
end
context 'invalid partial' do
it 'raises with a nice error message' do
expect {
partial.from Object.new, 'some_index'
Expand All @@ -45,8 +43,6 @@ def each_partial text
* an object that responds to #each_partial(str_or_sym) and yields each partial
ERROR
end
end
context 'invalid partial' do
it 'raises with a nice error message' do
expect {
partial.from Object.new, 'some_index', 'some_category'
Expand Down
64 changes: 64 additions & 0 deletions server/spec/lib/generators/source_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require 'spec_helper'

describe Picky::Generators::Source do
let(:generator) { described_class } # "class", actually a module.
context 'unblock_source' do
context 'with block' do
it 'unblocks' do
result = generator.from ->() { :some_source }, false

result.call == :some_source
end
end
context 'with #each' do
it 'takes the source directly' do
generator.from([:some_source], true) == :some_source
end
it 'takes the source directly' do
generator.from([:some_source], false) == :some_source
end
end
end
context 'extract_source' do
context 'block with source hash' do
it 'extracts a source' do
generator.from(->(){}, false).should be_kind_of(Proc)
end
end
context 'each source' do
let(:source) do
Class.new do
def each

end
end.new
end
it 'extracts a source' do
generator.from(source, false).should == source
end
end
context 'invalid source with nil not ok' do
it 'raises with a nice error message' do
expect {
generator.from Object.new, false
}.to raise_error(<<-ERROR)
The source should respond to either the method #each or
it can be a lambda/block, returning such a source.
ERROR
end
it 'raises with a nice error message' do
expect {
generator.from Object.new, false, 'some_index'
}.to raise_error(<<-ERROR)
The source for some_index should respond to either the method #each or
it can be a lambda/block, returning such a source.
ERROR
end
end
context 'with nil ok' do
it 'simply returns nil back' do
generator.from(nil, true).should == nil
end
end
end
end
6 changes: 2 additions & 4 deletions server/spec/lib/index_indexing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,14 @@
end
end
context 'with non#each source' do
let(:source) { stub :source, :harvest => nil }

it 'raises' do
the_source = source
the_source = stub :source, :harvest => nil
expect {
described_class.new :some_name do
source the_source
end
}.to raise_error(<<-ERROR)
The some_name source should respond to either the method #each or
The source for some_name should respond to either the method #each or
it can be a lambda/block, returning such a source.
ERROR
end
Expand Down

0 comments on commit 3254f36

Please sign in to comment.