redis-objects-model 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +35 -0
- data/Rakefile +5 -0
- data/lib/redis-objects-model.rb +78 -0
- data/lib/redis-objects-model/version.rb +7 -0
- data/redis-objects-model.gemspec +25 -0
- data/spec/redis-objects-model_spec.rb +43 -0
- data/spec/spec_helper.rb +17 -0
- metadata +120 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Calvin Walton <calvin.walton@kepstin.ca>
|
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,35 @@
|
|
1
|
+
# redis-objects-model
|
2
|
+
|
3
|
+
The simplest possible Model to use with redis-objects.
|
4
|
+
|
5
|
+
The minimum requirement for a class to be usable as a model with
|
6
|
+
redis-objects is that it must provide an id attribute. So here is a
|
7
|
+
base class that provides an id attribute - lazily yet atomically
|
8
|
+
allocated using Redis, of course.
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
gem 'redis-objects-model'
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install redis-objects-model
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
See the generated rdoc, or just read the documentation headers in
|
27
|
+
lib/redis-objects-model.rb
|
28
|
+
|
29
|
+
## Contributing
|
30
|
+
|
31
|
+
1. Fork it
|
32
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
33
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
34
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
35
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
require "redis-objects-model/version"
|
2
|
+
require "redis"
|
3
|
+
require "redis/objects"
|
4
|
+
|
5
|
+
class Redis
|
6
|
+
module Objects
|
7
|
+
|
8
|
+
##
|
9
|
+
# The simplest possible Model to use with redis-objects.
|
10
|
+
#
|
11
|
+
# The minimum requirement for a class to be usable as a model with
|
12
|
+
# redis-objects is that it must provide an id attribute. So here is a
|
13
|
+
# base class that provides an id attribute - lazily yet atomically
|
14
|
+
# allocated using Redis, of course.
|
15
|
+
#
|
16
|
+
# A few convenience methods are provided to allow you to check whether an
|
17
|
+
# id has been allocated, to do idiomatic 'find' type lookups, and to
|
18
|
+
# create with immediate id allocation.
|
19
|
+
class Model
|
20
|
+
include Redis::Objects
|
21
|
+
|
22
|
+
counter :last_id, :global => true
|
23
|
+
|
24
|
+
##
|
25
|
+
# Create an instance of the model.
|
26
|
+
#
|
27
|
+
# If the +id+ parameter is not nil, the model will be for the object
|
28
|
+
# with that id. Otherwise, you are creating a new object and a new id
|
29
|
+
# will be allocated atomically as needed.
|
30
|
+
def initialize(id = nil)
|
31
|
+
@id = id
|
32
|
+
end
|
33
|
+
|
34
|
+
##
|
35
|
+
# Retrieve the id of the object.
|
36
|
+
#
|
37
|
+
# If the instance is referencing an existing object, the id of that
|
38
|
+
# object will be returned. Otherwise a new id will be allocated (and
|
39
|
+
# immediately saved).
|
40
|
+
#
|
41
|
+
# In other words, accessing the id attribute is basically equivalent to
|
42
|
+
# persisting the object.
|
43
|
+
def id
|
44
|
+
@id || @id = last_id.increment
|
45
|
+
end
|
46
|
+
|
47
|
+
#########################################################################
|
48
|
+
# :section: Convenience Methods
|
49
|
+
#########################################################################
|
50
|
+
|
51
|
+
##
|
52
|
+
# Returns true if no id has been allocated for this object yet.
|
53
|
+
def new_record?
|
54
|
+
@id.nil?
|
55
|
+
end
|
56
|
+
|
57
|
+
##
|
58
|
+
# Returns false if no id has been allocated for this object yet.
|
59
|
+
def persisted?
|
60
|
+
!new_record?
|
61
|
+
end
|
62
|
+
|
63
|
+
##
|
64
|
+
# Create a new object, persisting it immediately.
|
65
|
+
def self.create
|
66
|
+
i = self.new
|
67
|
+
i.id
|
68
|
+
i
|
69
|
+
end
|
70
|
+
|
71
|
+
##
|
72
|
+
# Look up an object by id.
|
73
|
+
def self.find(id)
|
74
|
+
self.new id
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'redis-objects-model/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "redis-objects-model"
|
8
|
+
gem.version = Redis::Objects::Model::VERSION
|
9
|
+
gem.authors = ["Calvin Walton"]
|
10
|
+
gem.email = ["calvin.walton@kepstin.ca"]
|
11
|
+
gem.description = %q{The simplest possible model for redis-objects}
|
12
|
+
gem.summary = %q{You can't use redis-objects as a model in itself, because it requires an ID attribute. So, lets add one.}
|
13
|
+
gem.homepage = "https://github.com/kepstin/redis-objects-model"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_runtime_dependency('redis-objects', ['~> 0.6.0'])
|
21
|
+
gem.add_development_dependency('fakeredis', ['~>0.4.1'])
|
22
|
+
gem.add_development_dependency('rake', ['~> 10.0.0'])
|
23
|
+
gem.add_development_dependency('rspec', ['~> 2.12.0'])
|
24
|
+
gem.add_development_dependency('rubygems-tasks', ['~> 0.2.3'])
|
25
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'fakeredis'
|
2
|
+
require 'redis-objects-model'
|
3
|
+
|
4
|
+
describe 'redis-objects-model' do
|
5
|
+
it 'should not be persisted by default' do
|
6
|
+
model = Redis::Objects::Model.new
|
7
|
+
model.should be_new_record
|
8
|
+
model.should_not be_persisted
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'should be persisted after assigning an id' do
|
12
|
+
model = Redis::Objects::Model.new
|
13
|
+
model.id.should > 0
|
14
|
+
model.should_not be_new_record
|
15
|
+
model.should be_persisted
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'should return the same id with multiple calls' do
|
19
|
+
model = Redis::Objects::Model.new
|
20
|
+
id_1 = model.id
|
21
|
+
id_2 = model.id
|
22
|
+
id_1.should == id_2
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should not create multiple models with the same id' do
|
26
|
+
model_1 = Redis::Objects::Model.new
|
27
|
+
model_2 = Redis::Objects::Model.new
|
28
|
+
model_1.id.should_not == model_2.id
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should allow being created with an assigned id' do
|
32
|
+
id = Random.rand(1..100000)
|
33
|
+
model = Redis::Objects::Model.new id
|
34
|
+
model.should be_persisted
|
35
|
+
model.id.should == id
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should be persisted immediately when called with create' do
|
39
|
+
model = Redis::Objects::Model.create
|
40
|
+
model.should be_persisted
|
41
|
+
model.id.should > 0
|
42
|
+
end
|
43
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
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
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
|
12
|
+
# Run specs in random order to surface order dependencies. If you find an
|
13
|
+
# order dependency and want to debug it, you can fix the order by providing
|
14
|
+
# the seed, which is printed after each run.
|
15
|
+
# --seed 1234
|
16
|
+
config.order = 'random'
|
17
|
+
end
|
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: redis-objects-model
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Calvin Walton
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-12 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: redis-objects
|
16
|
+
requirement: &7909780 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.6.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *7909780
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: fakeredis
|
27
|
+
requirement: &7908780 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.4.1
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *7908780
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rake
|
38
|
+
requirement: &7908060 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 10.0.0
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *7908060
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rspec
|
49
|
+
requirement: &7907180 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.12.0
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *7907180
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: rubygems-tasks
|
60
|
+
requirement: &7906040 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ~>
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: 0.2.3
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *7906040
|
69
|
+
description: The simplest possible model for redis-objects
|
70
|
+
email:
|
71
|
+
- calvin.walton@kepstin.ca
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- .rspec
|
78
|
+
- Gemfile
|
79
|
+
- LICENSE.txt
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- lib/redis-objects-model.rb
|
83
|
+
- lib/redis-objects-model/version.rb
|
84
|
+
- redis-objects-model.gemspec
|
85
|
+
- spec/redis-objects-model_spec.rb
|
86
|
+
- spec/spec_helper.rb
|
87
|
+
homepage: https://github.com/kepstin/redis-objects-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
|
+
segments:
|
100
|
+
- 0
|
101
|
+
hash: 866709580333850131
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
segments:
|
109
|
+
- 0
|
110
|
+
hash: 866709580333850131
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 1.8.11
|
114
|
+
signing_key:
|
115
|
+
specification_version: 3
|
116
|
+
summary: You can't use redis-objects as a model in itself, because it requires an
|
117
|
+
ID attribute. So, lets add one.
|
118
|
+
test_files:
|
119
|
+
- spec/redis-objects-model_spec.rb
|
120
|
+
- spec/spec_helper.rb
|