relational_redis_mapper 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +31 -0
- data/Rakefile +2 -0
- data/lib/relational_redis_mapper.rb +152 -0
- data/lib/relational_redis_mapper/key_gen.rb +28 -0
- data/lib/relational_redis_mapper/redis.rb +89 -0
- data/lib/relational_redis_mapper/relation.rb +55 -0
- data/lib/relational_redis_mapper/utility_methods.rb +23 -0
- data/lib/relational_redis_mapper/version.rb +3 -0
- data/relational_redis_mapper.gemspec +28 -0
- data/spec/relational_redis_mapper_spec.rb +149 -0
- data/spec/spec_helper.rb +89 -0
- metadata +159 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZDJmMmFlODIzMzE3ZDllZGFmYWE4MTM2NjY4ZTBmZmEwZWFmZWQ2Ng==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ZmM5OWYyZGNhZTljOTMxM2M1YmRhN2FlYWE2YTZmMzM4ZTFmYjBiNg==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NzQzYTE2MDAwYjcyM2I4ZWYzOGUzMzQ5ZGZiNTA0NmFlMjFhNGQ3NDljMjA2
|
10
|
+
ZmNlNGRiMTE1YmFiYjliMzlkMjgyNzUxZjQxMzlhYTQwYzRiOWQ3NGJhYzE1
|
11
|
+
NjQxZjA0MDA1Y2RmZjlhM2NlMmYxOTVmNTU5ZjIwYjBmMjVkMjI=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ZDcwYmY2MGFjMGJlYzM1NTk4YWUyMDUyY2IwNjkwYWZhYmIzYmFhOGFmNTFi
|
14
|
+
M2UyYjhmYzUxYjZlM2ZkYjZlODM1MzA2ZWJlNDY5NDE1MzFiZjc1YWJkZmYz
|
15
|
+
ZmIyMzQ0OTM5YzY2ODE4YzE0ZTQxOTVmZjExZWQ0NzRhMTM1NDg=
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Aaron Weisberg
|
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,31 @@
|
|
1
|
+
# RelationalRedisMapper
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'relational_redis_mapper'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install relational_redis_mapper
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
TODO: Write usage instructions here
|
24
|
+
|
25
|
+
## Contributing
|
26
|
+
|
27
|
+
1. Fork it ( https://github.com/[my-github-username]/relational_redis_mapper/fork )
|
28
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
29
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
30
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
31
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,152 @@
|
|
1
|
+
require "relational_redis_mapper/version"
|
2
|
+
require 'redis'
|
3
|
+
require 'pry'
|
4
|
+
require 'active_support/inflector'
|
5
|
+
require 'relational_redis_mapper/redis'
|
6
|
+
require 'relational_redis_mapper/relation'
|
7
|
+
require 'relational_redis_mapper/key_gen'
|
8
|
+
require 'relational_redis_mapper/utility_methods'
|
9
|
+
|
10
|
+
|
11
|
+
$redis = RelationalRedisMapper::Redis.set(connection: ::Redis.new)
|
12
|
+
|
13
|
+
module RelationalRedisMapper
|
14
|
+
include UtilityMethods
|
15
|
+
|
16
|
+
extend Forwardable
|
17
|
+
attr_accessor :persisted, :id
|
18
|
+
|
19
|
+
def self.included(base)
|
20
|
+
base.extend ClassMethods
|
21
|
+
end
|
22
|
+
|
23
|
+
def_delegators :klass, :class_key, :query_keys, :uniqueness_attr, :key_gen, :redis
|
24
|
+
def id
|
25
|
+
@id ||= key_gen.uniq_id
|
26
|
+
end
|
27
|
+
|
28
|
+
class ValidationError; end
|
29
|
+
def validate
|
30
|
+
uniqueness_attr.none? do |attr|
|
31
|
+
redis.get_uniq_index key_gen.uniqueness_key(attr, send(attr))
|
32
|
+
end || raise(ValidationError)
|
33
|
+
end
|
34
|
+
|
35
|
+
def save
|
36
|
+
persisted_version.delete if changed?
|
37
|
+
if validate
|
38
|
+
save_object; save_indices
|
39
|
+
return self
|
40
|
+
end
|
41
|
+
nil
|
42
|
+
end
|
43
|
+
|
44
|
+
def delete
|
45
|
+
remove_indices; delete_object
|
46
|
+
end
|
47
|
+
|
48
|
+
def changed?
|
49
|
+
persisted_version && persisted_version != self
|
50
|
+
end
|
51
|
+
|
52
|
+
def persisted_version
|
53
|
+
klass.find(id)
|
54
|
+
end
|
55
|
+
|
56
|
+
private
|
57
|
+
|
58
|
+
def save_object
|
59
|
+
redis.save_object class_key, id, self
|
60
|
+
end
|
61
|
+
|
62
|
+
def delete_object
|
63
|
+
redis.delete_object class_key, id
|
64
|
+
end
|
65
|
+
|
66
|
+
def save_indices
|
67
|
+
query_keys.all?{|attr| redis.add_query_index key_gen.query_key(attr, send(attr)), id } &&
|
68
|
+
uniqueness_attr.all?{|attr| redis.add_uniq_index key_gen.uniqueness_key(attr, send(attr)), id }
|
69
|
+
end
|
70
|
+
|
71
|
+
def remove_indices
|
72
|
+
query_keys.all?{|attr| redis.rm_search_index key_gen.query_key(attr, send(attr)), id } &&
|
73
|
+
uniqueness_attr.all? { |attr| redis.rm_uniq_index key_gen.uniqueness_key(attr, send(attr)) }
|
74
|
+
end
|
75
|
+
|
76
|
+
module ClassMethods
|
77
|
+
|
78
|
+
#attr_readers that default to empty array
|
79
|
+
[:query_keys, :uniqueness_attr].each do |reader|
|
80
|
+
define_method reader do
|
81
|
+
instance_variable_get "@#{reader}" || []
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def validate_uniqueness_of(*args)
|
86
|
+
@uniqueness_attr = args
|
87
|
+
end
|
88
|
+
|
89
|
+
def query_attr(*keys)
|
90
|
+
@query_keys = keys
|
91
|
+
keys.each do |key|
|
92
|
+
|
93
|
+
define_singleton_method "find_by_#{key}" do |query|
|
94
|
+
find redis.find_id(key_gen.query_key(key, query))
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
def has_many_ordered(opt)
|
101
|
+
opt.each do |relation_collection, order_by|
|
102
|
+
|
103
|
+
define_method relation_collection do
|
104
|
+
instance_variable_set(
|
105
|
+
"@#{relation_collection}",
|
106
|
+
instance_variable_get("@#{relation_collection}") ||
|
107
|
+
Relation.new(self, relation_collection, order_by)
|
108
|
+
)
|
109
|
+
end
|
110
|
+
|
111
|
+
end
|
112
|
+
end
|
113
|
+
alias_method :has_many, :has_many_ordered
|
114
|
+
|
115
|
+
|
116
|
+
def find(id)
|
117
|
+
redis.find_object(class_key, id)
|
118
|
+
end
|
119
|
+
|
120
|
+
def find_all_by_ids(ids)
|
121
|
+
redis.find_objects(class_key, ids)
|
122
|
+
end
|
123
|
+
|
124
|
+
def where(attr, attr_val)
|
125
|
+
find_all_by_ids get_all_ids(attr, attr_val)
|
126
|
+
end
|
127
|
+
|
128
|
+
def get_all_ids(attr, val)
|
129
|
+
redis.find_ids key_gen.query_key(attr, val)
|
130
|
+
end
|
131
|
+
|
132
|
+
def all
|
133
|
+
redis.all_objects(class_key)
|
134
|
+
end
|
135
|
+
|
136
|
+
def class_key
|
137
|
+
key_gen.class_key
|
138
|
+
end
|
139
|
+
|
140
|
+
private
|
141
|
+
|
142
|
+
def redis
|
143
|
+
Redis.get
|
144
|
+
end
|
145
|
+
|
146
|
+
def key_gen
|
147
|
+
@key_gen ||= KeyGen.new(self)
|
148
|
+
end
|
149
|
+
|
150
|
+
end
|
151
|
+
|
152
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module RelationalRedisMapper
|
2
|
+
class KeyGen
|
3
|
+
|
4
|
+
attr_reader :class_key
|
5
|
+
|
6
|
+
def initialize(klass)
|
7
|
+
@class_key = klass.to_s.deconstantize
|
8
|
+
end
|
9
|
+
|
10
|
+
def query_key(attr_nm, attr_val)
|
11
|
+
"index:" << class_attr_val_key(attr_nm, attr_val)
|
12
|
+
end
|
13
|
+
|
14
|
+
#used for uniqueness validator
|
15
|
+
def uniqueness_key(attr, attr_val)
|
16
|
+
"uniqueness:" << class_attr_val_key(attr, attr_val)
|
17
|
+
end
|
18
|
+
|
19
|
+
def class_attr_val_key(attr, attr_val)
|
20
|
+
"#{class_key}:#{attr}:#{attr_val}"
|
21
|
+
end
|
22
|
+
|
23
|
+
def uniq_id
|
24
|
+
SecureRandom.uuid
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,89 @@
|
|
1
|
+
module RelationalRedisMapper
|
2
|
+
class Redis
|
3
|
+
|
4
|
+
attr_reader :redis, :serializer
|
5
|
+
|
6
|
+
class << self
|
7
|
+
|
8
|
+
def get(opt={})
|
9
|
+
@singleton ||= new(opt)
|
10
|
+
end
|
11
|
+
alias_method :set, :get
|
12
|
+
|
13
|
+
end
|
14
|
+
|
15
|
+
def add_query_index(key,val)
|
16
|
+
redis.lpush key, val
|
17
|
+
end
|
18
|
+
|
19
|
+
def find_id(key)
|
20
|
+
redis.lindex key, 0
|
21
|
+
end
|
22
|
+
|
23
|
+
def find_ids(key)
|
24
|
+
redis.lrange(key, 0, -1).flatten
|
25
|
+
end
|
26
|
+
|
27
|
+
def find_object(class_key, object_key)
|
28
|
+
deserialize redis.hget(class_key, object_key) rescue nil
|
29
|
+
end
|
30
|
+
|
31
|
+
def find_objects(key, ids)
|
32
|
+
redis.hmget(key, ids).map { |str| deserialize(str) } rescue []
|
33
|
+
end
|
34
|
+
|
35
|
+
def all_objects(key)
|
36
|
+
redis.hgetall(key).values.map { |str| deserialize(str) }
|
37
|
+
end
|
38
|
+
|
39
|
+
def rm_search_index(key,val)
|
40
|
+
redis.lrem key,1,val
|
41
|
+
end
|
42
|
+
|
43
|
+
def add_uniq_index(key,val)
|
44
|
+
redis.set key, val
|
45
|
+
end
|
46
|
+
|
47
|
+
def rm_uniq_index(key)
|
48
|
+
redis.del key
|
49
|
+
end
|
50
|
+
|
51
|
+
def get_uniq_index(key)
|
52
|
+
redis.get key
|
53
|
+
end
|
54
|
+
|
55
|
+
def save_object(*args)
|
56
|
+
object = args.pop
|
57
|
+
redis.hset *args, serialize(object)
|
58
|
+
end
|
59
|
+
|
60
|
+
def delete_object(*args)
|
61
|
+
redis.hdel *args
|
62
|
+
end
|
63
|
+
|
64
|
+
def remove_indices
|
65
|
+
query_keys.each { |attr| redis.lrem search_key(attr, send(attr)),1, uuid }
|
66
|
+
uniqueness_attr.each { |attr| redis.del uniq_key(attr, send(attr)) }
|
67
|
+
end
|
68
|
+
|
69
|
+
def method_missing(method, *args, &block)
|
70
|
+
redis.send method, *args, &block
|
71
|
+
end
|
72
|
+
|
73
|
+
def serialize(object)
|
74
|
+
serializer.dump(object)
|
75
|
+
end
|
76
|
+
|
77
|
+
def deserialize(stringified)
|
78
|
+
serializer.restore stringified
|
79
|
+
end
|
80
|
+
|
81
|
+
private
|
82
|
+
|
83
|
+
def initialize(opt)
|
84
|
+
@redis = opt[:connection]
|
85
|
+
@serializer = opt[:serializer] || Marshal
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module RelationalRedisMapper
|
2
|
+
class Relation
|
3
|
+
|
4
|
+
extend Forwardable
|
5
|
+
include Enumerable
|
6
|
+
|
7
|
+
attr_reader :subject, :relation_sym, :order_by_method
|
8
|
+
def_delegators :relation_klass, :find_all_by_ids, :get_all_ids, :find
|
9
|
+
|
10
|
+
def initialize(subject, relation_sym, order_by_method=nil)
|
11
|
+
@subject = subject
|
12
|
+
@relation_sym = relation_sym
|
13
|
+
@order_by_method = order_by_method
|
14
|
+
end
|
15
|
+
|
16
|
+
def << relation
|
17
|
+
$redis.zadd(relation_key, score(relation), relation.id)
|
18
|
+
all
|
19
|
+
end
|
20
|
+
|
21
|
+
def all
|
22
|
+
find_all_by_ids all_relation_ids
|
23
|
+
end
|
24
|
+
|
25
|
+
def where(attr, val)
|
26
|
+
ids = all_relation_ids.select do |id|
|
27
|
+
get_all_ids(attr, val).include?(id)
|
28
|
+
end
|
29
|
+
find_all_by_ids ids
|
30
|
+
end
|
31
|
+
|
32
|
+
def first
|
33
|
+
find all_relation_ids(0,0)
|
34
|
+
end
|
35
|
+
|
36
|
+
def last
|
37
|
+
find all_relation_ids(-1,-1)
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
def relation_key
|
42
|
+
"#{subject.class_key}:#{relation_klass.class_key}:#{subject.id}"
|
43
|
+
end
|
44
|
+
def all_relation_ids(start=0, stop=-1)
|
45
|
+
$redis.zrange(relation_key, start, stop)
|
46
|
+
end
|
47
|
+
def score(object)
|
48
|
+
order_by_method ? object.send(order_by_method) : 0
|
49
|
+
end
|
50
|
+
def relation_klass
|
51
|
+
relation_sym.to_s.classify.constantize
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module RelationalRedisMapper
|
2
|
+
module UtilityMethods
|
3
|
+
|
4
|
+
def klass
|
5
|
+
self.class
|
6
|
+
end
|
7
|
+
|
8
|
+
def ==(other)
|
9
|
+
self.to_hash == other.to_hash
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_hash
|
13
|
+
instance_variables.each_with_object({}) do |var, hash|
|
14
|
+
hash[var] = instance_variable_get(var)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def dup
|
19
|
+
dup = super; dup.id = nil; dup
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'relational_redis_mapper/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "relational_redis_mapper"
|
8
|
+
spec.version = RelationalRedisMapper::VERSION
|
9
|
+
spec.authors = ["Aaron Weisberg"]
|
10
|
+
spec.email = ["aaronweisberg@gmail.com"]
|
11
|
+
spec.summary = %q{Allows for relational mapping using redis}
|
12
|
+
spec.description = %q{ActiveRecord like interface backed by the blazing fast Redis.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_dependency 'redis', '~> 3.1.0'
|
22
|
+
spec.add_dependency 'i18n', '~> 0.6.11'
|
23
|
+
spec.add_dependency 'activesupport-inflector', '~> 0.1.0'
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec"
|
27
|
+
spec.add_development_dependency "pry"
|
28
|
+
end
|
@@ -0,0 +1,149 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'relational_redis_mapper'
|
3
|
+
|
4
|
+
module HashInit
|
5
|
+
def initialize(opt={})
|
6
|
+
opt.each { |k,v| send "#{k}=", v }
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
class Auction
|
11
|
+
include RelationalRedisMapper
|
12
|
+
include HashInit
|
13
|
+
attr_accessor :name, :time, :product
|
14
|
+
validate_uniqueness_of :name
|
15
|
+
query_attr :name, :time
|
16
|
+
has_many_ordered bids: :price
|
17
|
+
#has_many :people
|
18
|
+
|
19
|
+
def timestamp
|
20
|
+
time.to_i
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
class Product
|
25
|
+
include HashInit
|
26
|
+
include RelationalRedisMapper
|
27
|
+
attr_accessor :name, :price, :available
|
28
|
+
validate_uniqueness_of :name
|
29
|
+
query_attr :name, :price
|
30
|
+
has_many_ordered auctions: :timestamp
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
describe Product do
|
35
|
+
|
36
|
+
describe 'Products' do
|
37
|
+
|
38
|
+
let(:macbook) { Product.new(name: 'MacBook', price: 1999.99) }
|
39
|
+
let(:iphone) { Product.new(name: 'Iphone', price: 1999.99) }
|
40
|
+
let(:auction) { Auction.new(name: 'Second Macbook auction', time: Time.new(2007,11,10)) }
|
41
|
+
|
42
|
+
before do
|
43
|
+
@redis = RelationalRedisMapper::Redis.set
|
44
|
+
@redis.flushall; macbook.save; iphone.save
|
45
|
+
|
46
|
+
@macbook = Product.new(name: 'MacBook_2', price: 1999.99)
|
47
|
+
|
48
|
+
5.times do |x|
|
49
|
+
time = Time.new(2000 + x,1,1)
|
50
|
+
auction = instance_variable_set "@auction_#{x}", Auction.new(name: "Macbook #{x}", time: time)
|
51
|
+
auction.save
|
52
|
+
@macbook.auctions << auction
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
context "regular queries" do
|
58
|
+
it 'should return first by name or price' do
|
59
|
+
by_name = Product.find_by_name macbook.name
|
60
|
+
by_price = Product.find_by_name iphone.name
|
61
|
+
expect(by_name.name).to eq 'MacBook'
|
62
|
+
expect(by_price.name).to eq 'Iphone'
|
63
|
+
end
|
64
|
+
|
65
|
+
it 'should return nil when no match is found'do
|
66
|
+
by_price = Product.find_by_price 123
|
67
|
+
expect(by_price).to be_nil
|
68
|
+
end
|
69
|
+
|
70
|
+
it 'should return all results by price' do
|
71
|
+
all_price = Product.where :price, 1999.99
|
72
|
+
expect(all_price).to include macbook, iphone
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'should return emtpy array when no match is found' do
|
76
|
+
by_price = Product.where :price, 123
|
77
|
+
expect(by_price).to be_empty
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'should get all products' do
|
81
|
+
expect(Product.all).to include iphone, macbook
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
context 'relational queries' do
|
86
|
+
|
87
|
+
it 'should return multiple associations' do
|
88
|
+
auctions = @macbook.auctions.all
|
89
|
+
expect(auctions).to include @auction_1, @auction_2, @auction_3, @auction_4
|
90
|
+
expect(auctions).not_to include auction
|
91
|
+
expect(iphone.auctions.all).to be_empty
|
92
|
+
end
|
93
|
+
|
94
|
+
it 'should return earliest auction associated to product' do
|
95
|
+
expect(@macbook.auctions.first).to eq @auction_0
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'should return latest auction associated to product' do
|
99
|
+
expect(@macbook.auctions.last).to eq @auction_4
|
100
|
+
end
|
101
|
+
|
102
|
+
it 'should query assocation' do
|
103
|
+
query_by_assoc = @macbook.auctions.where(:name, @auction_0.name)
|
104
|
+
other = iphone.auctions.where(:name, @auction_0.name)
|
105
|
+
expect(query_by_assoc).to eq [@auction_0]
|
106
|
+
expect(other).to be_empty
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
context 'persistance' do
|
111
|
+
|
112
|
+
context 'delete' do
|
113
|
+
before { macbook.delete }
|
114
|
+
|
115
|
+
it 'should delete an object' do
|
116
|
+
persisted = @redis.hget macbook.class_key, macbook.id
|
117
|
+
expect(persisted).to be_nil
|
118
|
+
end
|
119
|
+
|
120
|
+
it 'should remove the indexes' do
|
121
|
+
existing_indices = macbook.uniqueness_attr.select do |attr|
|
122
|
+
@redis.find_id macbook.key_gen.query_key(attr, macbook.send(attr))
|
123
|
+
end
|
124
|
+
expect(existing_indices).to be_empty
|
125
|
+
end
|
126
|
+
|
127
|
+
end
|
128
|
+
|
129
|
+
it 'should not save same item twice' do
|
130
|
+
expect{ macbook.save }.to raise_exception
|
131
|
+
end
|
132
|
+
|
133
|
+
it 'should update item' do
|
134
|
+
old_name = macbook.name; new_name = 'Macbook Pro'
|
135
|
+
macbook.name = new_name; macbook.save
|
136
|
+
expect(Product.find_by_name(old_name)).to be_nil
|
137
|
+
expect(Product.find_by_name(new_name).name).to eq 'Macbook Pro'
|
138
|
+
end
|
139
|
+
|
140
|
+
it 'should not save two items with same name' do
|
141
|
+
b = macbook.dup
|
142
|
+
expect{ b.save }.to raise_exception
|
143
|
+
end
|
144
|
+
|
145
|
+
end
|
146
|
+
|
147
|
+
end
|
148
|
+
|
149
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause this
|
4
|
+
# file to always be loaded, without a need to explicitly require it in any files.
|
5
|
+
#
|
6
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
7
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
8
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
9
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
10
|
+
# a separate helper file that requires the additional dependencies and performs
|
11
|
+
# the additional setup, and require it from the spec files that actually need it.
|
12
|
+
#
|
13
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
14
|
+
# users commonly want.
|
15
|
+
#
|
16
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
17
|
+
RSpec.configure do |config|
|
18
|
+
# rspec-expectations config goes here. You can use an alternate
|
19
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
20
|
+
# assertions if you prefer.
|
21
|
+
config.expect_with :rspec do |expectations|
|
22
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
23
|
+
# and `failure_message` of custom matchers include text for helper methods
|
24
|
+
# defined using `chain`, e.g.:
|
25
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
26
|
+
# # => "be bigger than 2 and smaller than 4"
|
27
|
+
# ...rather than:
|
28
|
+
# # => "be bigger than 2"
|
29
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
30
|
+
end
|
31
|
+
|
32
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
33
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
34
|
+
config.mock_with :rspec do |mocks|
|
35
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
36
|
+
# a real object. This is generally recommended, and will default to
|
37
|
+
# `true` in RSpec 4.
|
38
|
+
mocks.verify_partial_doubles = true
|
39
|
+
end
|
40
|
+
|
41
|
+
# The settings below are suggested to provide a good initial experience
|
42
|
+
# with RSpec, but feel free to customize to your heart's content.
|
43
|
+
=begin
|
44
|
+
# These two settings work together to allow you to limit a spec run
|
45
|
+
# to individual examples or groups you care about by tagging them with
|
46
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
47
|
+
# get run.
|
48
|
+
config.filter_run :focus
|
49
|
+
config.run_all_when_everything_filtered = true
|
50
|
+
|
51
|
+
# Limits the available syntax to the non-monkey patched syntax that is recommended.
|
52
|
+
# For more details, see:
|
53
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
54
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
55
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
56
|
+
config.disable_monkey_patching!
|
57
|
+
|
58
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
59
|
+
# be too noisy due to issues in dependencies.
|
60
|
+
config.warnings = true
|
61
|
+
|
62
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
63
|
+
# file, and it's useful to allow more verbose output when running an
|
64
|
+
# individual spec file.
|
65
|
+
if config.files_to_run.one?
|
66
|
+
# Use the documentation formatter for detailed output,
|
67
|
+
# unless a formatter has already been configured
|
68
|
+
# (e.g. via a command-line flag).
|
69
|
+
config.default_formatter = 'doc'
|
70
|
+
end
|
71
|
+
|
72
|
+
# Print the 10 slowest examples and example groups at the
|
73
|
+
# end of the spec run, to help surface which specs are running
|
74
|
+
# particularly slow.
|
75
|
+
config.profile_examples = 10
|
76
|
+
|
77
|
+
# Run specs in random order to surface order dependencies. If you find an
|
78
|
+
# order dependency and want to debug it, you can fix the order by providing
|
79
|
+
# the seed, which is printed after each run.
|
80
|
+
# --seed 1234
|
81
|
+
config.order = :random
|
82
|
+
|
83
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
84
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
85
|
+
# test failures related to randomization by passing the same `--seed` value
|
86
|
+
# as the one that triggered the failure.
|
87
|
+
Kernel.srand config.seed
|
88
|
+
=end
|
89
|
+
end
|
metadata
ADDED
@@ -0,0 +1,159 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: relational_redis_mapper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aaron Weisberg
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: redis
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.1.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.1.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: i18n
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.6.11
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.6.11
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activesupport-inflector
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.1.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.1.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ~>
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.7'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ~>
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.7'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ~>
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ! '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ! '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: pry
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ! '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ! '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: ActiveRecord like interface backed by the blazing fast Redis.
|
112
|
+
email:
|
113
|
+
- aaronweisberg@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- .gitignore
|
119
|
+
- .rspec
|
120
|
+
- Gemfile
|
121
|
+
- LICENSE.txt
|
122
|
+
- README.md
|
123
|
+
- Rakefile
|
124
|
+
- lib/relational_redis_mapper.rb
|
125
|
+
- lib/relational_redis_mapper/key_gen.rb
|
126
|
+
- lib/relational_redis_mapper/redis.rb
|
127
|
+
- lib/relational_redis_mapper/relation.rb
|
128
|
+
- lib/relational_redis_mapper/utility_methods.rb
|
129
|
+
- lib/relational_redis_mapper/version.rb
|
130
|
+
- relational_redis_mapper.gemspec
|
131
|
+
- spec/relational_redis_mapper_spec.rb
|
132
|
+
- spec/spec_helper.rb
|
133
|
+
homepage: ''
|
134
|
+
licenses:
|
135
|
+
- MIT
|
136
|
+
metadata: {}
|
137
|
+
post_install_message:
|
138
|
+
rdoc_options: []
|
139
|
+
require_paths:
|
140
|
+
- lib
|
141
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ! '>='
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
147
|
+
requirements:
|
148
|
+
- - ! '>='
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: '0'
|
151
|
+
requirements: []
|
152
|
+
rubyforge_project:
|
153
|
+
rubygems_version: 2.2.2
|
154
|
+
signing_key:
|
155
|
+
specification_version: 4
|
156
|
+
summary: Allows for relational mapping using redis
|
157
|
+
test_files:
|
158
|
+
- spec/relational_redis_mapper_spec.rb
|
159
|
+
- spec/spec_helper.rb
|