lotus-rethinkdb 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 737a2817a5f539d7027d688b4bc510037206d0c5
4
+ data.tar.gz: a118133ad334d38dd49b3cb505aa596e92af4ded
5
+ SHA512:
6
+ metadata.gz: eaebeddd187f711f8ac12cdfd88637543978e0297590bc2e90b343f8bddb3acdd2f5ced130911ba16b6e7ae45af5caf79bf9f986e6a9cdd43ceb3b4224d08b95
7
+ data.tar.gz: 5c235fe25c5c2a977f200e2877e3607caa2ad8585be679f669a5f187bd97404aa6fa35d0445dce415e42796b74e8a73a3b3d2e159e00c7e2e8691143c16209c6
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ *.gem
15
+ mkmf.log
data/.travis.yml ADDED
@@ -0,0 +1,16 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.0.0
5
+ - 2.1.0
6
+ - 2.1.1
7
+ - 2.1.2
8
+ - 2.1.3
9
+ - 2.1.4
10
+ - 2.1.5
11
+ - 2.2.0
12
+
13
+ services: rethinkdb
14
+
15
+ before_script:
16
+ - sudo add-apt-repository ppa:rethinkdb/ppa && sudo apt-get update && sudo apt-get install rethinkdb
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in lotus-rethinkdb.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Angelo Ashmore
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,31 @@
1
+ # Lotus::Model RethinkDB Adapter
2
+
3
+ This adapter implements persistence layer for RethinkDB. Under development.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'lotus-rethinkdb'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install lotus-rethinkdb
20
+
21
+ ## Usage
22
+
23
+ TODO: Write usage instructions here
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it ( https://github.com/angeloashmore/lotus-rethinkdb/fork )
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'bundler/gem_tasks'
4
+
5
+ Rake::TestTask.new do |t|
6
+ t.pattern = 'test/**/*_test.rb'
7
+ t.libs.push 'test'
8
+ end
9
+
10
+ namespace :test do
11
+ task :coverage do
12
+ ENV['COVERAGE'] = 'true'
13
+ Rake::Task['test'].invoke
14
+ end
15
+ end
16
+
17
+ task default: :test
@@ -0,0 +1,205 @@
1
+ require 'delegate'
2
+ require 'active_support/core_ext/hash/indifferent_access'
3
+ require 'rethinkdb'
4
+
5
+ module Lotus
6
+ module Model
7
+ module Adapters
8
+ module Rethinkdb
9
+ # Maps a RethinkDB database table and perfoms manipulations on it.
10
+ #
11
+ # @api private
12
+ # @since 0.1.0
13
+ class Collection < SimpleDelegator
14
+ include RethinkDB::Shortcuts
15
+
16
+ # Initialize a collection
17
+ #
18
+ # @param connection [RethinkDB::Connection] the connection to the
19
+ # database
20
+ # @param dataset [RethinkDB::RQL] the dataset that maps a table or a
21
+ # subset of it
22
+ # @param mapped_collection [Lotus::Model::Mapping::Collection] a
23
+ # mapped collection
24
+ #
25
+ # @return [Lotus::Model::Adapters::Rethinkdb::Collection]
26
+ #
27
+ # @api private
28
+ # @since 0.1.0
29
+ def initialize(connection, dataset, mapped_collection)
30
+ super(dataset)
31
+ @connection, @mapped_collection = connection, mapped_collection
32
+ end
33
+
34
+ # Creates a document for the given entity and assigns an id.
35
+ #
36
+ # @param entity [Object] the entity to persist
37
+ #
38
+ # @see Lotus::Model::Adapters::Rethinkdb::Command#create
39
+ #
40
+ # @return the primary key of the created document
41
+ #
42
+ # @api private
43
+ # @since 0.1.0
44
+ def insert(entity)
45
+ response = _run do
46
+ super(_serialize(entity))
47
+ end
48
+
49
+ response['generated_keys'].first
50
+ end
51
+
52
+ # Updates the document corresponding to the given entity.
53
+ #
54
+ # @param entity [Object] the entity to persist
55
+ #
56
+ # @see Lotus::Model::Adapters::Rethinkdb::Command#update
57
+ #
58
+ # @api private
59
+ # @since 0.1.0
60
+ def update(entity)
61
+ _run do
62
+ super(_serialize(entity))
63
+ end
64
+ end
65
+
66
+ # Deletes the current scope.
67
+ #
68
+ # @see Lotus::Model::Adapters::Rethinkdb::Command#delete
69
+ #
70
+ # @api private
71
+ # @since 0.1.0
72
+ def delete
73
+ _run do
74
+ super
75
+ end
76
+ end
77
+
78
+ # Filters the current scope with a `filter` directive.
79
+ #
80
+ # @param args [Array] the array of arguments
81
+ #
82
+ # @see Lotus::Model::Adapters::Rethinkdb::Query#where
83
+ #
84
+ # @return [Lotus::Model::Adapters::Rethinkdb::Collection] the filtered
85
+ # collection
86
+ #
87
+ # @api private
88
+ # @since 0.1.0
89
+ def filter(*args)
90
+ _collection(super, @mapped_collection)
91
+ end
92
+
93
+ # Filters the current scope with a `pluck` directive.
94
+ #
95
+ # @param args [Array] the array of arguments
96
+ #
97
+ # @see Lotus::Model::Adapters::Rethinkdb::Query#pluck
98
+ #
99
+ # @return [Lotus::Model::Adapters::Rethinkdb::Collection] the filtered
100
+ # collection
101
+ #
102
+ # @api private
103
+ # @since 0.1.0
104
+ def pluck(*args)
105
+ _collection(super, @mapped_collection)
106
+ end
107
+
108
+ # Filters the current scope with a `limit` directive.
109
+ #
110
+ # @param args [Array] the array of arguments
111
+ #
112
+ # @see Lotus::Model::Adapters::Rethinkdb::Query#limit
113
+ #
114
+ # @return [Lotus::Model::Adapters::Rethinkdb::Collection] the filtered
115
+ # collection
116
+ #
117
+ # @api private
118
+ # @since 0.1.0
119
+ def limit(*args)
120
+ _collection(super, @mapped_collection)
121
+ end
122
+
123
+ # Filters the current scope with an `order_by` directive.
124
+ #
125
+ # @param args [Array] the array of arguments
126
+ #
127
+ # @see Lotus::Model::Adapters::Rethinkdb::Query#order
128
+ # @see Lotus::Model::Adapters::Rethinkdb::Query#desc
129
+ #
130
+ # @return [Lotus::Model::Adapters::Rethinkdb::Collection] the filtered
131
+ # collection
132
+ #
133
+ # @api private
134
+ # @since 0.1.0
135
+ def order_by(*args)
136
+ _collection(super, @mapped_collection)
137
+ end
138
+
139
+ # Resolves self by fetching the documents from the database and
140
+ # translating them into entities.
141
+ #
142
+ # @return [Array] the result of the query
143
+ #
144
+ # @api private
145
+ # @since 0.1.0
146
+ def to_a
147
+ _deserialize(
148
+ _run do
149
+ self
150
+ end
151
+ )
152
+ end
153
+
154
+ alias_method :execute, :to_a
155
+
156
+ private
157
+
158
+ # Serialize the given entity before to persist in the database.
159
+ #
160
+ # @return [Hash] the serialized entity
161
+ #
162
+ # @api private
163
+ # @since 0.1.0
164
+ def _serialize(entity)
165
+ @mapped_collection.serialize(entity)
166
+ end
167
+
168
+ # Deserialize a set of documents fetched from the database.
169
+ #
170
+ # @note ActiveSupport's HashWithIndifferentAccess is used to solve an
171
+ # incompatibility between Lotus::Model's use of symbols and
172
+ # RethinkDB's use of strings.
173
+ #
174
+ # @param documents [Array] a set of raw documents
175
+ #
176
+ # @api private
177
+ # @since 0.1.0
178
+ def _deserialize(documents)
179
+ @mapped_collection.deserialize(
180
+ documents.map(&:with_indifferent_access)
181
+ )
182
+ end
183
+
184
+ # Returns a collection with the connection automatically included.
185
+ #
186
+ # @return [Lotus::Model::Adapters::Rethinkdb::Collection]
187
+ #
188
+ # @api private
189
+ # @since 0.1.0
190
+ def _collection(*args)
191
+ Collection.new(@connection, *args)
192
+ end
193
+
194
+ # Run the enclosed block on the database.
195
+ #
196
+ # @api private
197
+ # @since 0.1.0
198
+ def _run
199
+ yield.run(@connection)
200
+ end
201
+ end
202
+ end
203
+ end
204
+ end
205
+ end
@@ -0,0 +1,66 @@
1
+ module Lotus
2
+ module Model
3
+ module Adapters
4
+ module Rethinkdb
5
+ # Execute a command for the given query.
6
+ #
7
+ # @see Lotus::Model::Adapters::Rethinkdb::Query
8
+ #
9
+ # @api private
10
+ # @since 0.1.0
11
+ class Command
12
+ # Initialize a command
13
+ #
14
+ # @param query [Lotus::Model::Adapters::Rethinkdb::Query]
15
+ #
16
+ # @api private
17
+ # @since 0.1.0
18
+ def initialize(query)
19
+ @collection = query.scoped
20
+ end
21
+
22
+ # Creates a document for the given entity.
23
+ #
24
+ # @param entity [Object] the entity to persist
25
+ #
26
+ # @see Lotus::Model::Adapters::Rethinkdb::Collection#insert
27
+ #
28
+ # @return the primary key of the just created document.
29
+ #
30
+ # @api private
31
+ # @since 0.1.0
32
+ def create(entity)
33
+ @collection.insert(entity)
34
+ end
35
+
36
+ # Updates the corresponding document for the given entity.
37
+ #
38
+ # @param entity [Object] the entity to persist
39
+ #
40
+ # @see Lotus::Model::Adapters::Rethinkdb::Collection#update
41
+ #
42
+ # @api private
43
+ # @since 0.1.0
44
+ def update(entity)
45
+ @collection.update(entity)
46
+ end
47
+
48
+ # Deletes all the documents for the current query.
49
+ #
50
+ # It's used to delete a single document or an entire database table.
51
+ #
52
+ # @see Lotus::Model::Adapters::RethinkdbAdapter#delete
53
+ # @see Lotus::Model::Adapters::RethinkdbAdapter#clear
54
+ #
55
+ # @api private
56
+ # @since 0.1.0
57
+ def delete
58
+ @collection.delete
59
+ end
60
+
61
+ alias_method :clear, :delete
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end