picky 3.6.4 → 3.6.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -49,7 +49,7 @@ module Picky
49
49
  end
50
50
 
51
51
  def to_s
52
- categories.join(', ')
52
+ "#{self.class}(#{categories.join(', ')})"
53
53
  end
54
54
 
55
55
  end
@@ -21,18 +21,6 @@ module Picky
21
21
  @category = category
22
22
  end
23
23
 
24
- # Returns the token's text.
25
- #
26
- def text
27
- @text ||= token.text
28
- end
29
-
30
- # Returns the category's bundle.
31
- #
32
- def bundle
33
- @bundle ||= category.bundle_for(token)
34
- end
35
-
36
24
  # Returns the category's name.
37
25
  #
38
26
  def category_name
@@ -44,7 +32,7 @@ module Picky
44
32
  # Note: Caching is most of the time useful.
45
33
  #
46
34
  def weight
47
- @weight ||= bundle.weight(text)
35
+ @weight ||= category.weight(token)
48
36
  end
49
37
 
50
38
  # Returns an array of ids for the given text.
@@ -52,19 +40,21 @@ module Picky
52
40
  # Note: Caching is most of the time useful.
53
41
  #
54
42
  def ids
55
- @ids ||= bundle.ids(text)
43
+ @ids ||= category.ids(token)
56
44
  end
57
45
 
58
46
  # The identifier for this combination.
59
47
  #
60
48
  def identifier
61
- "#{bundle.identifier}:#{token.identifier}"
49
+ "#{category.bundle_for(token).identifier}:#{token.identifier}"
62
50
  end
63
51
 
64
52
  # Note: Required for uniq!
65
53
  #
54
+ # TODO Ok with category or is the bundle needed?
55
+ #
66
56
  def hash
67
- [token.to_s, bundle].hash
57
+ [token.to_s, category].hash
68
58
  end
69
59
 
70
60
  # Combines the category names with the original names.
@@ -81,7 +71,7 @@ module Picky
81
71
  # "exact title:Peter*:peter"
82
72
  #
83
73
  def to_s
84
- "#{bundle.identifier} #{to_result.join(':')}"
74
+ "#{category.bundle_for(token).identifier} #{to_result.join(':')}"
85
75
  end
86
76
 
87
77
  end
@@ -11,21 +11,24 @@ module Picky
11
11
  #
12
12
  class ExactFirst
13
13
 
14
- delegate :similar,
14
+ delegate :add,
15
+ :qualifiers,
16
+ :exact,
17
+ :partial,
18
+ :replace,
19
+
15
20
  :identifier,
16
21
  :name,
17
- :to => :@exact
18
- delegate :index,
22
+
23
+ :index,
19
24
  :category,
20
- :weight,
21
- :generate_partial_from,
22
- :generate_caches_from_memory,
23
- :generate_derived,
24
25
  :dump,
25
26
  :load,
26
- :to => :@partial
27
+
28
+ :to => :@category
27
29
 
28
30
  def initialize category
31
+ @category = category
29
32
  @exact = category.exact
30
33
  @partial = category.partial
31
34
  end
@@ -39,15 +42,36 @@ module Picky
39
42
  end
40
43
  end
41
44
  def self.wrap_each_of categories
42
- categories.categories.collect! { |category| new(category) }
45
+ actual_categories = categories.categories
46
+ categories.clear_categories
47
+
48
+ actual_categories.each do |category|
49
+ categories << new(category)
50
+ end
43
51
  end
44
52
 
45
- def ids text
46
- @exact.ids(text) + @partial.ids(text)
53
+ def ids token
54
+ text = token.text
55
+ if token.partial?
56
+ @exact.ids(text) | @partial.ids(text)
57
+ else
58
+ @exact.ids text
59
+ end
60
+ end
61
+
62
+ def weight token
63
+ text = token.text
64
+ if token.partial?
65
+ [@exact.weight(text), @partial.weight(text)].compact.max
66
+ else
67
+ @exact.weight text
68
+ end
47
69
  end
48
70
 
49
- def weight text
50
- [@exact.weight(text) || 0, @partial.weight(text) || 0].max
71
+ # TODO Refactor! (Subclass Picky::Category?)
72
+ #
73
+ def combination_for token
74
+ weight(token) && Query::Combination.new(token, self)
51
75
  end
52
76
 
53
77
  end
@@ -43,21 +43,30 @@ describe Picky::Wrappers::Category::ExactFirst do
43
43
  @exact.stub! :ids => [1,4,5,6]
44
44
  @partial.stub! :ids => [2,3,7]
45
45
 
46
- @wrapper.ids(:anything).should == [1,4,5,6,2,3,7]
46
+ @wrapper.ids(stub(:token, :text => :anything, :partial? => true)).should == [1,4,5,6,2,3,7]
47
+ end
48
+ it "uses only the exact ids" do
49
+ @exact.stub! :ids => [1,4,5,6]
50
+ @partial.stub! :ids => [2,3,7]
51
+
52
+ @wrapper.ids(stub(:token, :text => :anything, :partial? => false)).should == [1,4,5,6]
47
53
  end
48
54
  end
