Skip to content

Commit

Permalink
added info to README, added MIT license, cosmetic changes in tests' b…
Browse files Browse the repository at this point in the history
…efore/after hooks
  • Loading branch information
Dmitrii Samoilov committed Jun 2, 2011
1 parent e715020 commit c04d72f
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 64 deletions.
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (C) 2011 by Dmitrii Samoilov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
72 changes: 71 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,21 @@ end
```

Following property types are supported:

* **Integer**

* **String**

* **Float**

* **RedisOrm::Boolean**

* **Time**

Attribute definition supports following options:

* **:default**

The default value of the attribute when it's getting saved w/o any.

## Searching records by the value
Expand Down Expand Up @@ -65,7 +72,7 @@ Photo.all(:order => "desc", :limit => 10, :offset => 50)

Indices are used in a different way then they are used in relational databases.

You could add index to any attribute of the model. Index could be compaund:
You could add index to any attribute of the model (it also could be compound):

```ruby
class User < RedisOrm::Base
Expand All @@ -76,17 +83,80 @@ class User < RedisOrm::Base
end
```

With index defined for the property (or number of properties) the id of the saved object is stored in the special sorted set, so it could be found later. For example with defined User model for the above code:

```ruby

```

Index definition supports following options:

* **:unique** Boolean default: false

## Associations

## Validation

RedisOrm includes ActiveModel::Validations. So all well-known functions are already in. An example from test/validations_test.rb:

```ruby
class Photo < RedisOrm::Base
property :image, String

validates_presence_of :image
validates_length_of :image, :in => 7..32
validates_format_of :image, :with => /\w*\.(gif|jpe?g|png)/
end
```

## Callbacks

RedisOrm provides 6 standard callbacks:

```ruby
after_save :callback
before_save :callback
after_create :callback
before_create :callback
after_destroy :callback
before_destroy :callback
```
They are implemented differently than in ActiveModel though work as expected.

## Saving records

When saving object to Redis new Hash is created with keys/values equal to the properties/values of the saving object. The object then

## Tests

Though I a fan of the Test::Unit all tests are based on RSpec. And the only reason I did it is before(:all) and after(:all) hooks. So I could spawn/kill redis-server's process:

```ruby
describe "check callbacks" do
before(:all) do
path_to_conf = File.dirname(File.expand_path(__FILE__)) + "/redis.conf"
$redis_pid = spawn 'redis-server ' + path_to_conf, :out => "/dev/null"
sleep(0.3) # must be some delay otherwise "Connection refused - Unable to connect to Redis"
path_to_socket = File.dirname(File.expand_path(__FILE__)) + "/../redis.sock"
$redis = Redis.new(:host => 'localhost', :path => path_to_socket)
end

before(:each) do
$redis.flushall if $redis
end

after(:each) do
$redis.flushall if $redis
end

after(:all) do
Process.kill 9, $redis_pid.to_i if $redis_pid
end

