redis-orm 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +4 -0
- data/README.md +189 -0
- data/Rakefile +1 -0
- data/lib/redis-orm.rb +32 -0
- data/lib/redis/actions.rb +21 -0
- data/lib/redis/actions/creating.rb +31 -0
- data/lib/redis/actions/destroying.rb +39 -0
- data/lib/redis/actions/finding.rb +26 -0
- data/lib/redis/actions/saving.rb +47 -0
- data/lib/redis/actions/timestamps.rb +19 -0
- data/lib/redis/actions/updating.rb +18 -0
- data/lib/redis/orm.rb +59 -0
- data/lib/redis/orm/attributes.rb +65 -0
- data/lib/redis/orm/version.rb +14 -0
- data/lib/redis/relations.rb +22 -0
- data/lib/redis/relations/belongs_to.rb +51 -0
- data/lib/redis/relations/has_many.rb +60 -0
- data/lib/redis/relations/has_one.rb +51 -0
- data/lib/redis/validations.rb +9 -0
- data/lib/redis/validations/uniqueness.rb +41 -0
- data/redis-orm.gemspec +25 -0
- data/spec/redis/actions/creating_spec.rb +32 -0
- data/spec/redis/actions/destroying_spec.rb +27 -0
- data/spec/redis/actions/finding_spec.rb +26 -0
- data/spec/redis/actions/timestamps_spec.rb +11 -0
- data/spec/redis/actions/updating_spec.rb +32 -0
- data/spec/redis/orm_spec.rb +56 -0
- data/spec/redis/relations/belongs_to_spec.rb +54 -0
- data/spec/redis/relations/has_many_spec.rb +33 -0
- data/spec/redis/relations/has_one_spec.rb +23 -0
- data/spec/redis/relations/interop_spec.rb +45 -0
- data/spec/redis/validations/uniqueness_spec.rb +32 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/support/active_model_lint.rb +20 -0
- data/spec/support/redis_helper.rb +27 -0
- data/test.rb +23 -0
- metadata +136 -0
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Redis::Actions::Finding do
|
4
|
+
include RedisHelper
|
5
|
+
|
6
|
+
context "with several pre-existing records" do
|
7
|
+
orm_class { attribute :random }
|
8
|
+
|
9
|
+
before(:each) do
|
10
|
+
10.times { |i| orm_class.new(:random => i).save! }
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should find all of them" do
|
14
|
+
all = orm_class.all
|
15
|
+
all.length.should == 10
|
16
|
+
10.times { |i| all[i].random.should == i }
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context "with key not matching known constants" do
|
21
|
+
it "should find record with its own class" do
|
22
|
+
orm_class.create!(:key => '1234')
|
23
|
+
orm_class.find('1234').should be_kind_of(orm_class)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
describe Redis::Actions::Timestamps do
|
2
|
+
include RedisHelper
|
3
|
+
|
4
|
+
context "when record is created" do
|
5
|
+
it "should set created_at and updated_at" do
|
6
|
+
instance = orm_class.create!
|
7
|
+
instance.created_at.should_not be_blank
|
8
|
+
instance.updated_at.should_not be_blank
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Redis::Actions::Updating do
|
4
|
+
include RedisHelper
|
5
|
+
|
6
|
+
orm_class { attribute :number }
|
7
|
+
|
8
|
+
it "should fire update callbacks once, and create callbacks once" do
|
9
|
+
before = { :create => 0, :update => 0, :save => 0 }
|
10
|
+
after = { :create => 0, :update => 0, :save => 0 }
|
11
|
+
orm_class do
|
12
|
+
before_save { before[:save] += 1 }
|
13
|
+
after_save { after[:save] += 1 }
|
14
|
+
before_create { before[:create] += 1 }
|
15
|
+
after_create { after[:create] += 1 }
|
16
|
+
before_update { before[:update] += 1 }
|
17
|
+
after_update { after[:update] += 1 }
|
18
|
+
end
|
19
|
+
|
20
|
+
orm = orm_class.new
|
21
|
+
orm.save
|
22
|
+
orm.number = 2
|
23
|
+
orm.save
|
24
|
+
|
25
|
+
before[:save].should == 2
|
26
|
+
after[:save].should == 2
|
27
|
+
before[:create].should == 1
|
28
|
+
before[:update].should == 1
|
29
|
+
after[:create].should == 1
|
30
|
+
after[:update].should == 1
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Redis::ORM do
|
4
|
+
include RedisHelper
|
5
|
+
it_should_behave_like "ActiveModel"
|
6
|
+
|
7
|
+
it "should have an attributes hash" do
|
8
|
+
subject.attributes.should be_kind_of(Hash)
|
9
|
+
end
|
10
|
+
|
11
|
+
context "with attributes" do
|
12
|
+
orm_class { attribute :one, 1 }
|
13
|
+
subject { orm_class.new }
|
14
|
+
|
15
|
+
it "should set attributes" do
|
16
|
+
subject.attributes = { :one => 2 }
|
17
|
+
subject.one.should == 2
|
18
|
+
end
|
19
|
+
|
20
|
+
it "should assign defaults" do
|
21
|
+
subject.one.should == 1
|
22
|
+
end
|
23
|
+
|
24
|
+
context "changing" do
|
25
|
+
before(:each) { subject.one = 2 }
|
26
|
+
|
27
|
+
it "should be changed" do
|
28
|
+
subject.should be_changed
|
29
|
+
end
|
30
|
+
|
31
|
+
context "and saving" do
|
32
|
+
before(:each) { subject.save.should == true }
|
33
|
+
|
34
|
+
it "should not be new" do
|
35
|
+
subject.should_not be_new_record
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should not be changed" do
|
39
|
+
subject.should_not be_changed
|
40
|
+
end
|
41
|
+
|
42
|
+
it "should have a key" do
|
43
|
+
subject.key.should_not be_nil
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should be find-able" do
|
47
|
+
orm_class.find(subject.key).should == subject
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should find separate instances" do
|
51
|
+
orm_class.find(subject.key).should_not be(subject)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Redis::Relations::BelongsTo do
|
4
|
+
include RedisHelper
|
5
|
+
|
6
|
+
orm_class { belongs_to :other }
|
7
|
+
subject { orm_class.new }
|
8
|
+
|
9
|
+
it "should save the relation" do
|
10
|
+
other = orm_class.new
|
11
|
+
other.save!
|
12
|
+
subject.other = other
|
13
|
+
subject.save!
|
14
|
+
|
15
|
+
orm_class.find(subject.key).other.should be_kind_of(orm_class)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "mass assignment" do
|
19
|
+
other = orm_class.new
|
20
|
+
other.save!
|
21
|
+
subject = orm_class.create!(:other => other)
|
22
|
+
subject.other.should be(other)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "should save without relatives" do
|
26
|
+
subject.save!
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
|
31
|
+
=begin
|
32
|
+
|
33
|
+
class Aye
|
34
|
+
has_many bees(, :foreign_key => :aye_id)
|
35
|
+
end
|
36
|
+
|
37
|
+
class Bee
|
38
|
+
(attribute :aye_id)
|
39
|
+
belongs_to aye
|
40
|
+
end
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
# reference built on the relation (foreign_key):
|
45
|
+
# has_many uses its own name while belongs_to uses
|
46
|
+
# the reference name
|
47
|
+
|
48
|
+
references/ayes
|
49
|
+
Aye/1 => [ Bee/1, Bee/2 ] # has_many stores an array of keys
|
50
|
+
Bee/1 => Aye/1 # belongs_to stores a single key
|
51
|
+
Bee/2 => Aye/1
|
52
|
+
|
53
|
+
|
54
|
+
=end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Redis::Relations::HasMany do
|
4
|
+
include RedisHelper
|
5
|
+
|
6
|
+
orm_class { has_many :others }
|
7
|
+
subject { orm_class.new }
|
8
|
+
|
9
|
+
it "should save the relations" do
|
10
|
+
10.times do
|
11
|
+
other = orm_class.new
|
12
|
+
other.save!
|
13
|
+
subject.others << other
|
14
|
+
end
|
15
|
+
subject.save!
|
16
|
+
|
17
|
+
orm_class.find(subject.key).others.should have(10).items
|
18
|
+
for i in 0...10
|
19
|
+
orm_class.find(subject.key).others[i].should be_kind_of(orm_class)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
it "mass assignment" do
|
24
|
+
other = orm_class.new
|
25
|
+
other.save!
|
26
|
+
subject = orm_class.create!(:others => [other])
|
27
|
+
subject.others.first.should be(other)
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should save without relatives" do
|
31
|
+
subject.save!
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Redis::Relations::HasOne do
|
4
|
+
include RedisHelper
|
5
|
+
|
6
|
+
orm_class { has_one :other }
|
7
|
+
subject { orm_class.new }
|
8
|
+
|
9
|
+
it "should save the relations" do
|
10
|
+
other = orm_class.create
|
11
|
+
subject.other = other
|
12
|
+
subject.save!
|
13
|
+
|
14
|
+
orm_class.find(subject.key).other.should be_kind_of(orm_class)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "mass assignment" do
|
18
|
+
other = orm_class.new
|
19
|
+
other.save!
|
20
|
+
subject = orm_class.create!(:other => other)
|
21
|
+
subject.other.should be(other)
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "relation interoperability:" do
|
4
|
+
include RedisHelper
|
5
|
+
|
6
|
+
describe "belongs_to and has_one" do
|
7
|
+
orm_class { has_one :other, :relation => :original; belongs_to :original }
|
8
|
+
subject { orm_class.new }
|
9
|
+
|
10
|
+
it "should populate the reverse association" do
|
11
|
+
other = orm_class.create
|
12
|
+
subject.other = other
|
13
|
+
subject.save!
|
14
|
+
|
15
|
+
other.original.should == subject
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "belongs_to and has_many" do
|
20
|
+
orm_class { has_many :others, :relation => :original; belongs_to :original }
|
21
|
+
subject { orm_class.new }
|
22
|
+
|
23
|
+
it "should populate the reverse association" do
|
24
|
+
other = orm_class.create
|
25
|
+
subject.others << other
|
26
|
+
subject.save!
|
27
|
+
|
28
|
+
other.original.should == subject
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should not set callbacks cross subclasses" do
|
33
|
+
saved = false
|
34
|
+
|
35
|
+
c = Class.new(orm_class) { after_save :saved; define_method(:saved) { saved = true }; class << self; def name; 'c'; end end }
|
36
|
+
c2= Class.new(orm_class) { class << self; def name; 'c2'; end end }
|
37
|
+
|
38
|
+
c2.new.save!
|
39
|
+
saved.should_not be_true
|
40
|
+
|
41
|
+
# sanity check
|
42
|
+
c.new.save!
|
43
|
+
saved.should be_true
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
describe Redis::Validations::Uniqueness do
|
2
|
+
include RedisHelper
|
3
|
+
|
4
|
+
context "without options" do
|
5
|
+
orm_class { attribute :name; validates_uniqueness_of :name }
|
6
|
+
subject { orm_class.new }
|
7
|
+
|
8
|
+
it "should validate" do
|
9
|
+
subject.name = "one"
|
10
|
+
subject.save!
|
11
|
+
|
12
|
+
other = orm_class.new
|
13
|
+
other.name = "one"
|
14
|
+
other.should_not be_valid
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should not reject its own values" do
|
18
|
+
subject.name = "one"
|
19
|
+
subject.save!
|
20
|
+
subject.should be_valid
|
21
|
+
end
|
22
|
+
|
23
|
+
it "should not leave keys in use when they change" do
|
24
|
+
subject.name = "one"
|
25
|
+
subject.save!
|
26
|
+
subject.name = "two"
|
27
|
+
subject.save!
|
28
|
+
|
29
|
+
orm_class.new(:name => "one").should be_valid
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$:.unshift File.expand_path("../lib", File.dirname(__FILE__))
|
2
|
+
require 'redis/orm'
|
3
|
+
|
4
|
+
Dir[File.expand_path('support/**/*.rb', File.dirname(__FILE__))].each { |f| require f }
|
5
|
+
|
6
|
+
RSpec.configure do |c|
|
7
|
+
c.before(:each) do
|
8
|
+
Redis.connection.select "redis-orm-testing"
|
9
|
+
Redis.connection.flushdb
|
10
|
+
Redis::ORM.serializer = YAML
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'active_model'
|
2
|
+
|
3
|
+
shared_examples_for "ActiveModel" do
|
4
|
+
require 'test/unit/assertions'
|
5
|
+
require 'active_model/lint'
|
6
|
+
include Test::Unit::Assertions
|
7
|
+
include ActiveModel::Lint::Tests
|
8
|
+
|
9
|
+
# to_s is to support ruby-1.9
|
10
|
+
ActiveModel::Lint::Tests.public_instance_methods.map{|m| m.to_s}.grep(/^test/).each do |m|
|
11
|
+
example m.gsub('_',' ') do
|
12
|
+
send m
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def model
|
17
|
+
subject
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module RedisHelper
|
2
|
+
def connection
|
3
|
+
@connection ||= Redis.connection
|
4
|
+
end
|
5
|
+
|
6
|
+
alias redis connection
|
7
|
+
|
8
|
+
def self.included(base)
|
9
|
+
class << base
|
10
|
+
def orm_class(name = "Klass", &block)
|
11
|
+
before(:each) { orm_class(name, &block) }
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def orm_class(name = "Klass", &block)
|
17
|
+
@orm_class ||= begin
|
18
|
+
klass = Class.new(Redis::ORM)
|
19
|
+
def klass.name; @name; end
|
20
|
+
def klass.name=(n); @name = n; end
|
21
|
+
klass.name = name;
|
22
|
+
klass
|
23
|
+
end
|
24
|
+
@orm_class.send(:class_eval, &block) if block_given?
|
25
|
+
@orm_class
|
26
|
+
end
|
27
|
+
end
|
data/test.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
$:.unshift "./lib"
|
2
|
+
require 'redis/orm'
|
3
|
+
|
4
|
+
class A < Redis::ORM
|
5
|
+
attribute :index
|
6
|
+
has_many :others
|
7
|
+
end
|
8
|
+
|
9
|
+
a1 = A.new(:index => 1)
|
10
|
+
a2 = A.new(:index => 2)
|
11
|
+
a3 = A.new(:index => 3)
|
12
|
+
|
13
|
+
a3.save!
|
14
|
+
a2.save!
|
15
|
+
a1.others << a2 << a3
|
16
|
+
a1.save!
|
17
|
+
|
18
|
+
p a1.key
|
19
|
+
p A.find(a1.key), A.find(a1.key).others
|
20
|
+
|
21
|
+
puts A.all.collect { |a| a.inspect }
|
22
|
+
|
23
|
+
A.destroy_all
|
metadata
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: redis-orm
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.0.1
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Colin MacKenzie IV
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2011-08-26 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ~>
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 2.6.0
|
24
|
+
type: :development
|
25
|
+
version_requirements: *id001
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: redis
|
28
|
+
prerelease: false
|
29
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
30
|
+
none: false
|
31
|
+
requirements:
|
32
|
+
- - ~>
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: 2.2.2
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id002
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: activemodel
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 3.0.10
|
46
|
+
type: :runtime
|
47
|
+
version_requirements: *id003
|
48
|
+
description: The goal of this project is to provide ORM for Redis that feels very similar to ActiveRecord.
|
49
|
+
email:
|
50
|
+
- sinisterchipmunk@gmail.com
|
51
|
+
executables: []
|
52
|
+
|
53
|
+
extensions: []
|
54
|
+
|
55
|
+
extra_rdoc_files: []
|
56
|
+
|
57
|
+
files:
|
58
|
+
- .gitignore
|
59
|
+
- Gemfile
|
60
|
+
- README.md
|
61
|
+
- Rakefile
|
62
|
+
- lib/redis-orm.rb
|
63
|
+
- lib/redis/actions.rb
|
64
|
+
- lib/redis/actions/creating.rb
|
65
|
+
- lib/redis/actions/destroying.rb
|
66
|
+
- lib/redis/actions/finding.rb
|
67
|
+
- lib/redis/actions/saving.rb
|
68
|
+
- lib/redis/actions/timestamps.rb
|
69
|
+
- lib/redis/actions/updating.rb
|
70
|
+
- lib/redis/orm.rb
|
71
|
+
- lib/redis/orm/attributes.rb
|
72
|
+
- lib/redis/orm/version.rb
|
73
|
+
- lib/redis/relations.rb
|
74
|
+
- lib/redis/relations/belongs_to.rb
|
75
|
+
- lib/redis/relations/has_many.rb
|
76
|
+
- lib/redis/relations/has_one.rb
|
77
|
+
- lib/redis/validations.rb
|
78
|
+
- lib/redis/validations/uniqueness.rb
|
79
|
+
- redis-orm.gemspec
|
80
|
+
- spec/redis/actions/creating_spec.rb
|
81
|
+
- spec/redis/actions/destroying_spec.rb
|
82
|
+
- spec/redis/actions/finding_spec.rb
|
83
|
+
- spec/redis/actions/timestamps_spec.rb
|
84
|
+
- spec/redis/actions/updating_spec.rb
|
85
|
+
- spec/redis/orm_spec.rb
|
86
|
+
- spec/redis/relations/belongs_to_spec.rb
|
87
|
+
- spec/redis/relations/has_many_spec.rb
|
88
|
+
- spec/redis/relations/has_one_spec.rb
|
89
|
+
- spec/redis/relations/interop_spec.rb
|
90
|
+
- spec/redis/validations/uniqueness_spec.rb
|
91
|
+
- spec/spec_helper.rb
|
92
|
+
- spec/support/active_model_lint.rb
|
93
|
+
- spec/support/redis_helper.rb
|
94
|
+
- test.rb
|
95
|
+
homepage: http://github.com/sinisterchipmunk/redis-orm
|
96
|
+
licenses: []
|
97
|
+
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: "0"
|
109
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
110
|
+
none: false
|
111
|
+
requirements:
|
112
|
+
- - ">="
|
113
|
+
- !ruby/object:Gem::Version
|
114
|
+
version: "0"
|
115
|
+
requirements: []
|
116
|
+
|
117
|
+
rubyforge_project: redis-orm
|
118
|
+
rubygems_version: 1.8.5
|
119
|
+
signing_key:
|
120
|
+
specification_version: 3
|
121
|
+
summary: Object-relational mapping for Redis
|
122
|
+
test_files:
|
123
|
+
- spec/redis/actions/creating_spec.rb
|
124
|
+
- spec/redis/actions/destroying_spec.rb
|
125
|
+
- spec/redis/actions/finding_spec.rb
|
126
|
+
- spec/redis/actions/timestamps_spec.rb
|
127
|
+
- spec/redis/actions/updating_spec.rb
|
128
|
+
- spec/redis/orm_spec.rb
|
129
|
+
- spec/redis/relations/belongs_to_spec.rb
|
130
|
+
- spec/redis/relations/has_many_spec.rb
|
131
|
+
- spec/redis/relations/has_one_spec.rb
|
132
|
+
- spec/redis/relations/interop_spec.rb
|
133
|
+
- spec/redis/validations/uniqueness_spec.rb
|
134
|
+
- spec/spec_helper.rb
|
135
|
+
- spec/support/active_model_lint.rb
|
136
|
+
- spec/support/redis_helper.rb
|