Skip to content

Commit 595a59a

Browse files
authored
Merge pull request #642 from splitrb/outside-web-sessions
add a simple way to load users outside web session
2 parents 3cd4ec3 + e5c1534 commit 595a59a

File tree

5 files changed

+45
-3
lines changed

5 files changed

+45
-3
lines changed

lib/split/persistence.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ module Persistence
88
require 'split/persistence/session_adapter'
99

1010
ADAPTERS = {
11-
:cookie => Split::Persistence::CookieAdapter,
12-
:session => Split::Persistence::SessionAdapter
11+
cookie: Split::Persistence::CookieAdapter,
12+
session: Split::Persistence::SessionAdapter,
13+
redis: Split::Persistence::RedisAdapter,
14+
dual_adapter: Split::Persistence::DualAdapter
1315
}.freeze
1416

1517
def self.adapter

lib/split/persistence/redis_adapter.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ def keys
4040
Split.redis.hkeys(redis_key)
4141
end
4242

43+
def self.find(user_id)
44+
new(nil, user_id)
45+
end
46+
4347
def self.with_config(options={})
4448
self.config.merge!(options)
4549
self

lib/split/user.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class User
88
def_delegators :@user, :keys, :[], :[]=, :delete
99
attr_reader :user
1010

11-
def initialize(context, adapter=nil)
11+
def initialize(context, adapter = nil)
1212
@user = adapter || Split::Persistence.adapter.new(context)
1313
@cleaned_up = false
1414
end
@@ -54,6 +54,16 @@ def active_experiments
5454
experiment_pairs
5555
end
5656

57+
def self.find(user_id, adapter)
58+
adapter = adapter.is_a?(Symbol) ? Split::Persistence::ADAPTERS[adapter] : adapter
59+
60+
if adapter.respond_to?(:find)
61+
User.new(nil, adapter.find(user_id))
62+
else
63+
nil
64+
end
65+
end
66+
5767
private
5868

5969
def keys_without_experiment(keys, experiment_key)

spec/persistence/redis_adapter_spec.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@
6060
end
6161
end
6262

63+
describe '#find' do
64+
before { Split::Persistence::RedisAdapter.with_config(:lookup_by => proc{'frag'}, :namespace => 'a_namespace') }
65+
66+
it "should create and user from a given key" do
67+
adapter = Split::Persistence::RedisAdapter.find(2)
68+
expect(adapter.redis_key).to eq("a_namespace:2")
69+
end
70+
end
71+
6372
context 'functional tests' do
6473
before { Split::Persistence::RedisAdapter.with_config(:lookup_by => 'lookup') }
6574

spec/user_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,23 @@
7272
end
7373
end
7474

75+
context 'allows user to be loaded from adapter' do
76+
it 'loads user from adapter (RedisAdapter)' do
77+
user = Split::Persistence::RedisAdapter.new(nil, 112233)
78+
user['foo'] = 'bar'
79+
80+
ab_user = Split::User.find(112233, :redis)
81+
82+
expect(ab_user['foo']).to eql('bar')
83+
end
84+
85+
it 'returns nil if adapter does not implement a finder method' do
86+
ab_user = Split::User.find(112233, :dual_adapter)
87+
expect(ab_user).to be_nil
88+
end
89+
90+
end
91+
7592
context "instantiated with custom adapter" do
7693
let(:custom_adapter) { double(:persistence_adapter) }
7794

0 commit comments

Comments
 (0)