redisant 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ .DS_Store
12
+ /*.gem
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.6
@@ -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
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in redisant.gemspec
4
+ gemspec
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
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
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,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -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,5 @@
1
+ class Connection
2
+ def initialize host=nil, port=nil
3
+ $redis = Redis.new( host: host || '127.0.0.1', port: port || 6379 )
4
+ end
5
+ 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,11 @@
1
+ class Index
2
+
3
+ attr_reader :name, :class, :options, :type, :order
4
+ def initialize name, klass, options
5
+ @name = name.to_s
6
+ @class = klass
7
+ @options = options
8
+ @type = options[:type].to_s
9
+ end
10
+
11
+ 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
@@ -0,0 +1,13 @@
1
+ class Inflector
2
+ def self.pluralize name
3
+ "#{name}s"
4
+ end
5
+
6
+ def self.singularize name
7
+ name.to_s.chomp("s")
8
+ end
9
+
10
+ def self.constantize name
11
+ Object.const_get name.to_s.capitalize
12
+ end
13
+ end