redis_backed_model 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +19 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +8 -0
- data/lib/redis_backed_model/inflections.rb +22 -0
- data/lib/redis_backed_model/redis_backed_model.rb +57 -0
- data/lib/redis_backed_model/sorted_set.rb +64 -0
- data/lib/redis_backed_model/version.rb +3 -0
- data/lib/redis_backed_model.rb +5 -0
- data/redis_backed_model.gemspec +20 -0
- data/spec/extras/person.rb +50 -0
- data/spec/redis_backed_model/inflections_spec.rb +14 -0
- data/spec/redis_backed_model/sorted_set_spec.rb +51 -0
- data/spec/redis_backed_model_spec.rb +171 -0
- data/spec/spec_helper.rb +25 -0
- metadata +118 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Ian Whitney
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# RedisBackedModel
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'redis_backed_model'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install redis_backed_model
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
module ActiveSupport::Inflector
|
2
|
+
|
3
|
+
def deinstance_variableize(the_string)
|
4
|
+
result = the_string.to_s.dup
|
5
|
+
result.downcase.gsub(/^@/, '')
|
6
|
+
end
|
7
|
+
|
8
|
+
def instance_variableize(the_string)
|
9
|
+
"@#{the_string}".to_sym
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
class String
|
14
|
+
def deinstance_variableize
|
15
|
+
ActiveSupport::Inflector.deinstance_variableize(self)
|
16
|
+
end
|
17
|
+
|
18
|
+
def instance_variableize
|
19
|
+
ActiveSupport::Inflector.instance_variableize(self)
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
module RedisBackedModel
|
2
|
+
class RedisBackedModel
|
3
|
+
|
4
|
+
def initialize(attributes={})
|
5
|
+
if attributes.class == Hash
|
6
|
+
attributes.each do |key, value|
|
7
|
+
add_to_instance_variables(key, value)
|
8
|
+
end
|
9
|
+
else
|
10
|
+
raise ArgumentError
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def to_redis
|
15
|
+
redis_commands = []
|
16
|
+
redis_commands << redis_set_command
|
17
|
+
instance_variables.each do | var |
|
18
|
+
redis_commands << instance_variable_to_redis(var) if instance_variable_to_redis(var)
|
19
|
+
end
|
20
|
+
scores.each do |score|
|
21
|
+
redis_commands << score.to_redis
|
22
|
+
end
|
23
|
+
redis_commands
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
attr_reader :id, :scores
|
29
|
+
|
30
|
+
def add_to_instance_variables(key, value)
|
31
|
+
if key.match(/score_\[\w+\|\w+\]/)
|
32
|
+
add_to_scores(key, value)
|
33
|
+
else
|
34
|
+
self.instance_variable_set("@#{key}", value)
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def add_to_scores(key, value)
|
39
|
+
@scores ||= []
|
40
|
+
@scores << SortedSet.new(self.class, id, Hash[key,value])
|
41
|
+
end
|
42
|
+
|
43
|
+
def instance_variable_to_redis(instance_variable)
|
44
|
+
"hset #{model_name_for_redis}:#{id} #{instance_variable.to_s.deinstance_variableize} #{instance_variable_get(instance_variable.to_s)}" unless instance_variable.to_s == '@id'
|
45
|
+
end
|
46
|
+
|
47
|
+
def model_name_for_redis
|
48
|
+
class_as_string = self.class.to_s.demodulize.underscore
|
49
|
+
end
|
50
|
+
|
51
|
+
def redis_set_command
|
52
|
+
"sadd #{model_name_for_redis}_ids #{id}"
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module RedisBackedModel
|
2
|
+
|
3
|
+
class SortedSet
|
4
|
+
require 'date'
|
5
|
+
|
6
|
+
|
7
|
+
def initialize(model, model_id, definition)
|
8
|
+
@model = model
|
9
|
+
@model_id = model_id
|
10
|
+
@definition = definition
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_redis
|
14
|
+
"zadd #{key} #{score} #{member}"
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
attr_accessor :model, :model_id #, :by_attribute, :score_label, :score
|
20
|
+
|
21
|
+
def definition_keys
|
22
|
+
@definition.keys.first
|
23
|
+
end
|
24
|
+
|
25
|
+
def definition_values
|
26
|
+
@definition.values.first
|
27
|
+
end
|
28
|
+
|
29
|
+
def key_by
|
30
|
+
parse_definition(definition_keys)[1]
|
31
|
+
end
|
32
|
+
|
33
|
+
def key_for
|
34
|
+
parse_definition(definition_keys)[0]
|
35
|
+
end
|
36
|
+
|
37
|
+
def key_model_name
|
38
|
+
@model.to_s.underscore.pluralize
|
39
|
+
end
|
40
|
+
|
41
|
+
def key_for_value
|
42
|
+
parse_definition(definition_values)[0]
|
43
|
+
end
|
44
|
+
|
45
|
+
def member
|
46
|
+
@model_id
|
47
|
+
end
|
48
|
+
|
49
|
+
def key
|
50
|
+
"#{key_model_name}_for_#{key_for}_by_#{key_by}:#{key_for_value}"
|
51
|
+
end
|
52
|
+
|
53
|
+
def score
|
54
|
+
score = parse_definition(definition_values)[1]
|
55
|
+
key_by == 'date' ? Date.parse(score).to_time.to_f : score
|
56
|
+
end
|
57
|
+
|
58
|
+
def parse_definition(string)
|
59
|
+
string.match(/.*\[(\S+)\]/)[1].split('|')
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/redis_backed_model/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Ian Whitney", "Davin Lagerroos"]
|
6
|
+
gem.email = ["iwhitney@ssa-i.org","dlaerroos@ssa-i.org"]
|
7
|
+
gem.description = %q{Provides methods to models that are backed by a redis instance.}
|
8
|
+
gem.summary = %q{Provides methods for the creation of redis-backed models, specifically the handling of sorted-set attributes and returning commands that will store the object in redis.}
|
9
|
+
gem.homepage = "https://github.com/SeniorServiceAmerica/redis_backed_model"
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "redis_backed_model"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = RedisBackedModel::VERSION
|
17
|
+
gem.add_development_dependency 'rspec'
|
18
|
+
gem.add_development_dependency 'redis'
|
19
|
+
gem.add_dependency('active_support')
|
20
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
class Person < RedisBackedModel::RedisBackedModel
|
2
|
+
attr_reader :id, :first_name, :last_name
|
3
|
+
|
4
|
+
def self.all
|
5
|
+
all = []
|
6
|
+
ids.each do |id|
|
7
|
+
all << self.find(id)
|
8
|
+
end
|
9
|
+
all
|
10
|
+
end
|
11
|
+
|
12
|
+
#def self.first
|
13
|
+
#id = ids.first
|
14
|
+
#self.find(id)
|
15
|
+
#end
|
16
|
+
|
17
|
+
def self.find(id)
|
18
|
+
attr = $redis.hgetall("person:#{id}")
|
19
|
+
self.new(attr.merge({:id => id}))
|
20
|
+
end
|
21
|
+
|
22
|
+
#def self.last
|
23
|
+
#id = ids.last
|
24
|
+
#self.find(id)
|
25
|
+
#end
|
26
|
+
|
27
|
+
def self.to_csv
|
28
|
+
f = File.new("person.csv", "w+")
|
29
|
+
f << "id,first_name,last_name\n"
|
30
|
+
self.all.each do |x|
|
31
|
+
f << "#{x.id},#{x.first_name},#{x.last_name}\n"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# def initialize(attr)
|
36
|
+
# @id = attr[:id]
|
37
|
+
# @first_name = attr["first_name"]
|
38
|
+
# @last_name = attr["last_name"]
|
39
|
+
# end
|
40
|
+
|
41
|
+
def name
|
42
|
+
"#{self.first_name} #{self.last_name}"
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def self.ids
|
48
|
+
@ids ||= $redis.sort('person_ids')
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
|
4
|
+
describe String do
|
5
|
+
|
6
|
+
it "instance_variablizes by pre-pending an '@' and turning itself into a symbol" do
|
7
|
+
'my_test_string'.instance_variableize.should eq(:@my_test_string)
|
8
|
+
end
|
9
|
+
|
10
|
+
it "deinstance_variableizes by removing the leading '@" do
|
11
|
+
'@instance_variable'.deinstance_variableize.should eq('instance_variable')
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe RedisBackedModel::SortedSet do
|
4
|
+
before(:all) do
|
5
|
+
|
6
|
+
end
|
7
|
+
|
8
|
+
before(:each) do
|
9
|
+
@scored_set = RedisBackedModel::SortedSet.new(FalseClass, 1, {'score_[foo|bar]' => '[wibble|wobble]'})
|
10
|
+
end
|
11
|
+
|
12
|
+
it "returns returns first part of [|] definition key as for_key" do
|
13
|
+
@scored_set.send(:key_for).should eq('foo')
|
14
|
+
end
|
15
|
+
|
16
|
+
it "returns first part of [|] definition value as for_value" do
|
17
|
+
@scored_set.send(:key_for_value).should eq('wibble')
|
18
|
+
end
|
19
|
+
|
20
|
+
it "returns the model, underscored and pluralized as model_name_for_key" do
|
21
|
+
@scored_set.send(:key_model_name).should eq('false_classes')
|
22
|
+
end
|
23
|
+
|
24
|
+
it "returns second part of definition key as key" do
|
25
|
+
@scored_set.send(:key_by).should eq('bar')
|
26
|
+
end
|
27
|
+
|
28
|
+
it "returns (key_model_name) + '_for_' + key_for + '_by_' + key_by + ':' + for_value as key" do
|
29
|
+
@scored_set.send(:key).should eq('false_classes_for_foo_by_bar:wibble')
|
30
|
+
end
|
31
|
+
|
32
|
+
it "returns second part of definition value as score" do
|
33
|
+
@scored_set.send(:score).should eq('wobble')
|
34
|
+
end
|
35
|
+
|
36
|
+
it "converts score dates to miliseconds" do
|
37
|
+
set = RedisBackedModel::SortedSet.new(FalseClass, 1, {'score_[foo|date]'=>'[wibble|2012-03-04]'})
|
38
|
+
date_in_milliseconds = Date.civil(2012,3,4).to_time.to_f
|
39
|
+
set.send(:score).should eq(date_in_milliseconds)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "returns model_is as member" do
|
43
|
+
@scored_set.send(:member).should eq(1)
|
44
|
+
@scored_set.send(:member).should eq(@scored_set.send(:model_id))
|
45
|
+
end
|
46
|
+
|
47
|
+
it "returns 'zadd key value model_id' as to_redis" do
|
48
|
+
@scored_set.to_redis.should eq('zadd false_classes_for_foo_by_bar:wibble wobble 1')
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
@@ -0,0 +1,171 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require 'extras/person'
|
3
|
+
|
4
|
+
describe RedisBackedModel do
|
5
|
+
before(:all) do
|
6
|
+
|
7
|
+
end
|
8
|
+
|
9
|
+
before(:each) do
|
10
|
+
@attributes = {}
|
11
|
+
@size = rand(9) + 1
|
12
|
+
key_seed = 'abcdefghijklmn'
|
13
|
+
(1..@size).each do | i |
|
14
|
+
key = key_seed[i..(rand(i)+i)]
|
15
|
+
@attributes[key] = i
|
16
|
+
end
|
17
|
+
@attributes['id'] = 1
|
18
|
+
@size += 1
|
19
|
+
end
|
20
|
+
|
21
|
+
it "has no instance variables unless specified" do
|
22
|
+
rbm = RedisBackedModel::RedisBackedModel.new
|
23
|
+
rbm.instance_variables.count.should eq(0)
|
24
|
+
end
|
25
|
+
|
26
|
+
it "creates an instance variable for a hash with one member" do
|
27
|
+
attributes = {'id' => 1}
|
28
|
+
rbm = RedisBackedModel::RedisBackedModel.new(attributes)
|
29
|
+
rbm.instance_variables.include?(:@id).should eq(true)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "creates an instance variable for each member of an attribute hash that doesn't have scores" do
|
33
|
+
rbm = RedisBackedModel::RedisBackedModel.new(@attributes)
|
34
|
+
rbm.instance_variables.count.should eq(@size)
|
35
|
+
@attributes.each do | key, value|
|
36
|
+
# rbm.instance_variables.include?("@#{key}".to_sym).should eq(true), "no instance variable for #{key}"
|
37
|
+
rbm.instance_variables.include?(key.instance_variableize).should eq(true), "no instance variable for #{key}"
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
it "raises an argument error if something other than a hash is passed in" do
|
42
|
+
expect { RedisBackedModel::RedisBackedModel.new('w') }.to raise_error(ArgumentError)
|
43
|
+
expect { RedisBackedModel::RedisBackedModel.new(['w', 1]) }.to raise_error(ArgumentError)
|
44
|
+
end
|
45
|
+
|
46
|
+
it "does not create a key for any score_[|] attribute" do
|
47
|
+
score_key = 'score_[foo|bar]'
|
48
|
+
@attributes[score_key] = '[1|2]'
|
49
|
+
rbm = RedisBackedModel::RedisBackedModel.new(@attributes)
|
50
|
+
rbm.instance_variables.include?(score_key.instance_variableize).should eq(false)
|
51
|
+
end
|
52
|
+
|
53
|
+
it "creates a scores instance variable if there are any score_[x|y] attributes" do
|
54
|
+
score_key = 'score_[foo|bar]'
|
55
|
+
@attributes[score_key] = '[1|2]'
|
56
|
+
rbm = RedisBackedModel::RedisBackedModel.new(@attributes)
|
57
|
+
rbm.instance_variables.include?('scores'.instance_variableize).should eq(true)
|
58
|
+
end
|
59
|
+
|
60
|
+
it "stores score_ attributes in scores as SortedSet objects" do
|
61
|
+
['score_[foo|bar]', 'score_[baz|qux]', 'score_[wibble|wobble]'].each_with_index do |score,index|
|
62
|
+
@attributes[score] = "[#{index}|#{index + 1}]"
|
63
|
+
end
|
64
|
+
rbm = RedisBackedModel::RedisBackedModel.new(@attributes)
|
65
|
+
rbm.send(:scores).each do |score|
|
66
|
+
score.class.should eq(RedisBackedModel::SortedSet)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
it "does not add near matches to scores instance variable, so it tries to add it as an instance variable instead, raising a name error because of []" do
|
71
|
+
['score_[foobar]', 'score[foo|bar]', 'score_[foobar|]'].each_with_index do |s,i|
|
72
|
+
@attributes[s] = '[i|i+1]'
|
73
|
+
expect { rbm = RedisBackedModel::RedisBackedModel.new(@attributes) }.to raise_error(NameError)
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
it "returns a redis command to adds the model id to a set named (model_name)_ids" do
|
78
|
+
rbm = RedisBackedModel::RedisBackedModel.new(@attributes)
|
79
|
+
rbm.send(:redis_set_command).should eq('sadd redis_backed_model_ids 1')
|
80
|
+
end
|
81
|
+
|
82
|
+
it "creates a hset command for instance variables that are not id" do
|
83
|
+
rbm = RedisBackedModel::RedisBackedModel.new()
|
84
|
+
rbm.instance_variable_set('@id', 1)
|
85
|
+
rbm.instance_variable_set('@foo', 20)
|
86
|
+
rbm.send(:instance_variable_to_redis, '@foo').should eq('hset redis_backed_model:1 foo 20')
|
87
|
+
end
|
88
|
+
|
89
|
+
it "puts a underscored version of the model name as the first part of the hset key name in instance_variable_set" do
|
90
|
+
rbm = RedisBackedModel::RedisBackedModel.new()
|
91
|
+
rbm.instance_variable_set('@id', 1)
|
92
|
+
rbm.instance_variable_set('@foo', 20)
|
93
|
+
hset_command = rbm.send(:instance_variable_to_redis, '@foo')
|
94
|
+
hset_command.split(' ')[1].split(':')[0].should eq(RedisBackedModel.to_s.underscore)
|
95
|
+
end
|
96
|
+
|
97
|
+
it "puts the id as the second part of the hset hset key name in instance_variable_set" do
|
98
|
+
rbm = RedisBackedModel::RedisBackedModel.new()
|
99
|
+
rbm.instance_variable_set('@id', 1)
|
100
|
+
rbm.instance_variable_set('@foo', 20)
|
101
|
+
hset_command = rbm.send(:instance_variable_to_redis, '@foo')
|
102
|
+
hset_command.split(' ')[1].split(':')[1].should eq(rbm.instance_variable_get('@id').to_s)
|
103
|
+
end
|
104
|
+
|
105
|
+
it "puts the instance_variable_name (without @) as the hset field name in instance_variable_set" do
|
106
|
+
rbm = RedisBackedModel::RedisBackedModel.new()
|
107
|
+
rbm.instance_variable_set('@id', 1)
|
108
|
+
rbm.instance_variable_set('@foo', 20)
|
109
|
+
hset_command = rbm.send(:instance_variable_to_redis, '@foo')
|
110
|
+
hset_command.split(' ')[2].should eq('foo')
|
111
|
+
end
|
112
|
+
|
113
|
+
it "put the instance variable value as the hset value in instance_variable_set" do
|
114
|
+
rbm = RedisBackedModel::RedisBackedModel.new()
|
115
|
+
rbm.instance_variable_set('@id', 1)
|
116
|
+
rbm.instance_variable_set('@foo', 20)
|
117
|
+
hset_command = rbm.send(:instance_variable_to_redis, '@foo')
|
118
|
+
hset_command.split(' ')[3].should eq('20')
|
119
|
+
end
|
120
|
+
|
121
|
+
it "does not create an hset command for the id instance variable" do
|
122
|
+
rbm = RedisBackedModel::RedisBackedModel.new()
|
123
|
+
rbm.instance_variable_set('@id', 1)
|
124
|
+
rbm.send(:instance_variable_to_redis, '@id').should eq(nil)
|
125
|
+
end
|
126
|
+
|
127
|
+
it "includes as sadd command in to_redis" do
|
128
|
+
@attributes['score_[foo|bar]'] = '[1|2012-03-04]'
|
129
|
+
rbm = RedisBackedModel::RedisBackedModel.new(@attributes)
|
130
|
+
rbm.to_redis.select { |command| command.match(/sadd/)}.count.should eq(1)
|
131
|
+
end
|
132
|
+
|
133
|
+
it "includes a hset command for each instance variable except id in to_redis" do
|
134
|
+
@attributes['score_[foo|bar]'] = '[1|2012-03-04]'
|
135
|
+
rbm = RedisBackedModel::RedisBackedModel.new(@attributes)
|
136
|
+
expected = rbm.instance_variables.count - 1
|
137
|
+
rbm.to_redis.select {|command| command.match(/hset/)}.count.should eq(expected)
|
138
|
+
end
|
139
|
+
|
140
|
+
it "includes a sorted_set.to_redis command for each score attribute in to_redis" do
|
141
|
+
scores = ['score_[foo|bar]', 'score_[baz|qux]', 'score_[wibble|wobble]']
|
142
|
+
scores.each_with_index do |score,index|
|
143
|
+
@attributes[score] = "[#{index}|#{index + 1}]"
|
144
|
+
end
|
145
|
+
rbm = RedisBackedModel::RedisBackedModel.new(@attributes)
|
146
|
+
rbm.to_redis.select { |command| command.match(/zadd/) }.count.should eq(scores.count)
|
147
|
+
end
|
148
|
+
|
149
|
+
end
|
150
|
+
|
151
|
+
# test inheriting from RedisBackedModel
|
152
|
+
describe Person do
|
153
|
+
|
154
|
+
before(:all) do
|
155
|
+
$redis.hset 'person:0', 'first_name', 'jane'
|
156
|
+
$redis.hset 'person:0', 'last_name', 'doe'
|
157
|
+
end
|
158
|
+
|
159
|
+
it "should set its instance_variables from redis, using RedisBackedModel initialize" do
|
160
|
+
person = Person.find(0)
|
161
|
+
person.instance_variables.include?(:@first_name).should eq(true)
|
162
|
+
person.instance_variables.include?(:@last_name).should eq(true)
|
163
|
+
person.instance_variables.include?(:@id).should eq(true)
|
164
|
+
end
|
165
|
+
|
166
|
+
it "should have same name method" do
|
167
|
+
person = Person.find(0)
|
168
|
+
person.name.should eq('jane doe')
|
169
|
+
end
|
170
|
+
|
171
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'redis_backed_model'
|
2
|
+
require 'redis'
|
3
|
+
|
4
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
5
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
6
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
7
|
+
# loaded once.
|
8
|
+
#
|
9
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
12
|
+
config.run_all_when_everything_filtered = true
|
13
|
+
config.filter_run :focus
|
14
|
+
|
15
|
+
# Run specs in random order to surface order dependencies. If you find an
|
16
|
+
# order dependency and want to debug it, you can fix the order by providing
|
17
|
+
# the seed, which is printed after each run.
|
18
|
+
# --seed 1234
|
19
|
+
config.order = 'random'
|
20
|
+
end
|
21
|
+
|
22
|
+
SPEC_ROOT = File.dirname(__FILE__)
|
23
|
+
require File.join(SPEC_ROOT, '/../lib/redis_backed_model')
|
24
|
+
|
25
|
+
$redis = Redis.new()
|
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: redis_backed_model
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ian Whitney
|
9
|
+
- Davin Lagerroos
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2012-08-29 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ! '>='
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '0'
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
27
|
+
requirements:
|
28
|
+
- - ! '>='
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '0'
|
31
|
+
- !ruby/object:Gem::Dependency
|
32
|
+
name: redis
|
33
|
+
requirement: !ruby/object:Gem::Requirement
|
34
|
+
none: false
|
35
|
+
requirements:
|
36
|
+
- - ! '>='
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
type: :development
|
40
|
+
prerelease: false
|
41
|
+
version_requirements: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ! '>='
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: active_support
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :runtime
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
description: Provides methods to models that are backed by a redis instance.
|
64
|
+
email:
|
65
|
+
- iwhitney@ssa-i.org
|
66
|
+
- dlaerroos@ssa-i.org
|
67
|
+
executables: []
|
68
|
+
extensions: []
|
69
|
+
extra_rdoc_files: []
|
70
|
+
files:
|
71
|
+
- .gitignore
|
72
|
+
- Gemfile
|
73
|
+
- LICENSE
|
74
|
+
- README.md
|
75
|
+
- Rakefile
|
76
|
+
- lib/redis_backed_model.rb
|
77
|
+
- lib/redis_backed_model/inflections.rb
|
78
|
+
- lib/redis_backed_model/redis_backed_model.rb
|
79
|
+
- lib/redis_backed_model/sorted_set.rb
|
80
|
+
- lib/redis_backed_model/version.rb
|
81
|
+
- redis_backed_model.gemspec
|
82
|
+
- spec/extras/person.rb
|
83
|
+
- spec/redis_backed_model/inflections_spec.rb
|
84
|
+
- spec/redis_backed_model/sorted_set_spec.rb
|
85
|
+
- spec/redis_backed_model_spec.rb
|
86
|
+
- spec/spec_helper.rb
|
87
|
+
homepage: https://github.com/SeniorServiceAmerica/redis_backed_model
|
88
|
+
licenses: []
|
89
|
+
post_install_message:
|
90
|
+
rdoc_options: []
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
none: false
|
101
|
+
requirements:
|
102
|
+
- - ! '>='
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 1.8.21
|
108
|
+
signing_key:
|
109
|
+
specification_version: 3
|
110
|
+
summary: Provides methods for the creation of redis-backed models, specifically the
|
111
|
+
handling of sorted-set attributes and returning commands that will store the object
|
112
|
+
in redis.
|
113
|
+
test_files:
|
114
|
+
- spec/extras/person.rb
|
115
|
+
- spec/redis_backed_model/inflections_spec.rb
|
116
|
+
- spec/redis_backed_model/sorted_set_spec.rb
|
117
|
+
- spec/redis_backed_model_spec.rb
|
118
|
+
- spec/spec_helper.rb
|