redisant 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +1 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +46 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/redisant/attribute_builder.rb +38 -0
- data/lib/redisant/connection.rb +5 -0
- data/lib/redisant/criteria.rb +180 -0
- data/lib/redisant/errors.rb +20 -0
- data/lib/redisant/index.rb +11 -0
- data/lib/redisant/index_builder.rb +21 -0
- data/lib/redisant/inflector.rb +13 -0
- data/lib/redisant/orm.rb +19 -0
- data/lib/redisant/query.rb +168 -0
- data/lib/redisant/records.rb +390 -0
- data/lib/redisant/relation_builder.rb +55 -0
- data/lib/redisant/relations.rb +201 -0
- data/lib/redisant/search.rb +29 -0
- data/lib/redisant/search_builder.rb +21 -0
- data/lib/redisant/set.rb +6 -0
- data/lib/redisant/version.rb +3 -0
- data/lib/redisant.rb +17 -0
- data/redisant.gemspec +27 -0
- metadata +145 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 0ef5e633c7cc819756750fb6fce38da764f25e913bd39e5bca34bedae5f31e37
|
4
|
+
data.tar.gz: 1e7d37f5d263c26dce475a54745d2303d6b2daeedf66891b085bc3bb62153eb9
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 6d38c528b7e52bbaa58efe37f8e75e3e82bcb2c9de95b01586ba6c6c5d78220fce83a63779002a0cbd4da0661b1b65ff2d9d2b45f32a626206669f48f7f3c2c1
|
7
|
+
data.tar.gz: 53cab79b3f5f133d61ce75746c4f640cd94b45633dff94d6bded7a2b946d0f9c82a3175b9e9053c7065923ad1f5f7d7e8ab0ba563549b920ed8bb2174e667cc6
|
data/.gitignore
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Emil Tin
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# Redisant
|
2
|
+
|
3
|
+
ORM-like object storage for Redis. Makes it easy to store, find and query objects, and link them via associations.
|
4
|
+
|
5
|
+
NOTE: running the rspec tests requires that redis is running. All data in the database will be deleted.
|
6
|
+
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'redisant'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install redisant
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
You need a Redis sever running to use this gem. An easy way to do this is via Docker:
|
27
|
+
|
28
|
+
> docker run -d -p 6379:6379 redis
|
29
|
+
|
30
|
+
TODO: Write usage instructions here
|
31
|
+
|
32
|
+
## Development
|
33
|
+
|
34
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
35
|
+
|
36
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
37
|
+
|
38
|
+
## Contributing
|
39
|
+
|
40
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/emiltin/redisant. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
41
|
+
|
42
|
+
|
43
|
+
## License
|
44
|
+
|
45
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
46
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "redisant"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
module AttributeBuilder
|
2
|
+
def self.included(base)
|
3
|
+
base.extend ClassMethods
|
4
|
+
end
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def attribute name, options={}
|
8
|
+
attributes[name.to_s] = options
|
9
|
+
|
10
|
+
if options[:index]
|
11
|
+
index name, type: options[:index] # add index for attribute
|
12
|
+
end
|
13
|
+
|
14
|
+
if options[:search] || options[:unique]
|
15
|
+
finder name, type: options[:search] # add search for attribute
|
16
|
+
end
|
17
|
+
|
18
|
+
send :define_method, name.to_s do
|
19
|
+
attribute(name.to_s)
|
20
|
+
end
|
21
|
+
|
22
|
+
send :define_method, "#{name.to_s}=" do |value|
|
23
|
+
set_attribute(name.to_s, value)
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
def find_attribute name
|
29
|
+
indexes[name.to_s]
|
30
|
+
end
|
31
|
+
|
32
|
+
def attributes
|
33
|
+
@attributes ||= {}
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,180 @@
|
|
1
|
+
class Criteria
|
2
|
+
undef_method '=='
|
3
|
+
undef_method '!='
|
4
|
+
|
5
|
+
attr_reader :object_class, :ids_key
|
6
|
+
|
7
|
+
def initialize base
|
8
|
+
if base.is_a? Relation
|
9
|
+
@ids_key = base.redis_key
|
10
|
+
@object_class = base.object_class
|
11
|
+
criteria[:relation] = base
|
12
|
+
else
|
13
|
+
@ids_key = base.id_key
|
14
|
+
@object_class = base
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def relation relation
|
19
|
+
criteria[:relation] = relation
|
20
|
+
self
|
21
|
+
end
|
22
|
+
|
23
|
+
def criteria
|
24
|
+
@criteria ||= {:where => {}}
|
25
|
+
end
|
26
|
+
|
27
|
+
def where options
|
28
|
+
merge_options options
|
29
|
+
self
|
30
|
+
end
|
31
|
+
|
32
|
+
def limit limit
|
33
|
+
criteria[:limit] = limit
|
34
|
+
self
|
35
|
+
end
|
36
|
+
|
37
|
+
def count
|
38
|
+
criteria[:count] = true
|
39
|
+
result
|
40
|
+
end
|
41
|
+
|
42
|
+
def random
|
43
|
+
criteria[:random] = true
|
44
|
+
result
|
45
|
+
end
|
46
|
+
|
47
|
+
def first options={}
|
48
|
+
merge_options options
|
49
|
+
criteria[:offset] = 0
|
50
|
+
criteria[:limit] = 1
|
51
|
+
criteria[:order] = :asc
|
52
|
+
result
|
53
|
+
end
|
54
|
+
|
55
|
+
def last options={}
|
56
|
+
merge_options options
|
57
|
+
criteria[:offset] = 0
|
58
|
+
criteria[:limit] = 1
|
59
|
+
criteria[:order] = :desc
|
60
|
+
result
|
61
|
+
end
|
62
|
+
|
63
|
+
def sort options
|
64
|
+
criteria[:sort] = options
|
65
|
+
self
|
66
|
+
end
|
67
|
+
|
68
|
+
def order options
|
69
|
+
raise Redisant::InvalidArgument.new('Invalid order') unless ['asc','desc'].include? options.to_s
|
70
|
+
|
71
|
+
criteria[:order] = options
|
72
|
+
self
|
73
|
+
end
|
74
|
+
|
75
|
+
def ids
|
76
|
+
criteria[:ids] = true
|
77
|
+
self
|
78
|
+
end
|
79
|
+
|
80
|
+
def any?
|
81
|
+
criteria[:exists] = true
|
82
|
+
criteria[:ids] = true
|
83
|
+
result
|
84
|
+
end
|
85
|
+
|
86
|
+
|
87
|
+
def ids?
|
88
|
+
criteria[:ids] == true
|
89
|
+
end
|
90
|
+
|
91
|
+
def where?
|
92
|
+
criteria[:where].size > 0
|
93
|
+
end
|
94
|
+
|
95
|
+
def where_single?
|
96
|
+
num_conditions == 1
|
97
|
+
end
|
98
|
+
|
99
|
+
def where_multi?
|
100
|
+
num_conditions >= 1
|
101
|
+
end
|
102
|
+
|
103
|
+
def single?
|
104
|
+
criteria[:limit] == 1 ||
|
105
|
+
criteria[:random] ||
|
106
|
+
criteria[:count]
|
107
|
+
end
|
108
|
+
|
109
|
+
def sort?
|
110
|
+
criteria[:sort] != nil
|
111
|
+
end
|
112
|
+
|
113
|
+
def order?
|
114
|
+
criteria[:order] != nil
|
115
|
+
end
|
116
|
+
|
117
|
+
def limit?
|
118
|
+
criteria[:limit] != nil
|
119
|
+
end
|
120
|
+
|
121
|
+
def count?
|
122
|
+
criteria[:count] == true
|
123
|
+
end
|
124
|
+
|
125
|
+
def random?
|
126
|
+
criteria[:random] == true
|
127
|
+
end
|
128
|
+
|
129
|
+
def num_conditions
|
130
|
+
n = criteria[:where].keys.size
|
131
|
+
if criteria[:relation]
|
132
|
+
n += 1
|
133
|
+
end
|
134
|
+
n
|
135
|
+
end
|
136
|
+
|
137
|
+
def get_conditions
|
138
|
+
criteria[:where]
|
139
|
+
end
|
140
|
+
|
141
|
+
def get_relation
|
142
|
+
criteria[:relation]
|
143
|
+
end
|
144
|
+
|
145
|
+
def get_order
|
146
|
+
criteria[:order]
|
147
|
+
end
|
148
|
+
|
149
|
+
def method_missing(*args, &block)
|
150
|
+
load_if_needed
|
151
|
+
@result.send(*args, &block)
|
152
|
+
end
|
153
|
+
|
154
|
+
def load_if_needed
|
155
|
+
loaded = @loaded
|
156
|
+
@loaded = true
|
157
|
+
reload! unless loaded
|
158
|
+
end
|
159
|
+
|
160
|
+
|
161
|
+
def loaded?
|
162
|
+
@loaded == true
|
163
|
+
end
|
164
|
+
|
165
|
+
def result
|
166
|
+
load_if_needed
|
167
|
+
@result
|
168
|
+
end
|
169
|
+
|
170
|
+
def reload!
|
171
|
+
@result = Query.new(self).run
|
172
|
+
end
|
173
|
+
|
174
|
+
private
|
175
|
+
|
176
|
+
def merge_options options
|
177
|
+
criteria[:where].merge!(options)
|
178
|
+
end
|
179
|
+
|
180
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
module Redisant
|
2
|
+
class Error < StandardError
|
3
|
+
attr_reader :message
|
4
|
+
def initialize message
|
5
|
+
@message = message
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
class InvalidArgument < Error
|
10
|
+
def initialize message
|
11
|
+
super message
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class ValidationFailed < Error
|
16
|
+
def initialize message
|
17
|
+
super message
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module IndexBuilder
|
2
|
+
def self.included(base)
|
3
|
+
base.extend ClassMethods
|
4
|
+
end
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
def index name, options={}
|
8
|
+
indexes[name.to_s] ||= Index.new name, self, options
|
9
|
+
end
|
10
|
+
|
11
|
+
def find_index name
|
12
|
+
indexes[name.to_s]
|
13
|
+
end
|
14
|
+
|
15
|
+
def indexes
|
16
|
+
@indexes ||= {}
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|