49
55
 
50
56
  describe 'weight' do
51
57
  context "exact with weight" do
52
58
  before(:each) do
53
- @exact.stub! :weight => 1.23
59
+ @exact.stub! :weight => 0.12
54
60
  end
55
61
  context "partial with weight" do
56
62
  before(:each) do
57
- @partial.stub! :weight => 0.12
63
+ @partial.stub! :weight => 1.23
58
64
  end
59
65
  it "uses the higher weight" do
60
- @wrapper.weight(:anything).should == 1.23
66
+ @wrapper.weight(stub(:token, :text => :anything, :partial? => true)).should == 1.23
67
+ end
68
+ it "uses the exact weight" do
69
+ @wrapper.weight(stub(:token, :text => :anything, :partial? => false)).should == 0.12
61
70
  end
62
71
  end
63
72
  context "partial without weight" do
@@ -65,7 +74,10 @@ describe Picky::Wrappers::Category::ExactFirst do
65
74
  @partial.stub! :weight => nil
66
75
  end
67
76
  it "uses the exact weight" do
68
- @wrapper.weight(:anything).should == 1.23
77
+ @wrapper.weight(stub(:token, :text => :anything, :partial? => true)).should == 0.12
78
+ end
79
+ it "uses the exact weight" do
80
+ @wrapper.weight(stub(:token, :text => :anything, :partial? => false)).should == 0.12
69
81
  end
70
82
  end
71
83
  end
@@ -78,15 +90,21 @@ describe Picky::Wrappers::Category::ExactFirst do
78
90
  @partial.stub! :weight => 0.12
79
91
  end
80
92
  it "uses the partial weight" do
81
- @wrapper.weight(:anything).should == 0.12
93
+ @wrapper.weight(stub(:token, :text => :anything, :partial? => true)).should == 0.12
94
+ end
95
+ it "uses the exact weight" do
96
+ @wrapper.weight(stub(:token, :text => :anything, :partial? => false)).should == nil
82
97
  end
83
98
  end
84
99
  context "partial without weight" do
85
100
  before(:each) do
86
101
  @partial.stub! :weight => nil
87
102
  end
88
- it "is zero" do
89
- @wrapper.weight(:anything).should == 0
103
+ it "is nil" do
104
+ @wrapper.weight(stub(:token, :text => :anything, :partial? => true)).should == nil
105
+ end
106
+ it "is nil" do
107
+ @wrapper.weight(stub(:token, :text => :anything, :partial? => false)).should == nil
90
108
  end
91
109
  end
92
110
  end
@@ -7,7 +7,10 @@ describe Picky::Query::Combination do
7
7
  before(:each) do
8
8
  @bundle = stub :bundle, :identifier => :bundle_name
9
9
  @token = Picky::Query::Token.processed('some_text~', 'Some Original~')
10
- @category = stub :category, :bundle_for => @bundle, :name => :some_category_name
10
+ @category = stub :category,
11
+ :bundle_for => @bundle,
12
+ :name => :some_category_name,
13
+ :identifier => 'some_category_identifier'
11
14
 
12
15
  @combination = described_class.new @token, @category
13
16
  end
@@ -22,7 +25,7 @@ describe Picky::Query::Combination do
22
25
 
23
26
  describe 'hash' do
24
27
  it 'should hash the token and the bundle' do
25
- @combination.hash.should == [@token.to_s, @bundle].hash
28
+ @combination.hash.should == [@token.to_s, @category].hash
26
29
  end
27
30
  end
28
31
 
@@ -52,15 +55,15 @@ describe Picky::Query::Combination do
52
55
 
53
56
  describe 'ids' do
54
57
  it 'should call ids with the text on bundle' do
55
- @bundle.should_receive(:ids).once.with 'some_text'
58
+ @category.should_receive(:ids).once.with @token
56
59
 
57
60
  @combination.ids
58
61
  end
59
62
  it 'should not call it twice, but cache' do
60
- @bundle.stub! :ids => :some_ids
63
+ @category.stub! :ids => :some_ids
61
64
  @combination.ids
62
65
 
63
- @bundle.should_receive(:ids).never
66
+ @category.should_receive(:ids).never
64
67
 
65
68
  @combination.ids.should == :some_ids
66
69
  end
@@ -68,15 +71,15 @@ describe Picky::Query::Combination do
68
71
 
69
72
  describe 'weight' do
70
73
  it 'should call weight with the text on bundle' do
71
- @bundle.should_receive(:weight).once.with 'some_text'
74
+ @category.should_receive(:weight).once.with @token
72
75
 
73
76
  @combination.weight
74
77
  end
75
78
  it 'should not call it twice, but cache' do
76
- @bundle.stub! :weight => :some_weight
79
+ @category.stub! :weight => :some_weight
77
80
  @combination.weight
78
81
 
79
- @bundle.should_receive(:weight).never
82
+ @category.should_receive(:weight).never
80
83
 
81
84
  @combination.weight.should == :some_weight
