rethinker 0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE.md +10 -0
- data/README.md +49 -0
- data/USAGE.rb.md +99 -0
- data/lib/rethinker.rb +38 -0
- data/lib/rethinker/autoload.rb +14 -0
- data/lib/rethinker/connection.rb +49 -0
- data/lib/rethinker/criterion.rb +32 -0
- data/lib/rethinker/database.rb +38 -0
- data/lib/rethinker/document.rb +8 -0
- data/lib/rethinker/document/attributes.rb +117 -0
- data/lib/rethinker/document/core.rb +37 -0
- data/lib/rethinker/document/dynamic_attributes.rb +12 -0
- data/lib/rethinker/document/id.rb +54 -0
- data/lib/rethinker/document/injection_layer.rb +11 -0
- data/lib/rethinker/document/persistence.rb +79 -0
- data/lib/rethinker/document/polymorphic.rb +37 -0
- data/lib/rethinker/document/relation.rb +34 -0
- data/lib/rethinker/document/selection.rb +49 -0
- data/lib/rethinker/document/serialization.rb +41 -0
- data/lib/rethinker/document/timestamps.rb +18 -0
- data/lib/rethinker/document/validation.rb +62 -0
- data/lib/rethinker/error.rb +6 -0
- data/lib/rethinker/query_runner.rb +32 -0
- data/lib/rethinker/query_runner/connection.rb +17 -0
- data/lib/rethinker/query_runner/database_on_demand.rb +16 -0
- data/lib/rethinker/query_runner/driver.rb +10 -0
- data/lib/rethinker/query_runner/selection.rb +8 -0
- data/lib/rethinker/query_runner/table_on_demand.rb +12 -0
- data/lib/rethinker/query_runner/write_error.rb +34 -0
- data/lib/rethinker/railtie.rb +16 -0
- data/lib/rethinker/railtie/database.rake +37 -0
- data/lib/rethinker/relation.rb +6 -0
- data/lib/rethinker/relation/belongs_to.rb +36 -0
- data/lib/rethinker/relation/has_many.rb +30 -0
- data/lib/rethinker/relation/has_many/selection.rb +27 -0
- data/lib/rethinker/selection.rb +6 -0
- data/lib/rethinker/selection/core.rb +39 -0
- data/lib/rethinker/selection/count.rb +13 -0
- data/lib/rethinker/selection/delete.rb +9 -0
- data/lib/rethinker/selection/enumerable.rb +22 -0
- data/lib/rethinker/selection/first.rb +20 -0
- data/lib/rethinker/selection/inc.rb +12 -0
- data/lib/rethinker/selection/limit.rb +15 -0
- data/lib/rethinker/selection/order_by.rb +41 -0
- data/lib/rethinker/selection/scope.rb +11 -0
- data/lib/rethinker/selection/update.rb +6 -0
- data/lib/rethinker/selection/where.rb +23 -0
- data/lib/rethinker/version.rb +3 -0
- metadata +140 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 21309e2b80eb709a611c6117dc846129f8fce756
|
4
|
+
data.tar.gz: 6d5eb8dde92819643dff92eb4987e6144911e40e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 42c8cf1bdce21900141fb02385004c61ac86ea58901c0187838d6a33bb2b6c287544a50f1f2507734f5c64e04cc90aa067deafb525a340092867684e678c2fbd
|
7
|
+
data.tar.gz: 08c4d9949d1d9177adf52096068e009e39361eebb15df9f63ce612aee70306e80e34a0fb5c0175cae72aaed20bdcc160963251baf37f919ab2842f5d2a75faa3
|
data/LICENSE.md
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
Copyright © 2013 Databasify
|
2
|
+
|
3
|
+
Rethinker is a fork of NoBrainer
|
4
|
+
Copyright © 2012 Nicolas Viennot
|
5
|
+
|
6
|
+
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:
|
7
|
+
|
8
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
9
|
+
|
10
|
+
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.
|
data/README.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
Rethinker
|
2
|
+
===========
|
3
|
+
|
4
|
+
Rethinker is a Ruby ORM for [RethinkDB](http://www.rethinkdb.com/).
|
5
|
+
|
6
|
+
Installation
|
7
|
+
-------------
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'rethinker'
|
11
|
+
```
|
12
|
+
|
13
|
+
Usage
|
14
|
+
------
|
15
|
+
|
16
|
+
[See documentation](http://databasify.github.io/rethinker)
|
17
|
+
|
18
|
+
Features
|
19
|
+
---------
|
20
|
+
|
21
|
+
* Compatible with Rails 3 and Rails 4
|
22
|
+
* Autogeneration of ID, MongoDB style
|
23
|
+
* Creation of database and tables on demand
|
24
|
+
* Attributes accessors (`attr_accessor`)
|
25
|
+
* Dynamic attributes
|
26
|
+
* Validation support, expected behavior with `save!`, `save`, etc. (uniqueness validation still in development)
|
27
|
+
* Validatation with create, update, save, and destroy callbacks.
|
28
|
+
* `find`, `create`, `save`, `update_attributes`, `destroy` (`*.find` vs. `find!`).
|
29
|
+
* `where`, `order_by`, `skip`, `limit`, `each`
|
30
|
+
* `update`, `inc`, `dec`
|
31
|
+
* `belongs_to`, `has_many`
|
32
|
+
* `to_json`, `to_xml`
|
33
|
+
* `attr_protected`
|
34
|
+
* Scopes
|
35
|
+
* Thread-safe
|
36
|
+
* Polymorphism
|
37
|
+
|
38
|
+
Contributors
|
39
|
+
------------
|
40
|
+
- Andy Selvig (@ajselvig)
|
41
|
+
|
42
|
+
License
|
43
|
+
--------
|
44
|
+
|
45
|
+
Copyright © 2013 Databasify
|
46
|
+
|
47
|
+
Rethinker is a fork of NoBrainer, copyright © 2012 Nicolas Viennot
|
48
|
+
|
49
|
+
See [`LICENSE.md`](https://github.com/databasify/rethinker/blob/master/LICENSE.md).
|
data/USAGE.rb.md
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
# Rethinker
|
2
|
+
## A Ruby ORM for RethinkDB
|
3
|
+
|
4
|
+
**Source:** [github.com/databasify/rethinker](https://github.com/databasify/rethinker)
|
5
|
+
|
6
|
+
<small>Based on [NoBrainer](https://github.com/nviennot/nobrainer)</small>
|
7
|
+
|
8
|
+
.
|
9
|
+
|
10
|
+
Connect to RethinkDB.
|
11
|
+
Connection string takes the form `rethinkdb://<hostname>/<database>`.
|
12
|
+
Currently, Rethinker only supports a single database.
|
13
|
+
In a Rails project, `config/initializers/rethinker.rb` would be a good place for this.
|
14
|
+
|
15
|
+
Rethinker.connect 'rethinkdb://localhost/blog'
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
|
24
|
+
class Post
|
25
|
+
To use Rethinker with a model, include `Rethinker::Document`.
|
26
|
+
include Rethinker::Document
|
27
|
+
|
28
|
+
Specify field names, optionally providing a default value.
|
29
|
+
|
30
|
+
field :title, default: "Untitled"
|
31
|
+
field :body
|
32
|
+
field :slug
|
33
|
+
|
34
|
+
Rethinker currently supports only `has_many`/`belongs_to` relationships.
|
35
|
+
|
36
|
+
has_many :comments
|
37
|
+
|
38
|
+
Rethinker supports all of the [validations from Active Model](http://api.rubyonrails.org/classes/ActiveModel/Validations.html).
|
39
|
+
validates :body, presence: true
|
40
|
+
validates :title, presence: true, length: {minimum: 5, maximum: 250}
|
41
|
+
validates :slug, uniqueness: true
|
42
|
+
end
|
43
|
+
|
44
|
+
class Comment
|
45
|
+
include Rethinker::Document
|
46
|
+
You can enable the persistence of dynamic attributes by also including `Rethinker::Document::DynamicAttributes`. See below for an example.
|
47
|
+
include Rethinker::Document::DynamicAttributes
|
48
|
+
|
49
|
+
field :author
|
50
|
+
field :body
|
51
|
+
|
52
|
+
belongs_to :post
|
53
|
+
|
54
|
+
validates :author, :body, :post, :presence => true
|
55
|
+
|
56
|
+
When using a `uniqueness` validation with one or more scopes, and referencing an association, use the association's foreign key, e.g. `post_id`, and not the association name, e.g. `post`
|
57
|
+
validates :body, uniqueness: {scope: :post_id}
|
58
|
+
|
59
|
+
after_create do
|
60
|
+
puts "#{author} commented on #{post.title}"
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
|
65
|
+
|
66
|
+
You can clear all data in the database by truncating all tables...
|
67
|
+
|
68
|
+
|
69
|
+
|
70
|
+
Rethinker.purge!
|
71
|
+
|
72
|
+
... or optionally dropping the whole database.
|
73
|
+
Rethinker.purge!(drop: true)
|
74
|
+
|
75
|
+
Prefer `Model.create!` to `Model.create`, because `create!` will raise an exception if validations fail, whereas `create` will simply return an unsaved model object.
|
76
|
+
post = Post.create!(:title => 'ohai', :body => 'yummy')
|
77
|
+
|
78
|
+
puts post.comments.create(:author => 'dude').
|
79
|
+
errors.full_messages == ["Body can't be blank"]
|
80
|
+
|
81
|
+
post.comments.create(:author => 'dude', :body => 'burp')
|
82
|
+
post.comments.create(:author => 'dude', :body => 'wut')
|
83
|
+
post.comments.create(:author => 'joe', :body => 'sir')
|
84
|
+
Comment.all.each { |comment| puts comment.body }
|
85
|
+
|
86
|
+
post.comments.where(:author => 'dude').destroy
|
87
|
+
puts post.comments.count == 1
|
88
|
+
|
89
|
+
Because we included `Rethinker::Document::DynamicAttributes` in the `Comment` class, we can access and update fields not defined using `field`.
|
90
|
+
|
91
|
+
Note that setters and getters are *not* created, so you need to access dynamic fields through `object['fieldname']`.
|
92
|
+
|
93
|
+
comment = post.comments.first
|
94
|
+
comment['author_status'] = 'Space Cadet'
|
95
|
+
comment.save
|
96
|
+
|
97
|
+
post.comments.first['author_status']
|
98
|
+
=> 'Space Cadet'
|
99
|
+
|
data/lib/rethinker.rb
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'rethinkdb'
|
2
|
+
require 'active_model'
|
3
|
+
require 'active_support/core_ext'
|
4
|
+
|
5
|
+
if Gem::Version.new(RUBY_VERSION.dup) < Gem::Version.new('1.9')
|
6
|
+
raise 'Please use Ruby 1.9 or later'
|
7
|
+
end
|
8
|
+
|
9
|
+
module Rethinker
|
10
|
+
require 'rethinker/railtie' if defined?(Rails)
|
11
|
+
require 'rethinker/autoload'
|
12
|
+
extend Rethinker::Autoload
|
13
|
+
|
14
|
+
autoload :Document, :Connection, :Database, :Error, :QueryRunner, :Selection, :Relation, :Criterion
|
15
|
+
require 'rethinker/document/dynamic_attributes'
|
16
|
+
|
17
|
+
class << self
|
18
|
+
# Note: we always access the connection explicitly, so that in the future,
|
19
|
+
# we can refactor to return a connection depending on the context.
|
20
|
+
# Note that a connection is tied to a database in Rethinker.
|
21
|
+
attr_accessor :connection
|
22
|
+
|
23
|
+
def connect(uri)
|
24
|
+
self.connection = Connection.new(uri).tap { |c| c.connect }
|
25
|
+
end
|
26
|
+
|
27
|
+
delegate :db_create, :db_drop, :db_list, :database, :to => :connection
|
28
|
+
delegate :table_create, :table_drop, :table_list,
|
29
|
+
:purge!, :to => :database
|
30
|
+
delegate :run, :to => QueryRunner
|
31
|
+
|
32
|
+
def rails3?
|
33
|
+
return @rails3 unless @rails3.nil?
|
34
|
+
@rails3 = Gem.loaded_specs['activemodel'].version >= Gem::Version.new('3') &&
|
35
|
+
Gem.loaded_specs['activemodel'].version < Gem::Version.new('4')
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
class Rethinker::Connection
|
2
|
+
# A connection is bound to a specific database.
|
3
|
+
|
4
|
+
attr_accessor :uri, :host, :port, :database_name
|
5
|
+
|
6
|
+
def initialize(uri)
|
7
|
+
self.uri = uri
|
8
|
+
parse_uri
|
9
|
+
end
|
10
|
+
|
11
|
+
def raw
|
12
|
+
@raw ||= RethinkDB::Connection.new(:host => host, :port => port, :db => database_name)
|
13
|
+
end
|
14
|
+
|
15
|
+
alias_method :connect, :raw
|
16
|
+
delegate :reconnect, :close, :run, :to => :raw
|
17
|
+
|
18
|
+
[:db_create, :db_drop, :db_list].each do |cmd|
|
19
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
20
|
+
def #{cmd}(*args)
|
21
|
+
Rethinker.run { RethinkDB::RQL.new.#{cmd}(*args) }
|
22
|
+
end
|
23
|
+
RUBY
|
24
|
+
end
|
25
|
+
|
26
|
+
def database
|
27
|
+
@database ||= Rethinker::Database.new(self)
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def parse_uri
|
33
|
+
require 'uri'
|
34
|
+
parsed_uri = URI.parse(uri)
|
35
|
+
|
36
|
+
if parsed_uri.scheme != 'rethinkdb'
|
37
|
+
raise Rethinker::Error::Connection,
|
38
|
+
"Invalid URI. Expecting something like rethinkdb://host:port/database. Got #{uri}"
|
39
|
+
end
|
40
|
+
|
41
|
+
apply_connection_settings!(parsed_uri)
|
42
|
+
end
|
43
|
+
|
44
|
+
def apply_connection_settings!(uri)
|
45
|
+
self.host = uri.host
|
46
|
+
self.port = uri.port || 28015
|
47
|
+
self.database_name = uri.path.gsub(/^\//, '')
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
class Rethinker::Criterion
|
2
|
+
|
3
|
+
attr_accessor :method, :args, :block
|
4
|
+
|
5
|
+
def initialize(method, *args, &block)
|
6
|
+
self.method = method
|
7
|
+
self.args = args.flatten
|
8
|
+
self.block = block
|
9
|
+
end
|
10
|
+
|
11
|
+
def execute(query = nil, context)
|
12
|
+
return RethinkDB::RQL.new.send(self.method, self.args.first) if query.nil?
|
13
|
+
if !args.flatten.blank? && block
|
14
|
+
query.send self.method, *process_args(self.args, context), block
|
15
|
+
elsif !args.flatten.blank?
|
16
|
+
query.send self.method, *process_args(self.args, context)
|
17
|
+
elsif block
|
18
|
+
query.send self.method, block
|
19
|
+
else
|
20
|
+
query.send self.method
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def process_args(args, context)
|
25
|
+
if args && args.first.respond_to?(:to_rql)
|
26
|
+
args.first.to_rql(context)
|
27
|
+
else
|
28
|
+
args
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
class Rethinker::Database
|
2
|
+
attr_accessor :connection
|
3
|
+
|
4
|
+
delegate :database_name, :to => :connection
|
5
|
+
|
6
|
+
def initialize(connection)
|
7
|
+
self.connection = connection
|
8
|
+
end
|
9
|
+
|
10
|
+
def raw
|
11
|
+
@raw ||= RethinkDB::RQL.new.db(database_name)
|
12
|
+
end
|
13
|
+
|
14
|
+
def purge!(options={})
|
15
|
+
if options[:drop]
|
16
|
+
connection.db_drop(database_name)
|
17
|
+
else
|
18
|
+
# truncating each table is much faster
|
19
|
+
table_list.each do |table_name|
|
20
|
+
self.class.truncate_table!(table_name)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
rescue RuntimeError => e
|
24
|
+
raise e unless e.message =~ /No entry with that name/
|
25
|
+
end
|
26
|
+
|
27
|
+
[:table_create, :table_drop, :table_list].each do |cmd|
|
28
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
29
|
+
def #{cmd}(*args)
|
30
|
+
Rethinker.run { raw.#{cmd}(*args) }
|
31
|
+
end
|
32
|
+
RUBY
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.truncate_table!(table_name)
|
36
|
+
Rethinker.run { RethinkDB::RQL.new.table(table_name).delete }
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
module Rethinker::Document::Attributes
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
included do
|
5
|
+
if Rethinker.rails3?
|
6
|
+
include ActiveModel::MassAssignmentSecurity
|
7
|
+
end
|
8
|
+
attr_accessor :attributes
|
9
|
+
|
10
|
+
# Not using class_attribute because we want to
|
11
|
+
# use our custom logic
|
12
|
+
class << self; attr_accessor :fields; end
|
13
|
+
self.fields = {}
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize(attrs={}, options={})
|
17
|
+
super
|
18
|
+
assign_attributes(attrs, options.reverse_merge(:pristine => true))
|
19
|
+
end
|
20
|
+
|
21
|
+
def [](name)
|
22
|
+
__send__("#{name}")
|
23
|
+
end
|
24
|
+
|
25
|
+
def []=(name, value)
|
26
|
+
__send__("#{name}=", value)
|
27
|
+
end
|
28
|
+
|
29
|
+
def reset_attributes
|
30
|
+
# XXX Performance optimization: we don't save field that are not
|
31
|
+
# explicitly set. The row will therefore not contain nil for
|
32
|
+
# unset attributes. This has some implication when using where()
|
33
|
+
# see lib/rethinker/selection/where.rb
|
34
|
+
self.attributes = {}
|
35
|
+
|
36
|
+
# assign default attributes based on the field definitions
|
37
|
+
self.class.fields.each do |name, options|
|
38
|
+
self.__send__("#{name}=", options[:default]) if options.has_key?(:default)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def assign_attributes(attrs, options={})
|
43
|
+
reset_attributes if options[:pristine]
|
44
|
+
|
45
|
+
# TODO Should we reject undeclared fields ?
|
46
|
+
if options[:from_db]
|
47
|
+
attributes.merge! attrs
|
48
|
+
else
|
49
|
+
if Rethinker.rails3?
|
50
|
+
unless options[:without_protection]
|
51
|
+
attrs = sanitize_for_mass_assignment(attrs, options[:as])
|
52
|
+
end
|
53
|
+
end
|
54
|
+
attrs.each do |k,v|
|
55
|
+
if respond_to?("#{k}=")
|
56
|
+
__send__("#{k}=", v)
|
57
|
+
else
|
58
|
+
attributes[k.to_s] = v
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
alias_method :attributes=, :assign_attributes
|
64
|
+
|
65
|
+
# TODO test that thing
|
66
|
+
def inspect
|
67
|
+
attrs = self.class.fields.keys.map { |f| "#{f}: #{attributes[f.to_s].inspect}" }
|
68
|
+
"#<#{self.class} #{attrs.join(', ')}>"
|
69
|
+
end
|
70
|
+
|
71
|
+
module ClassMethods
|
72
|
+
def new_from_db(attrs, options={})
|
73
|
+
klass_from_attrs(attrs).new(attrs, options.reverse_merge(:from_db => true)) if attrs
|
74
|
+
end
|
75
|
+
|
76
|
+
def inherited(subclass)
|
77
|
+
super
|
78
|
+
subclass.fields = self.fields.dup
|
79
|
+
end
|
80
|
+
|
81
|
+
def field(name, options={})
|
82
|
+
name = name.to_sym
|
83
|
+
|
84
|
+
# Using a hash because:
|
85
|
+
# - at some point, we want to associate informations with a field (like the type)
|
86
|
+
# - it gives us a set for free
|
87
|
+
([self] + descendants).each do |klass|
|
88
|
+
klass.fields[name] ||= {}
|
89
|
+
klass.fields[name].merge!(options)
|
90
|
+
end
|
91
|
+
|
92
|
+
# Using a layer so the user can use super when overriding these methods
|
93
|
+
inject_in_layer :attributes, <<-RUBY, __FILE__, __LINE__ + 1
|
94
|
+
def #{name}=(value)
|
95
|
+
attributes['#{name}'] = value
|
96
|
+
end
|
97
|
+
|
98
|
+
def #{name}
|
99
|
+
attributes['#{name}']
|
100
|
+
end
|
101
|
+
RUBY
|
102
|
+
end
|
103
|
+
|
104
|
+
def remove_field(name)
|
105
|
+
name = name.to_sym
|
106
|
+
|
107
|
+
([self] + descendants).each do |klass|
|
108
|
+
klass.fields.delete(name)
|
109
|
+
end
|
110
|
+
|
111
|
+
inject_in_layer :attributes, <<-RUBY, __FILE__, __LINE__ + 1
|
112
|
+
undef #{name}=
|
113
|
+
undef #{name}
|
114
|
+
RUBY
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|