inferx 0.1.6 → 0.1.7
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.
- data/lib/inferx.rb +2 -3
- data/lib/inferx/adapter.rb +15 -4
- data/lib/inferx/categories.rb +2 -0
- data/lib/inferx/category.rb +13 -5
- data/lib/inferx/version.rb +1 -1
- data/spec/inferx/adapter_spec.rb +26 -9
- data/spec/inferx/categories_spec.rb +34 -4
- data/spec/inferx/category_spec.rb +33 -1
- data/spec/inferx_spec.rb +3 -3
- data/spec/spec_helper.rb +1 -0
- metadata +2 -2
data/lib/inferx.rb
CHANGED
@@ -9,10 +9,9 @@ class Inferx
|
|
9
9
|
# {https://github.com/redis/redis-rb redis}
|
10
10
|
#
|
11
11
|
# @option options [String] :namespace namespace of keys to be used to Redis
|
12
|
+
# @option options [Boolean] :manual whether save manually, defaults to false
|
12
13
|
def initialize(options = {})
|
13
|
-
|
14
|
-
redis = Redis.new(options)
|
15
|
-
@categories = Categories.new(redis, namespace)
|
14
|
+
@categories = Categories.new(Redis.new(options), options)
|
16
15
|
end
|
17
16
|
|
18
17
|
attr_reader :categories
|
data/lib/inferx/adapter.rb
CHANGED
@@ -2,10 +2,21 @@ class Inferx
|
|
2
2
|
class Adapter
|
3
3
|
|
4
4
|
# @param [Redis] redis an instance of Redis
|
5
|
-
# @param [
|
6
|
-
|
5
|
+
# @param [Hash] options
|
6
|
+
# @option options [String] :namespace namespace of keys to be used to Redis
|
7
|
+
# @option options [Boolean] :manual whether manual save, defaults to false
|
8
|
+
def initialize(redis, options = {})
|
7
9
|
@redis = redis
|
8
|
-
@
|
10
|
+
@options = options
|
11
|
+
@namespace = options[:namespace]
|
12
|
+
@manual = !!options[:manual]
|
13
|
+
end
|
14
|
+
|
15
|
+
# Determine if manual save.
|
16
|
+
#
|
17
|
+
# @return [Boolean] whether or not manual save
|
18
|
+
def manual?
|
19
|
+
@manual
|
9
20
|
end
|
10
21
|
|
11
22
|
# Get the key for access to categories.
|
@@ -39,7 +50,7 @@ class Inferx
|
|
39
50
|
# @param [Array] args any arguments
|
40
51
|
# @return [Object] a instance of the class
|
41
52
|
def spawn(klass, *args)
|
42
|
-
klass.new(@redis, *args, @
|
53
|
+
klass.new(@redis, *args, @options)
|
43
54
|
end
|
44
55
|
|
45
56
|
protected
|
data/lib/inferx/categories.rb
CHANGED
@@ -28,6 +28,7 @@ class Inferx
|
|
28
28
|
def add(*category_names)
|
29
29
|
@redis.pipelined do
|
30
30
|
category_names.each { |category_name| hsetnx(category_name, 0) }
|
31
|
+
@redis.save unless manual?
|
31
32
|
end
|
32
33
|
end
|
33
34
|
|
@@ -38,6 +39,7 @@ class Inferx
|
|
38
39
|
@redis.pipelined do
|
39
40
|
category_names.each { |category_name| hdel(category_name) }
|
40
41
|
@redis.del(*category_names.map(&method(:make_category_key)))
|
42
|
+
@redis.save unless manual?
|
41
43
|
end
|
42
44
|
end
|
43
45
|
|
data/lib/inferx/category.rb
CHANGED
@@ -5,9 +5,11 @@ class Inferx
|
|
5
5
|
|
6
6
|
# @param [Redis] redis an instance of Redis
|
7
7
|
# @param [Symbol] name a category name
|
8
|
-
# @param [
|
9
|
-
|
10
|
-
|
8
|
+
# @param [Hash] options
|
9
|
+
# @option options [String] :namespace namespace of keys to be used to Redis
|
10
|
+
# @option options [Boolean] :manual whether manual save, defaults to false
|
11
|
+
def initialize(redis, name, options = {})
|
12
|
+
super(redis, options)
|
11
13
|
@name = name
|
12
14
|
end
|
13
15
|
|
@@ -63,7 +65,10 @@ class Inferx
|
|
63
65
|
count + pair[1]
|
64
66
|
end
|
65
67
|
|
66
|
-
|
68
|
+
if increase > 0
|
69
|
+
hincrby(name, increase)
|
70
|
+
@redis.save unless manual?
|
71
|
+
end
|
67
72
|
end
|
68
73
|
end
|
69
74
|
|
@@ -87,7 +92,10 @@ class Inferx
|
|
87
92
|
decrease += score if score < 0
|
88
93
|
end
|
89
94
|
|
90
|
-
|
95
|
+
if decrease > 0
|
96
|
+
hincrby(name, -decrease)
|
97
|
+
@redis.save unless manual?
|
98
|
+
end
|
91
99
|
end
|
92
100
|
|
93
101
|
# Get total of scores.
|
data/lib/inferx/version.rb
CHANGED
data/spec/inferx/adapter_spec.rb
CHANGED
@@ -8,12 +8,29 @@ describe Inferx::Adapter, '#initialize' do
|
|
8
8
|
adapter.instance_eval { @redis }.should == redis
|
9
9
|
end
|
10
10
|
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
it 'sets nil to @namespace' do
|
12
|
+
adapter = described_class.new(redis_stub)
|
13
|
+
adapter.instance_eval { @namespace }.should be_nil
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'sets false to @manual' do
|
17
|
+
adapter = described_class.new(redis_stub)
|
18
|
+
adapter.should_not be_manual
|
19
|
+
end
|
20
|
+
|
21
|
+
context 'with namespace option' do
|
22
|
+
it 'sets the value of namespace option to @namespace' do
|
23
|
+
adapter = described_class.new(redis_stub, :namespace => 'example')
|
14
24
|
adapter.instance_eval { @namespace }.should == 'example'
|
15
25
|
end
|
16
26
|
end
|
27
|
+
|
28
|
+
context 'with manual option' do
|
29
|
+
it 'sets the value of manual option to @manual' do
|
30
|
+
adapter = described_class.new(redis_stub, :manual => true)
|
31
|
+
adapter.should be_manual
|
32
|
+
end
|
33
|
+
end
|
17
34
|
end
|
18
35
|
|
19
36
|
describe Inferx::Adapter, '#categories_key' do
|
@@ -37,9 +54,9 @@ describe Inferx::Adapter, '#make_categories_key' do
|
|
37
54
|
adapter.categories_key.should == 'inferx:categories'
|
38
55
|
end
|
39
56
|
|
40
|
-
context 'with
|
57
|
+
context 'with namespace' do
|
41
58
|
it 'returns the key included the namespace' do
|
42
|
-
adapter = described_class.new(redis_stub, 'example')
|
59
|
+
adapter = described_class.new(redis_stub, :namespace => 'example')
|
43
60
|
adapter.categories_key.should == 'inferx:example:categories'
|
44
61
|
end
|
45
62
|
end
|
@@ -51,9 +68,9 @@ describe Inferx::Adapter, '#make_category_key' do
|
|
51
68
|
adapter.make_category_key(:red).should == 'inferx:categories:red'
|
52
69
|
end
|
53
70
|
|
54
|
-
context 'with
|
71
|
+
context 'with namespace' do
|
55
72
|
it 'returns the key included the namespace' do
|
56
|
-
adapter = described_class.new(redis_stub, 'example')
|
73
|
+
adapter = described_class.new(redis_stub, :namespace => 'example')
|
57
74
|
adapter.make_category_key(:red).should == 'inferx:example:categories:red'
|
58
75
|
end
|
59
76
|
end
|
@@ -62,8 +79,8 @@ end
|
|
62
79
|
describe Inferx::Adapter, '#spawn' do
|
63
80
|
it 'calls constructor of the class with the instance variables and the arguments' do
|
64
81
|
redis = redis_stub
|
65
|
-
adapter = described_class.new(redis, 'example')
|
66
|
-
klass = mock.tap { |m| m.should_receive(:new).with(redis, 'arg1', 'arg2', 'example') }
|
82
|
+
adapter = described_class.new(redis, :namespace => 'example')
|
83
|
+
klass = mock.tap { |m| m.should_receive(:new).with(redis, 'arg1', 'arg2', :namespace => 'example') }
|
67
84
|
adapter.spawn(klass, 'arg1', 'arg2')
|
68
85
|
end
|
69
86
|
|
@@ -10,9 +10,10 @@ end
|
|
10
10
|
describe Inferx::Categories, '#initialize' do
|
11
11
|
it 'calls Inferx::Adapter#initialize' do
|
12
12
|
redis = redis_stub
|
13
|
-
categories = described_class.new(redis, 'example')
|
13
|
+
categories = described_class.new(redis, :namespace => 'example', :manual => true)
|
14
14
|
categories.instance_eval { @redis }.should == redis
|
15
15
|
categories.instance_eval { @namespace }.should == 'example'
|
16
|
+
categories.should be_manual
|
16
17
|
end
|
17
18
|
end
|
18
19
|
|
@@ -55,13 +56,13 @@ describe Inferx::Categories, '#get' do
|
|
55
56
|
categories.get(:red)
|
56
57
|
end
|
57
58
|
|
58
|
-
it 'calles Inferx::Category.new with the instance of Redis, the category name and the
|
59
|
+
it 'calles Inferx::Category.new with the instance of Redis, the category name and the options' do
|
59
60
|
redis = redis_stub do |s|
|
60
61
|
s.stub!(:hexists).and_return(true)
|
61
62
|
end
|
62
63
|
|
63
|
-
Inferx::Category.should_receive(:new).with(redis, :red, 'example')
|
64
|
-
categories = described_class.new(redis, 'example')
|
64
|
+
Inferx::Category.should_receive(:new).with(redis, :red, :namespace => 'example', :manual => true)
|
65
|
+
categories = described_class.new(redis, :namespace => 'example', :manual => true)
|
65
66
|
categories.get(:red)
|
66
67
|
end
|
67
68
|
|
@@ -90,6 +91,7 @@ describe Inferx::Categories, '#add' do
|
|
90
91
|
it 'calls Redis#hsetnx' do
|
91
92
|
redis = redis_stub do |s|
|
92
93
|
s.should_receive(:hsetnx).with('inferx:categories', :red, 0)
|
94
|
+
s.should_receive(:save)
|
93
95
|
end
|
94
96
|
|
95
97
|
categories = described_class.new(redis)
|
@@ -100,11 +102,24 @@ describe Inferx::Categories, '#add' do
|
|
100
102
|
redis = redis_stub do |s|
|
101
103
|
s.should_receive(:hsetnx).with('inferx:categories', :red, 0)
|
102
104
|
s.should_receive(:hsetnx).with('inferx:categories', :green, 0)
|
105
|
+
s.should_receive(:save)
|
103
106
|
end
|
104
107
|
|
105
108
|
categories = described_class.new(redis)
|
106
109
|
categories.add(:red, :green)
|
107
110
|
end
|
111
|
+
|
112
|
+
context 'with manual save' do
|
113
|
+
it 'does not call Redis#save' do
|
114
|
+
redis = redis_stub do |s|
|
115
|
+
s.stub(:hsetnx)
|
116
|
+
s.should_not_receive(:save)
|
117
|
+
end
|
118
|
+
|
119
|
+
categories = described_class.new(redis, :manual => true)
|
120
|
+
categories.add(:red)
|
121
|
+
end
|
122
|
+
end
|
108
123
|
end
|
109
124
|
|
110
125
|
describe Inferx::Categories, '#remove' do
|
@@ -112,6 +127,7 @@ describe Inferx::Categories, '#remove' do
|
|
112
127
|
redis = redis_stub do |s|
|
113
128
|
s.should_receive(:hdel).with('inferx:categories', :red)
|
114
129
|
s.should_receive(:del).with('inferx:categories:red')
|
130
|
+
s.should_receive(:save)
|
115
131
|
end
|
116
132
|
|
117
133
|
categories = described_class.new(redis)
|
@@ -123,11 +139,25 @@ describe Inferx::Categories, '#remove' do
|
|
123
139
|
s.should_receive(:hdel).with('inferx:categories', :red)
|
124
140
|
s.should_receive(:hdel).with('inferx:categories', :green)
|
125
141
|
s.should_receive(:del).with('inferx:categories:red', 'inferx:categories:green')
|
142
|
+
s.should_receive(:save)
|
126
143
|
end
|
127
144
|
|
128
145
|
categories = described_class.new(redis)
|
129
146
|
categories.remove(:red, :green)
|
130
147
|
end
|
148
|
+
|
149
|
+
context 'with manual save' do
|
150
|
+
it 'does not call Redis#save' do
|
151
|
+
redis = redis_stub do |s|
|
152
|
+
s.stub!(:hdel)
|
153
|
+
s.stub!(:del)
|
154
|
+
s.should_not_receive(:save)
|
155
|
+
end
|
156
|
+
|
157
|
+
categories = described_class.new(redis, :manual => true)
|
158
|
+
categories.remove(:red)
|
159
|
+
end
|
160
|
+
end
|
131
161
|
end
|
132
162
|
|
133
163
|
describe Inferx::Categories, '#each' do
|
@@ -4,9 +4,10 @@ require 'inferx/category'
|
|
4
4
|
describe Inferx::Category, '#initialize' do
|
5
5
|
it 'calls Inferx::Adapter#initialize' do
|
6
6
|
redis = redis_stub
|
7
|
-
category = described_class.new(redis, :red, 'example')
|
7
|
+
category = described_class.new(redis, :red, :namespace => 'example', :manual => true)
|
8
8
|
category.instance_eval { @redis }.should == redis
|
9
9
|
category.instance_eval { @namespace }.should == 'example'
|
10
|
+
category.should be_manual
|
10
11
|
end
|
11
12
|
|
12
13
|
it 'sets the category name to the name attribute' do
|
@@ -94,6 +95,7 @@ describe Inferx::Category, '#train' do
|
|
94
95
|
s.should_receive(:zincrby).with('inferx:categories:red', 2, 'apple')
|
95
96
|
s.should_receive(:zincrby).with('inferx:categories:red', 3, 'strawberry')
|
96
97
|
s.should_receive(:hincrby).with('inferx:categories', :red, 5)
|
98
|
+
s.should_receive(:save)
|
97
99
|
end
|
98
100
|
|
99
101
|
category = described_class.new(redis, :red)
|
@@ -104,12 +106,26 @@ describe Inferx::Category, '#train' do
|
|
104
106
|
it 'does not call Redis#hincrby' do
|
105
107
|
redis = redis_stub do |s|
|
106
108
|
s.should_not_receive(:hincrby)
|
109
|
+
s.should_not_receive(:save)
|
107
110
|
end
|
108
111
|
|
109
112
|
category = described_class.new(redis, :red)
|
110
113
|
category.train(%w())
|
111
114
|
end
|
112
115
|
end
|
116
|
+
|
117
|
+
context 'with manual save' do
|
118
|
+
it 'does not call Redis#save' do
|
119
|
+
redis = redis_stub do |s|
|
120
|
+
s.stub!(:zincrby)
|
121
|
+
s.stub!(:hincrby)
|
122
|
+
s.should_not_receive(:save)
|
123
|
+
end
|
124
|
+
|
125
|
+
category = described_class.new(redis, :red, :manual => true)
|
126
|
+
category.train(%w(apple strawberry apple strawberry strawberry))
|
127
|
+
end
|
128
|
+
end
|
113
129
|
end
|
114
130
|
|
115
131
|
describe Inferx::Category, '#untrain' do
|
@@ -119,6 +135,7 @@ describe Inferx::Category, '#untrain' do
|
|
119
135
|
s.should_receive(:zincrby).with('inferx:categories:red', -3, 'strawberry')
|
120
136
|
s.should_receive(:zremrangebyscore).with('inferx:categories:red', '-inf', 0).and_return(%w(3 -2 1))
|
121
137
|
s.should_receive(:hincrby).with('inferx:categories', :red, -3)
|
138
|
+
s.should_receive(:save)
|
122
139
|
end
|
123
140
|
|
124
141
|
category = described_class.new(redis, :red)
|
@@ -131,12 +148,27 @@ describe Inferx::Category, '#untrain' do
|
|
131
148
|
s.stub!(:zincrby)
|
132
149
|
s.stub!(:zremrangebyscore).and_return(%w(-2 -3 2))
|
133
150
|
s.should_not_receive(:hincrby)
|
151
|
+
s.should_not_receive(:save)
|
134
152
|
end
|
135
153
|
|
136
154
|
category = described_class.new(redis, :red)
|
137
155
|
category.untrain(%w(apple strawberry apple strawberry strawberry))
|
138
156
|
end
|
139
157
|
end
|
158
|
+
|
159
|
+
context 'with manual save' do
|
160
|
+
it 'does not call Redis#save' do
|
161
|
+
redis = redis_stub do |s|
|
162
|
+
s.stub!(:zincrby)
|
163
|
+
s.stub!(:zremrangebyscore).and_return(%w(3 -2 1))
|
164
|
+
s.stub!(:hincrby)
|
165
|
+
s.should_not_receive(:save)
|
166
|
+
end
|
167
|
+
|
168
|
+
category = described_class.new(redis, :red, :manual => true)
|
169
|
+
category.untrain(%w(apple strawberry apple strawberry strawberry))
|
170
|
+
end
|
171
|
+
end
|
140
172
|
end
|
141
173
|
|
142
174
|
describe Inferx::Category, '#size' do
|
data/spec/inferx_spec.rb
CHANGED
@@ -12,10 +12,10 @@ describe Inferx do
|
|
12
12
|
end
|
13
13
|
|
14
14
|
describe Inferx, '#initialize' do
|
15
|
-
it "calls #{described_class}::Categories.new with a connection of Redis and the
|
15
|
+
it "calls #{described_class}::Categories.new with a connection of Redis and the options" do
|
16
16
|
redis = redis_stub
|
17
|
-
Inferx::Categories.should_receive(:new).with(redis, 'example')
|
18
|
-
described_class.new(:namespace => 'example')
|
17
|
+
Inferx::Categories.should_receive(:new).with(redis, :namespace => 'example', :manual => true)
|
18
|
+
described_class.new(:namespace => 'example', :manual => true)
|
19
19
|
end
|
20
20
|
|
21
21
|
it "sets an instance of #{described_class}::Categories to the categories attribute" do
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: inferx
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-04-
|
12
|
+
date: 2012-04-16 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: redis
|