82
85
  end
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+ #
3
+ require 'spec_helper'
4
+
5
+ describe "Weights" do
6
+
7
+ # This tests the weights option.
8
+ #
9
+ it 'can handle dynamic weights' do
10
+ index = Picky::Index.new :exact_first do
11
+ source { [] }
12
+ category :text, partial: Picky::Partial::Substring.new(from: 1)
13
+ end
14
+
15
+ require 'ostruct'
16
+ exact = OpenStruct.new id: 1, text: "disco"
17
+ partial = OpenStruct.new id: 2, text: "discofox"
18
+ index.add exact
19
+ index.add partial
20
+
21
+ normal = Picky::Search.new index
22
+ normal.search("disco").ids.should == [2, 1] # 2 was added later.
23
+
24
+ index = Picky::Wrappers::Category::ExactFirst.wrap index
25
+
26
+ exact_first = Picky::Search.new index
27
+ exact_first.search("disco").ids.should == [1, 2] # Exact first.
28
+ exact_first.search("disc").ids.should == [2, 1] # Not exact, so not first.
29
+ end
30
+
31
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: picky
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.6.4
4
+ version: 3.6.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-23 00:00:00.000000000 Z
12
+ date: 2011-11-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70330594739240 !ruby/object:Gem::Requirement
16
+ requirement: &70228260909520 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,21 +21,21 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70330594739240
24
+ version_requirements: *70228260909520
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: picky-client
27
- requirement: &70330594738740 !ruby/object:Gem::Requirement
27
+ requirement: &70228260909020 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ~>
31
31
  - !ruby/object:Gem::Version
32
- version: 3.6.4
32
+ version: 3.6.6
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70330594738740
35
+ version_requirements: *70228260909020
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rack
38
- requirement: &70330594738320 !ruby/object:Gem::Requirement
38
+ requirement: &70228260908600 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :runtime
45
45
  prerelease: false
46
- version_requirements: *70330594738320
46
+ version_requirements: *70228260908600
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rack_fast_escape
49
- requirement: &70330594737860 !ruby/object:Gem::Requirement
49
+ requirement: &70228260908140 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :runtime
56
56
  prerelease: false
57
- version_requirements: *70330594737860
57
+ version_requirements: *70228260908140
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: text
60
- requirement: &70330594737440 !ruby/object:Gem::Requirement
60
+ requirement: &70228260907720 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :runtime
67
67
  prerelease: false
68
- version_requirements: *70330594737440
68
+ version_requirements: *70228260907720
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: yajl-ruby
71
- requirement: &70330594737020 !ruby/object:Gem::Requirement
71
+ requirement: &70228260907300 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :runtime
78
78
  prerelease: false
79
- version_requirements: *70330594737020
79
+ version_requirements: *70228260907300
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: activesupport
82
- requirement: &70330594752900 !ruby/object:Gem::Requirement
82
+ requirement: &70228260906800 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ~>
@@ -87,10 +87,10 @@ dependencies:
87
87
  version: '3.0'
88
88
  type: :runtime
89
89
  prerelease: false
90
- version_requirements: *70330594752900
90
+ version_requirements: *70228260906800
91
91
  - !ruby/object:Gem::Dependency
92
92
  name: unicorn
93
- requirement: &70330594752480 !ruby/object:Gem::Requirement
93
+ requirement: &70228260906380 !ruby/object:Gem::Requirement
94
94
  none: false
95
95
  requirements:
96
96
  - - ! '>='
@@ -98,10 +98,10 @@ dependencies:
98
98
  version: '0'
99
99
  type: :runtime
100
100
  prerelease: false
101
- version_requirements: *70330594752480
101
+ version_requirements: *70228260906380
102
102
  - !ruby/object:Gem::Dependency
103
103
  name: sinatra
104
- requirement: &70330594752020 !ruby/object:Gem::Requirement
104
+ requirement: &70228260905920 !ruby/object:Gem::Requirement
105
105
  none: false
106
106
  requirements:
107
107
  - - ! '>='
@@ -109,7 +109,7 @@ dependencies:
109
109
  version: '0'
110
110
  type: :runtime
111
111
  prerelease: false
112
- version_requirements: *70330594752020
112
+ version_requirements: *70228260905920
113
113
  description: Fast Ruby semantic text search engine with comfortable single field interface.
114
114
  email: florian.hanke+picky@gmail.com
115
115
  executables:
@@ -353,6 +353,7 @@ files:
353
353
  - spec/lib/tasks/try_spec.rb
354
354
  - spec/lib/tokenizer_spec.rb
355
355
  - spec/specific/dynamic_weights_spec.rb
356
+ - spec/specific/exact_first_spec.rb
356
357
  - spec/specific/realtime_spec.rb
357
358
  - spec/specific/regression_spec.rb
358
359
  - spec/specific/speed_spec.rb
@@ -482,6 +483,7 @@ test_files:
482
483
  - spec/lib/tasks/try_spec.rb
483
484
  - spec/lib/tokenizer_spec.rb
484
485
  - spec/specific/dynamic_weights_spec.rb
486
+ - spec/specific/exact_first_spec.rb
485
487
  - spec/specific/realtime_spec.rb
486
488
  - spec/specific/regression_spec.rb
487
489
  - spec/specific/speed_spec.rb