seasy 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/seasy/index.rb +27 -27
- data/seasy.gemspec +1 -1
- data/spec/index_spec.rb +25 -24
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.9
|
data/lib/seasy/index.rb
CHANGED
@@ -2,24 +2,24 @@ require 'singleton'
|
|
2
2
|
|
3
3
|
module Seasy
|
4
4
|
|
5
|
-
class Configuration
|
5
|
+
class Configuration
|
6
6
|
include Singleton
|
7
|
-
|
7
|
+
|
8
8
|
attr_accessor :storage
|
9
|
-
|
9
|
+
|
10
10
|
def initialize
|
11
11
|
@storage = Seasy::InMemoryStorage
|
12
12
|
end
|
13
13
|
end
|
14
|
-
|
14
|
+
|
15
15
|
def configure
|
16
16
|
config = Seasy::Configuration.instance
|
17
17
|
yield config
|
18
18
|
end
|
19
|
-
|
19
|
+
|
20
20
|
class Index
|
21
21
|
attr_accessor :name
|
22
|
-
|
22
|
+
|
23
23
|
def initialize name = 'default'
|
24
24
|
@name = name
|
25
25
|
@storage = Configuration.instance.storage.new
|
@@ -29,7 +29,7 @@ module Seasy
|
|
29
29
|
def Index::default
|
30
30
|
@@defaultee = Index.new if not defined? @@defaultee
|
31
31
|
end
|
32
|
-
|
32
|
+
|
33
33
|
def Index::with_name name
|
34
34
|
stringed_name = name.to_s
|
35
35
|
@@indices = {} if not defined? @@indices
|
@@ -38,58 +38,58 @@ module Seasy
|
|
38
38
|
end
|
39
39
|
@@indices[stringed_name]
|
40
40
|
end
|
41
|
-
|
41
|
+
|
42
42
|
def add searchee, target, options = {}
|
43
|
-
options[:source] = target if options[:source].nil?
|
43
|
+
options[:source] = target if options[:source].nil?
|
44
44
|
save target, fragmentize( searchee ), options
|
45
45
|
end
|
46
46
|
|
47
47
|
def search query
|
48
|
-
@storage.search query
|
48
|
+
@storage.search query.downcase
|
49
49
|
end
|
50
|
-
|
51
|
-
def clear
|
50
|
+
|
51
|
+
def clear
|
52
52
|
@storage.clear
|
53
53
|
end
|
54
|
-
|
54
|
+
|
55
55
|
def remove target
|
56
56
|
@storage.remove target
|
57
57
|
end
|
58
|
-
|
58
|
+
|
59
59
|
private
|
60
|
-
|
60
|
+
|
61
61
|
def fragmentize searchee
|
62
62
|
f = Fragmentizer.new
|
63
63
|
f.fragmentize searchee
|
64
64
|
end
|
65
|
-
|
65
|
+
|
66
66
|
def save target, weights, options
|
67
67
|
@storage.save target, weights, options
|
68
68
|
end
|
69
|
-
|
69
|
+
|
70
70
|
end
|
71
71
|
|
72
|
-
# a store got search queries as keys and an array of
|
72
|
+
# a store got search queries as keys and an array of
|
73
73
|
# target-weight tuples as values
|
74
74
|
class InMemoryStorage
|
75
|
-
def initialize
|
75
|
+
def initialize
|
76
76
|
@store = {}
|
77
77
|
@sources = {}
|
78
78
|
end
|
79
|
-
|
79
|
+
|
80
80
|
# target is a simple value - we care not what
|
81
|
-
# weights are all fragments (indices) and their weight
|
81
|
+
# weights are all fragments (indices) and their weight
|
82
82
|
# eg. { "aba" => 1, "ab" => 1, "ba" => 1, "b" => 1, "a" => 2 } for the string "aba"
|
83
83
|
def save target, weights, options = {}
|
84
84
|
raise ":source need to be set" if options[:source].nil?
|
85
85
|
source = options[:source]
|
86
|
-
@sources[source] ||= []
|
86
|
+
@sources[source] ||= []
|
87
87
|
@sources[source] << target
|
88
88
|
weights.keys.each do |key|
|
89
89
|
add weights[key], key, target
|
90
90
|
end
|
91
91
|
end
|
92
|
-
|
92
|
+
|
93
93
|
def add weight, key, target
|
94
94
|
if @store[key].nil?
|
95
95
|
@store[key] = {target => weight}
|
@@ -99,22 +99,22 @@ module Seasy
|
|
99
99
|
@store[key][target] += weight
|
100
100
|
end
|
101
101
|
end
|
102
|
-
|
102
|
+
|
103
103
|
# return { target1 => weight, target2 => weight }
|
104
104
|
def search query
|
105
105
|
@store[query] || {}
|
106
106
|
end
|
107
|
-
|
107
|
+
|
108
108
|
def clear
|
109
109
|
@store = {}
|
110
110
|
@sources = {}
|
111
111
|
end
|
112
|
-
|
112
|
+
|
113
113
|
def remove source
|
114
114
|
targets = @sources[source]
|
115
115
|
@store.delete_if {|key,value| !value[targets.first].nil?}
|
116
116
|
end
|
117
|
-
|
117
|
+
|
118
118
|
end
|
119
119
|
|
120
120
|
module_function :configure
|
data/seasy.gemspec
CHANGED
data/spec/index_spec.rb
CHANGED
@@ -12,7 +12,7 @@ describe Index do
|
|
12
12
|
config.storage = InMemoryStorage
|
13
13
|
end
|
14
14
|
end
|
15
|
-
|
15
|
+
|
16
16
|
it "should default and have some basic behaviour" do
|
17
17
|
i = subject
|
18
18
|
target_one = "1"
|
@@ -21,9 +21,10 @@ describe Index do
|
|
21
21
|
i.search( "red" ).should == {target_one => 1}
|
22
22
|
i.add 'red red wine', target_two
|
23
23
|
i.search( "red").should == {target_two => 2 ,target_one => 1}
|
24
|
+
i.search( "RED").should == {target_two => 2 ,target_one => 1}
|
24
25
|
i.search( "e" ).should == {target_one => 3, target_two => 3}
|
25
26
|
end
|
26
|
-
|
27
|
+
|
27
28
|
it "should be possible to add complex strings twice" do
|
28
29
|
i = subject
|
29
30
|
target = 1
|
@@ -34,31 +35,31 @@ describe Index do
|
|
34
35
|
i.search( 'lu' ).should == {target => 3}
|
35
36
|
i.search( 'f' ).should == {target => 8}
|
36
37
|
end
|
37
|
-
|
38
|
+
|
38
39
|
it "should have named indices" do
|
39
40
|
one_index = Index.with_name 42
|
40
41
|
another_index = Index.with_name 66
|
41
|
-
|
42
|
+
|
42
43
|
one_index.name.should == '42'
|
43
44
|
another_index.name.should == '66'
|
44
|
-
|
45
|
+
|
45
46
|
one_index.add 'meaning', 'universe'
|
46
47
|
another_index.add 'evilness', 'hell'
|
47
|
-
|
48
|
+
|
48
49
|
one_index.search( 'vil' ).should == { }
|
49
50
|
another_index.search( 'vil' ).should == { 'hell' => 1 }
|
50
|
-
|
51
|
+
|
51
52
|
one_index.search( 'ean' ).should == {'universe' => 1}
|
52
|
-
another_index.search( 'ean' ).should == { }
|
53
|
+
another_index.search( 'ean' ).should == { }
|
53
54
|
end
|
54
|
-
|
55
|
+
|
55
56
|
it "should handle source also" do
|
56
57
|
i = subject
|
57
58
|
i.add 'landsnora', 'edsberg'
|
58
59
|
i.add 'landsnora', 'edsberg', :source => 'sollentuna'
|
59
60
|
i.search( 'landsnora' ).should == {'edsberg' => 2}
|
60
61
|
end
|
61
|
-
|
62
|
+
|
62
63
|
it "should remove targets" do
|
63
64
|
i = subject
|
64
65
|
i.add 'searchentry', 'gooo', :source => 'gooo'
|
@@ -75,61 +76,61 @@ describe Index do
|
|
75
76
|
i.search( 'entry' ).should == {}
|
76
77
|
end
|
77
78
|
|
78
|
-
it "should have a configurable storage" do
|
79
|
+
it "should have a configurable storage" do
|
79
80
|
configure do |config|
|
80
81
|
config.storage = DummyStorage
|
81
82
|
end
|
82
|
-
|
83
|
+
|
83
84
|
i = Index.default
|
84
85
|
i.add 'a', 1
|
85
86
|
i.search 'a'
|
86
87
|
DummyStorage.should be_saved_once
|
87
88
|
DummyStorage.should be_searched_once
|
88
89
|
end
|
89
|
-
|
90
|
+
|
90
91
|
it "should set name in init if name= is present" do
|
91
92
|
configure do |config|
|
92
93
|
config.storage = DummyStorage
|
93
94
|
end
|
94
|
-
|
95
|
+
|
95
96
|
i = Index.with_name 'a_name'
|
96
97
|
DummyStorage.should be_name_setter_called
|
97
98
|
end
|
98
|
-
|
99
|
+
|
99
100
|
end
|
100
101
|
|
101
|
-
class DummyStorage
|
102
|
-
|
102
|
+
class DummyStorage
|
103
|
+
|
103
104
|
def initialize
|
104
105
|
@@saved_count = 0
|
105
106
|
@@searched_count = 0
|
106
107
|
@@name_setter_called = false
|
107
108
|
end
|
108
|
-
|
109
|
+
|
109
110
|
def save target, weights, options = {}
|
110
111
|
@@saved_count += 1
|
111
112
|
end
|
112
|
-
|
113
|
+
|
113
114
|
def search query
|
114
115
|
@@searched_count += 1
|
115
116
|
end
|
116
|
-
|
117
|
+
|
117
118
|
def DummyStorage::saved_once?
|
118
119
|
@@saved_count == 1
|
119
120
|
end
|
120
|
-
|
121
|
+
|
121
122
|
def DummyStorage::searched_once?
|
122
123
|
@@searched_count == 1
|
123
124
|
end
|
124
|
-
|
125
|
+
|
125
126
|
def DummyStorage::name_setter_called?
|
126
127
|
@@name_setter_called
|
127
128
|
end
|
128
|
-
|
129
|
+
|
129
130
|
def name= name
|
130
131
|
@@name_setter_called = true
|
131
132
|
end
|
132
|
-
|
133
|
+
|
133
134
|
def clear
|
134
135
|
end
|
135
136
|
end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: seasy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0.
|
5
|
+
version: 0.0.9
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Fredrik Rubensson
|
@@ -93,7 +93,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
93
93
|
requirements:
|
94
94
|
- - ">="
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
hash: -
|
96
|
+
hash: -182271339406525344
|
97
97
|
segments:
|
98
98
|
- 0
|
99
99
|
version: "0"
|