# it "should ..." do
# ...
# end
end
```

Copyright © 2011 Dmitrii Samoilov, released under the MIT license
12 changes: 5 additions & 7 deletions test/associasions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ class User < RedisOrm::Base
describe "check associations" do
before(:all) do
path_to_conf = File.dirname(File.expand_path(__FILE__)) + "/redis.conf"
$redis_pid = spawn 'redis-server ' + path_to_conf, :out=>"/dev/null"
sleep(1)
$redis_pid = spawn 'redis-server ' + path_to_conf, :out => "/dev/null"
sleep(0.3) # must be some delay otherwise "Connection refused - Unable to connect to Redis"
path_to_socket = File.dirname(File.expand_path(__FILE__)) + "/../redis.sock"
$redis = Redis.new(:host => 'localhost', :path => path_to_socket)#:port => 6379)
$redis = Redis.new(:host => 'localhost', :path => path_to_socket)
end

before(:each) do
Expand Down Expand Up @@ -68,10 +68,8 @@ class User < RedisOrm::Base
$redis.flushall if $redis
end

after(:all) do
if $redis_pid
Process.kill 9, $redis_pid.to_i
end
after(:all) do
Process.kill 9, $redis_pid.to_i if $redis_pid
end

it "should return array" do
Expand Down
10 changes: 4 additions & 6 deletions test/basic_functionality_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class DefaultUser < RedisOrm::Base
describe "check basic functionality" do
before(:all) do
path_to_conf = File.dirname(File.expand_path(__FILE__)) + "/redis.conf"
$redis_pid = spawn 'redis-server ' + path_to_conf, :out=>"/dev/null"
sleep(1)
$redis_pid = spawn 'redis-server ' + path_to_conf, :out => "/dev/null"
sleep(0.3) # must be some delay otherwise "Connection refused - Unable to connect to Redis"
path_to_socket = File.dirname(File.expand_path(__FILE__)) + "/../redis.sock"
$redis = Redis.new(:host => 'localhost', :path => path_to_socket)#:port => 6379)
$redis = Redis.new(:host => 'localhost', :path => path_to_socket)
end

before(:each) do
Expand All @@ -39,9 +39,7 @@ class DefaultUser < RedisOrm::Base
end

after(:all) do
if $redis_pid
Process.kill 9, $redis_pid.to_i
end
Process.kill 9, $redis_pid.to_i if $redis_pid
end

it "test_simple_creation" do
Expand Down
10 changes: 4 additions & 6 deletions test/callbacks_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ def after_destroy_callback
describe "check callbacks" do
before(:all) do
path_to_conf = File.dirname(File.expand_path(__FILE__)) + "/redis.conf"
$redis_pid = spawn 'redis-server ' + path_to_conf, :out=>"/dev/null"
sleep(1)
$redis_pid = spawn 'redis-server ' + path_to_conf, :out => "/dev/null"
sleep(0.3) # must be some delay otherwise "Connection refused - Unable to connect to Redis"
path_to_socket = File.dirname(File.expand_path(__FILE__)) + "/../redis.sock"
$redis = Redis.new(:host => 'localhost', :path => path_to_socket)#:port => 6379)
$redis = Redis.new(:host => 'localhost', :path => path_to_socket)
end

before(:each) do
Expand All @@ -89,9 +89,7 @@ def after_destroy_callback
end

after(:all) do
if $redis_pid
Process.kill 9, $redis_pid.to_i
end
Process.kill 9, $redis_pid.to_i if $redis_pid
end

it "should fire after_create/after_destroy callbacks" do
Expand Down
12 changes: 5 additions & 7 deletions test/changes_array_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ class User < RedisOrm::Base
describe "check associations" do
before(:all) do
path_to_conf = File.dirname(File.expand_path(__FILE__)) + "/redis.conf"
$redis_pid = spawn 'redis-server ' + path_to_conf, :out=>"/dev/null"
sleep(1)
$redis_pid = spawn 'redis-server ' + path_to_conf, :out => "/dev/null"
sleep(0.3) # must be some delay otherwise "Connection refused - Unable to connect to Redis"
path_to_socket = File.dirname(File.expand_path(__FILE__)) + "/../redis.sock"
$redis = Redis.new(:host => 'localhost', :path => path_to_socket)#:port => 6379)
$redis = Redis.new(:host => 'localhost', :path => path_to_socket)
end

before(:each) do
Expand All @@ -24,10 +24,8 @@ class User < RedisOrm::Base
$redis.flushall if $redis
end

after(:all) do
if $redis_pid
Process.kill 9, $redis_pid.to_i
end
after(:all) do
Process.kill 9, $redis_pid.to_i if $redis_pid
end

it "should return correct _changes array" do
Expand Down
12 changes: 5 additions & 7 deletions test/dynamic_finders_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ class CustomUser < RedisOrm::Base
describe "check associations" do
before(:all) do
path_to_conf = File.dirname(File.expand_path(__FILE__)) + "/redis.conf"
$redis_pid = spawn 'redis-server ' + path_to_conf, :out=>"/dev/null"
sleep(1)
$redis_pid = spawn 'redis-server ' + path_to_conf, :out => "/dev/null"
sleep(0.3) # must be some delay otherwise "Connection refused - Unable to connect to Redis"
path_to_socket = File.dirname(File.expand_path(__FILE__)) + "/../redis.sock"
$redis = Redis.new(:host => 'localhost', :path => path_to_socket)#:port => 6379)
$redis = Redis.new(:host => 'localhost', :path => path_to_socket)
end

before(:each) do
Expand All @@ -36,10 +36,8 @@ class CustomUser < RedisOrm::Base
$redis.flushall if $redis
end

after(:all) do
if $redis_pid
Process.kill 9, $redis_pid.to_i
end
after(:all) do
Process.kill 9, $redis_pid.to_i if $redis_pid
end

it "should create and use indexes to implement dynamic finders" do
Expand Down
10 changes: 4 additions & 6 deletions test/exceptions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class Jigsaw < RedisOrm::Base
describe "exceptions test" do
before(:all) do
path_to_conf = File.dirname(File.expand_path(__FILE__)) + "/redis.conf"
$redis_pid = spawn 'redis-server ' + path_to_conf, :out=>"/dev/null"
sleep(1)
$redis_pid = spawn 'redis-server ' + path_to_conf, :out => "/dev/null"
sleep(0.3) # must be some delay otherwise "Connection refused - Unable to connect to Redis"
path_to_socket = File.dirname(File.expand_path(__FILE__)) + "/../redis.sock"
$redis = Redis.new(:host => 'localhost', :path => path_to_socket)#:port => 6379)
$redis = Redis.new(:host => 'localhost', :path => path_to_socket)
end

before(:each) do
Expand All @@ -39,9 +39,7 @@ class Jigsaw < RedisOrm::Base
end

after(:all) do
if $redis_pid
Process.kill 9, $redis_pid.to_i
end
Process.kill 9, $redis_pid.to_i if $redis_pid
end

it "should raise an exception if association is provided with improper class" do
Expand Down
13 changes: 4 additions & 9 deletions test/has_one_has_many_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ class Location < RedisOrm::Base
describe "check associations" do
before(:all) do
path_to_conf = File.dirname(File.expand_path(__FILE__)) + "/redis.conf"
$redis_pid = spawn 'redis-server ' + path_to_conf, :out=>"/dev/null"
sleep(1)
puts 'started - ' + $redis_pid.to_s
$redis_pid = spawn 'redis-server ' + path_to_conf, :out => "/dev/null"
sleep(0.3) # must be some delay otherwise "Connection refused - Unable to connect to Redis"
path_to_socket = File.dirname(File.expand_path(__FILE__)) + "/../redis.sock"
puts 'path_to_socket - ' + path_to_socket.inspect
$redis = Redis.new(:host => 'localhost', :path => path_to_socket)#:port => 6379)
$redis = Redis.new(:host => 'localhost', :path => path_to_socket)
end

before(:each) do
Expand All @@ -33,10 +31,7 @@ class Location < RedisOrm::Base
end

after(:all) do
puts 'finish - ' + $redis_pid.to_s
if $redis_pid
Process.kill 9, $redis_pid.to_i
end
Process.kill 9, $redis_pid.to_i if $redis_pid
end

it "should save associations properly" do
Expand Down
10 changes: 4 additions & 6 deletions test/indices_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ class User < RedisOrm::Base
describe "check indices" do
before(:all) do
path_to_conf = File.dirname(File.expand_path(__FILE__)) + "/redis.conf"
$redis_pid = spawn 'redis-server ' + path_to_conf, :out=>"/dev/null"
sleep(1)
$redis_pid = spawn 'redis-server ' + path_to_conf, :out => "/dev/null"
sleep(0.3) # must be some delay otherwise "Connection refused - Unable to connect to Redis"
path_to_socket = File.dirname(File.expand_path(__FILE__)) + "/../redis.sock"
$redis = Redis.new(:host => 'localhost', :path => path_to_socket)#:port => 6379)
$redis = Redis.new(:host => 'localhost', :path => path_to_socket)
end

before(:each) do
Expand All @@ -28,9 +28,7 @@ class User < RedisOrm::Base
end

after(:all) do
if $redis_pid
Process.kill 9, $redis_pid.to_i
end
Process.kill 9, $redis_pid.to_i if $redis_pid
end

it "should change index accordingly to the changes in the model" do
Expand Down
10 changes: 4 additions & 6 deletions test/options_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,14 @@ class User < RedisOrm::Base
describe "test options" do
before(:all) do
path_to_conf = File.dirname(File.expand_path(__FILE__)) + "/redis.conf"
$redis_pid = spawn 'redis-server ' + path_to_conf, :out=>"/dev/null"
sleep(1)
$redis_pid = spawn 'redis-server ' + path_to_conf, :out => "/dev/null"
sleep(0.3) # must be some delay otherwise "Connection refused - Unable to connect to Redis"
path_to_socket = File.dirname(File.expand_path(__FILE__)) + "/../redis.sock"
$redis = Redis.new(:host => 'localhost', :path => path_to_socket)#:port => 6379)
$redis = Redis.new(:host => 'localhost', :path => path_to_socket)
end

after(:all) do
if $redis_pid
Process.kill 9, $redis_pid.to_i
end
Process.kill 9, $redis_pid.to_i if $redis_pid
end

after(:each) do
Expand Down
6 changes: 3 additions & 3 deletions test/validations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class Photo < RedisOrm::Base
describe "check associations" do
before(:all) do
path_to_conf = File.dirname(File.expand_path(__FILE__)) + "/redis.conf"
$redis_pid = spawn 'redis-server ' + path_to_conf, :out=>"/dev/null"
sleep(1)
$redis_pid = spawn 'redis-server ' + path_to_conf, :out => "/dev/null"
sleep(0.3) # must be some delay otherwise "Connection refused - Unable to connect to Redis"
path_to_socket = File.dirname(File.expand_path(__FILE__)) + "/../redis.sock"
$redis = Redis.new(:host => 'localhost', :path => path_to_socket)#:port => 6379)
$redis = Redis.new(:host => 'localhost', :path => path_to_socket)
end

after(:all) do
Expand Down

0 comments on commit c04d72f

Please sign in to comment.