active_redis_orm 0.0.10 → 0.1.1
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.
- checksums.yaml +4 -4
- data/README.md +97 -4
- data/lib/active_redis/attributes.rb +1 -1
- data/lib/active_redis/save.rb +1 -1
- data/lib/active_redis/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 899ade6f6bc04ef66e156a453353590f4d66289b
|
4
|
+
data.tar.gz: e80082d10320bfdc323dda8673341ad443864e45
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 86ae6af995214ec4443333edc352e5ff8e4953ad8a03c0d9e7dd879947897d7f97f65c7f11915ac8e6f8c0df57a0557d0eb4756dbee7e859e18d50e193fc0f0a
|
7
|
+
data.tar.gz: 145d76d2954de1e367a315ece73bf18d36d6de24a0ba838962347aeafc10a3ddc05696f37a138c2b9e0ea9d99b24db82803cbd1d4c4c5ae24e59a441f519e080
|
data/README.md
CHANGED
@@ -1,6 +1,4 @@
|
|
1
|
-
#
|
2
|
-
|
3
|
-
TODO: Write a gem description
|
1
|
+
# ActiveRedis
|
4
2
|
|
5
3
|
## Installation
|
6
4
|
|
@@ -18,7 +16,102 @@ Or install it yourself as:
|
|
18
16
|
|
19
17
|
## Usage
|
20
18
|
|
21
|
-
|
19
|
+
In an initializer, make sure that you're using the correct connection:
|
20
|
+
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
ActiveRedis.redis = Redis.new(connection_details)
|
24
|
+
```
|
25
|
+
|
26
|
+
Creating an ActiveRedis class looks like a combination of ActiveRecord and Mongoid:
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
class User < ActiveRedis::Base
|
30
|
+
field :username, finder_field: true
|
31
|
+
field :name
|
32
|
+
field :birthday, type: :date
|
33
|
+
field :age, type: :integer, default: lambda{|user| user.age_from_birthday}
|
34
|
+
field :things, type: :set
|
35
|
+
end
|
36
|
+
```
|
37
|
+
|
38
|
+
Declaring fields is similar to Mongoid - you declare the field, and its type (the default is string). We use symbols for type, unlike Mongoid which uses the data type's class.
|
39
|
+
|
40
|
+
Available types are: :string, :set, :sorted_set, :list, :hash, :float, :integer (or :int), :counter, :boolean, :date, :time (or :datetime, :timestamp).
|
41
|
+
|
42
|
+
You can declare a default value with either a value or a lambda.
|
43
|
+
|
44
|
+
|
45
|
+
### Finder Fields
|
46
|
+
|
47
|
+
When you want to be able to find an object by something other than its ID, you can define an index by stating a field to be a finder_field. This will allow you to do this:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
User.create(username: "some_guy")
|
51
|
+
|
52
|
+
User.find_by_username("some_guy") #=> our user
|
53
|
+
```
|
54
|
+
|
55
|
+
### Redis Data Structures
|
56
|
+
|
57
|
+
Using hashes, sets, sorted sets and lists is simple-ish.
|
58
|
+
|
59
|
+
|
60
|
+
Hash simply acts like a hash
|
61
|
+
|
62
|
+
|
63
|
+
Sets and Lists act like any ruby array
|
64
|
+
|
65
|
+
|
66
|
+
Sorted sets' getters act as an array, but setters are hash-like, where the hash key is the value and hash value is the score. Yes, it's a bit confusing.
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
class User < ActiveRedis::Base
|
70
|
+
field :foo, type: :sorted_set
|
71
|
+
end
|
72
|
+
|
73
|
+
user = User.new
|
74
|
+
|
75
|
+
user.foo["value2"] = 1
|
76
|
+
|
77
|
+
user.foo["value1"] = 0
|
78
|
+
user.save
|
79
|
+
|
80
|
+
user.foo #=> ["value1", "value2"]
|
81
|
+
```
|
82
|
+
|
83
|
+
## Callbacks and Validations
|
84
|
+
|
85
|
+
Callbacks and validations work just like in Mongoid or ActiveRecord
|
86
|
+
|
87
|
+
```ruby
|
88
|
+
class Messages < ActiveRedis::Base
|
89
|
+
include ActiveRedis::Timestamps
|
90
|
+
|
91
|
+
field :text
|
92
|
+
field :user_id
|
93
|
+
|
94
|
+
validates :text, presence: true, length: {maximum: 256}
|
95
|
+
validates :user_id, presence: true
|
96
|
+
|
97
|
+
after_create :do_something
|
98
|
+
|
99
|
+
def do_something
|
100
|
+
#this gets called after create
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
message = Message.new(text: "I am new text")
|
105
|
+
message.save #=> false
|
106
|
+
|
107
|
+
message.errors.messages #=> {:user_id=>["can't be blank"]}
|
108
|
+
```
|
109
|
+
|
110
|
+
available callbacks are before/after/around for create/update/save
|
111
|
+
|
112
|
+
|
113
|
+
available validations are any validations which come with ActiveModel (we're still missing the uniqueness validation, as it's a problem in redis, but it'll happen)
|
114
|
+
|
22
115
|
|
23
116
|
## Contributing
|
24
117
|
|
@@ -445,7 +445,7 @@ module ActiveRedis
|
|
445
445
|
|
446
446
|
class_eval %Q{
|
447
447
|
def self.find_by_#{field_name}(value)
|
448
|
-
id =
|
448
|
+
id = ActiveRedis.redis.get(finder_key(:#{field_name}, value))
|
449
449
|
if id.present?
|
450
450
|
self.find(id)
|
451
451
|
else
|
data/lib/active_redis/save.rb
CHANGED
data/lib/active_redis/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_redis_orm
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tom Caspy
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-09-
|
11
|
+
date: 2014-09-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|