riagent 0.0.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 +7 -0
- data/.gitignore +19 -0
- data/Gemfile +24 -0
- data/Gemfile.lock +73 -0
- data/LICENSE +202 -0
- data/README.md +230 -0
- data/Rakefile +47 -0
- data/lib/rails/generators/riagent/install/install_generator.rb +35 -0
- data/lib/rails/generators/riagent/install/templates/riak.yml +16 -0
- data/lib/riagent/active_document.rb +53 -0
- data/lib/riagent/associations.rb +119 -0
- data/lib/riagent/configuration.rb +73 -0
- data/lib/riagent/conversion.rb +83 -0
- data/lib/riagent/errors.rb +33 -0
- data/lib/riagent/persistence/riak_json_strategy.rb +62 -0
- data/lib/riagent/persistence.rb +166 -0
- data/lib/riagent/railtie.rb +35 -0
- data/lib/riagent/search_schema.rb +58 -0
- data/lib/riagent/version.rb +23 -0
- data/lib/riagent.rb +29 -0
- data/riagent.gemspec +51 -0
- data/test/config/riak.yml.example +19 -0
- data/test/examples/models/address_book.rb +32 -0
- data/test/examples/models/blog_post.rb +31 -0
- data/test/examples/models/contact.rb +29 -0
- data/test/examples/models/user.rb +41 -0
- data/test/integration/persistence_integration_test.rb +76 -0
- data/test/seeds.rb +30 -0
- data/test/test_helper.rb +33 -0
- data/test/unit/active_document_test.rb +41 -0
- data/test/unit/active_model_lint_test.rb +33 -0
- data/test/unit/associations_has_many_test.rb +32 -0
- data/test/unit/associations_has_one_test.rb +85 -0
- data/test/unit/config_test.rb +37 -0
- data/test/unit/embedded_test.rb +38 -0
- data/test/unit/persistence_riak_json_test.rb +118 -0
- data/test/unit/persistence_test.rb +57 -0
- data/test/unit/search_schema_test.rb +26 -0
- data/test/unit/validation_test.rb +39 -0
- metadata +227 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4eadd9aec51f179844d1ac1a5ff54826ba59ad35
|
4
|
+
data.tar.gz: 4cbaed26f3b6f06e43c0405f401b69835f10ca81
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: b31c273dbcc22ad47467ce2f7d5875ce6b04be513124b38973695130266341ed4e8440bc786d34f7e9d044271b0f77a46abc50b05f4f0155086765cfbe852bac
|
7
|
+
data.tar.gz: e59f9bdd5a8e332de5e56c49e3565c772e485fbff10e19ae3f96fd1ac79e0d6ad0e4436d1c041290c471fe95ffe50c4216710d6bc8f86a187fa5e743966e0acf
|
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
## -------------------------------------------------------------------
|
2
|
+
##
|
3
|
+
## Copyright (c) "2014" Dmitri Zagidulin and Basho Technologies, Inc.
|
4
|
+
##
|
5
|
+
## This file is provided to you under the Apache License,
|
6
|
+
## Version 2.0 (the "License"); you may not use this file
|
7
|
+
## except in compliance with the License. You may obtain
|
8
|
+
## a copy of the License at
|
9
|
+
##
|
10
|
+
## http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
##
|
12
|
+
## Unless required by applicable law or agreed to in writing,
|
13
|
+
## software distributed under the License is distributed on an
|
14
|
+
## "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
15
|
+
## KIND, either express or implied. See the License for the
|
16
|
+
## specific language governing permissions and limitations
|
17
|
+
## under the License.
|
18
|
+
##
|
19
|
+
## -------------------------------------------------------------------
|
20
|
+
|
21
|
+
source 'https://rubygems.org'
|
22
|
+
|
23
|
+
# Specify your gem's dependencies in riagent.gemspec
|
24
|
+
gemspec
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
riagent (0.0.1)
|
5
|
+
activemodel (~> 4.0)
|
6
|
+
activesupport (~> 4.0)
|
7
|
+
riagent-document
|
8
|
+
riak-client
|
9
|
+
riak_json
|
10
|
+
|
11
|
+
GEM
|
12
|
+
remote: https://rubygems.org/
|
13
|
+
specs:
|
14
|
+
activemodel (4.0.2)
|
15
|
+
activesupport (= 4.0.2)
|
16
|
+
builder (~> 3.1.0)
|
17
|
+
activesupport (4.0.2)
|
18
|
+
i18n (~> 0.6, >= 0.6.4)
|
19
|
+
minitest (~> 4.2)
|
20
|
+
multi_json (~> 1.3)
|
21
|
+
thread_safe (~> 0.1)
|
22
|
+
tzinfo (~> 0.3.37)
|
23
|
+
atomic (1.1.14)
|
24
|
+
axiom-types (0.0.5)
|
25
|
+
descendants_tracker (~> 0.0.1)
|
26
|
+
ice_nine (~> 0.9)
|
27
|
+
beefcake (0.3.7)
|
28
|
+
builder (3.1.4)
|
29
|
+
coercible (1.0.0)
|
30
|
+
descendants_tracker (~> 0.0.1)
|
31
|
+
descendants_tracker (0.0.3)
|
32
|
+
equalizer (0.0.9)
|
33
|
+
i18n (0.6.9)
|
34
|
+
ice_nine (0.11.0)
|
35
|
+
innertube (1.0.2)
|
36
|
+
json (1.8.1)
|
37
|
+
mime-types (2.1)
|
38
|
+
minitest (4.7.5)
|
39
|
+
minitest-spec-context (0.0.3)
|
40
|
+
multi_json (1.8.4)
|
41
|
+
rake (10.1.1)
|
42
|
+
rest-client (1.6.7)
|
43
|
+
mime-types (>= 1.16)
|
44
|
+
riagent-document (0.0.1)
|
45
|
+
activesupport (~> 4.0)
|
46
|
+
virtus
|
47
|
+
riak-client (1.4.3)
|
48
|
+
beefcake (~> 0.3.7)
|
49
|
+
builder (>= 2.1.2)
|
50
|
+
i18n (>= 0.4.0)
|
51
|
+
innertube (~> 1.0.2)
|
52
|
+
multi_json (~> 1.0)
|
53
|
+
riak_json (0.0.2)
|
54
|
+
json
|
55
|
+
rest-client
|
56
|
+
thread_safe (0.1.3)
|
57
|
+
atomic
|
58
|
+
tzinfo (0.3.38)
|
59
|
+
virtus (1.0.1)
|
60
|
+
axiom-types (~> 0.0.5)
|
61
|
+
coercible (~> 1.0)
|
62
|
+
descendants_tracker (~> 0.0.1)
|
63
|
+
equalizer (~> 0.0.7)
|
64
|
+
|
65
|
+
PLATFORMS
|
66
|
+
ruby
|
67
|
+
|
68
|
+
DEPENDENCIES
|
69
|
+
bundler
|
70
|
+
minitest (~> 4.2)
|
71
|
+
minitest-spec-context
|
72
|
+
rake
|
73
|
+
riagent!
|
data/LICENSE
ADDED
@@ -0,0 +1,202 @@
|
|
1
|
+
Apache License
|
2
|
+
Version 2.0, January 2004
|
3
|
+
http://www.apache.org/licenses/
|
4
|
+
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6
|
+
|
7
|
+
1. Definitions.
|
8
|
+
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
11
|
+
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13
|
+
the copyright owner that is granting the License.
|
14
|
+
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
16
|
+
other entities that control, are controlled by, or are under common
|
17
|
+
control with that entity. For the purposes of this definition,
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
19
|
+
direction or management of such entity, whether by contract or
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22
|
+
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24
|
+
exercising permissions granted by this License.
|
25
|
+
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
27
|
+
including but not limited to software source code, documentation
|
28
|
+
source, and configuration files.
|
29
|
+
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
31
|
+
transformation or translation of a Source form, including but
|
32
|
+
not limited to compiled object code, generated documentation,
|
33
|
+
and conversions to other media types.
|
34
|
+
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
36
|
+
Object form, made available under the License, as indicated by a
|
37
|
+
copyright notice that is included in or attached to the work
|
38
|
+
(an example is provided in the Appendix below).
|
39
|
+
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46
|
+
the Work and Derivative Works thereof.
|
47
|
+
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
49
|
+
the original version of the Work and any modifications or additions
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
61
|
+
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
64
|
+
subsequently incorporated within the Work.
|
65
|
+
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
72
|
+
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78
|
+
where such license applies only to those patent claims licensable
|
79
|
+
by such Contributor that are necessarily infringed by their
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
82
|
+
institute patent litigation against any entity (including a
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
85
|
+
or contributory patent infringement, then any patent licenses
|
86
|
+
granted to You under this License for that Work shall terminate
|
87
|
+
as of the date such litigation is filed.
|
88
|
+
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
91
|
+
modifications, and in Source or Object form, provided that You
|
92
|
+
meet the following conditions:
|
93
|
+
|
94
|
+
(a) You must give any other recipients of the Work or
|
95
|
+
Derivative Works a copy of this License; and
|
96
|
+
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
98
|
+
stating that You changed the files; and
|
99
|
+
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
102
|
+
attribution notices from the Source form of the Work,
|
103
|
+
excluding those notices that do not pertain to any part of
|
104
|
+
the Derivative Works; and
|
105
|
+
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
108
|
+
include a readable copy of the attribution notices contained
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
111
|
+
of the following places: within a NOTICE text file distributed
|
112
|
+
as part of the Derivative Works; within the Source form or
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
114
|
+
within a display generated by the Derivative Works, if and
|
115
|
+
wherever such third-party notices normally appear. The contents
|
116
|
+
of the NOTICE file are for informational purposes only and
|
117
|
+
do not modify the License. You may add Your own attribution
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
120
|
+
that such additional attribution notices cannot be construed
|
121
|
+
as modifying the License.
|
122
|
+
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
124
|
+
may provide additional or different license terms and conditions
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
128
|
+
the conditions stated in this License.
|
129
|
+
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
133
|
+
this License, without any additional terms or conditions.
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135
|
+
the terms of any separate license agreement you may have executed
|
136
|
+
with Licensor regarding such Contributions.
|
137
|
+
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
140
|
+
except as required for reasonable and customary use in describing the
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
142
|
+
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
152
|
+
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
158
|
+
incidental, or consequential damages of any character arising as a
|
159
|
+
result of this License or out of the use or inability to use the
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
162
|
+
other commercial damages or losses), even if such Contributor
|
163
|
+
has been advised of the possibility of such damages.
|
164
|
+
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168
|
+
or other liability obligations and/or rights consistent with this
|
169
|
+
License. However, in accepting such obligations, You may act only
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
174
|
+
of your accepting any such warranty or additional liability.
|
175
|
+
|
176
|
+
END OF TERMS AND CONDITIONS
|
177
|
+
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
179
|
+
|
180
|
+
To apply the Apache License to your work, attach the following
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "{}"
|
182
|
+
replaced with your own identifying information. (Don't include
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
184
|
+
comment syntax for the file format. We also recommend that a
|
185
|
+
file or class name and description of purpose be included on the
|
186
|
+
same "printed page" as the copyright notice for easier
|
187
|
+
identification within third-party archives.
|
188
|
+
|
189
|
+
Copyright {yyyy} {name of copyright owner}
|
190
|
+
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192
|
+
you may not use this file except in compliance with the License.
|
193
|
+
You may obtain a copy of the License at
|
194
|
+
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
196
|
+
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200
|
+
See the License for the specific language governing permissions and
|
201
|
+
limitations under the License.
|
202
|
+
|
data/README.md
ADDED
@@ -0,0 +1,230 @@
|
|
1
|
+
# riagent
|
2
|
+
|
3
|
+
Object Document Mapper for RiakJson (Riak + Solr), for use with Ruby on Rails 4 and Sinatra.
|
4
|
+
|
5
|
+
## Requirements
|
6
|
+
- Ruby 1.9+
|
7
|
+
- Riak build with [Riak Json](https://github.com/basho-labs/riak_json) and Solr/Yokozuna enabled
|
8
|
+
(see [Setup](https://github.com/basho-labs/riak_json#setup) section for instructions)
|
9
|
+
- [RiakJson](https://github.com/basho-labs/riak_json_ruby_client) Ruby Client gem installed locally
|
10
|
+
- [riagent-document](https://github.com/dmitrizagidulin/riagent-document) gem installed locally
|
11
|
+
|
12
|
+
## Motivation
|
13
|
+
*a.k.a. Why not just use a plain [riak_json client](https://github.com/basho-labs/riak_json_ruby_client) or a
|
14
|
+
[riak-ruby-client](https://github.com/basho/riak-ruby-client)?*
|
15
|
+
|
16
|
+
A RiakJson or Riak client just answers the question "How do I store stuff in Riak?".
|
17
|
+
In order to develop any non-toy application using Riak as a persistence layer, a developer must answer further questions:
|
18
|
+
|
19
|
+
- How do I define my model objects, and how do I serialize them so I can store them in Riak?
|
20
|
+
- What will my access patterns be? Plain Key/Value reads and writes, or will I need to perform queries?
|
21
|
+
- How do I integrate painlessly with my framework of choice? (Ruby on Rails, Sinatra)
|
22
|
+
|
23
|
+
Riagent attempts to provide answers to those questions, to encode recommended best-practice Riak query patterns,
|
24
|
+
and in general to aid rapid application development by working with Riak's strenghts while respecting its limitations.
|
25
|
+
It is intended as a spiritual successor to [Ripple](https://github.com/basho-labs/ripple).
|
26
|
+
|
27
|
+
#### Model Definition and Serialization
|
28
|
+
Riagent provides a model definition language that will be familiar to most Rails developers, via
|
29
|
+
the [riagent-document](https://github.com/dmitrizagidulin/riagent-document) gem.
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
include 'riagent-document'
|
33
|
+
|
34
|
+
class User
|
35
|
+
include Riagent::ActiveDocument
|
36
|
+
|
37
|
+
attribute :username, String
|
38
|
+
attribute :email, String
|
39
|
+
attribute :country, String, default: 'USA'
|
40
|
+
end
|
41
|
+
```
|
42
|
+
|
43
|
+
Riagent::ActiveDocument instances provide a rich set of functionality including attribute defaults, type coercions,
|
44
|
+
conversion to and from JSON, search schema creation, easy persistence to Riak,
|
45
|
+
document embedding, and more. See the [riagent-document README](https://github.com/dmitrizagidulin/riagent-document)
|
46
|
+
for more details.
|
47
|
+
|
48
|
+
#### Advanced Riak K/V and Query Support
|
49
|
+
Reads and writes of single objects into Riak are easy (and have the benefit of being massively scalable, highly concurrent, and fault-tolerant).
|
50
|
+
But what about listing and querying? Every developer that gets past a Hello World get/put tutorial on Riak is soon faced with questions
|
51
|
+
about more advanced access patterns. How do I implement collections, and list things? How do I search or query on various attributes?
|
52
|
+
Should I use Secondary Indexes? What about Search/Solr integration?
|
53
|
+
|
54
|
+
Riagent provides a set of high-level notations and functionality that allows developers create collections and associations on Riak,
|
55
|
+
either via plain K/V operations when possible, or via advanced mechanisms such as Solr/[RiakJson](https://github.com/basho-labs/riak_json)
|
56
|
+
queries when necessary.
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
class User
|
60
|
+
include Riagent::ActiveDocument
|
61
|
+
|
62
|
+
collection_type :riak_json # Persist to a RiakJson::Collection
|
63
|
+
|
64
|
+
attribute :username, String, search_index: { as: :text }
|
65
|
+
attribute :email, String, search_index: { as: :string }
|
66
|
+
attribute :language, String, default: 'en'
|
67
|
+
|
68
|
+
# Associations
|
69
|
+
has_one :email_preference, :class => EmailPreference
|
70
|
+
has_many :posts, :class => BlogPost, :using => :solr
|
71
|
+
|
72
|
+
# Validations
|
73
|
+
validates_presence_of :username
|
74
|
+
end
|
75
|
+
```
|
76
|
+
|
77
|
+
#### Rails and Sinatra integration
|
78
|
+
Riagent and ActiveDocuments are integrated into the usual Rails workflow.
|
79
|
+
|
80
|
+
- Riagent provides client configuration and initialization, via the ```config/riak.yml``` file
|
81
|
+
- Provides a simple Key/Value persistence layer with ```save()```, ```find()```, ```update()``` and ```destroy()``` methods.
|
82
|
+
- ActiveDocument implements the ActiveModel API.
|
83
|
+
(Specifically, passes the [ActiveModel Lint Test suite](http://api.rubyonrails.org/classes/ActiveModel/Lint/Tests.html))
|
84
|
+
- Provides a full range of validations for each attribute.
|
85
|
+
- Provides ```before_save``` / ```after_save``` type Callback functionality
|
86
|
+
- Provides a custom Query capability (to Riak/Solr), for searches, range queries, aggregations and more
|
87
|
+
- Derives RiakJson/Solr search schemas from annotated document attributes (see Schemas below)
|
88
|
+
|
89
|
+
## Usage
|
90
|
+
### Adding Riagent to a Rails App
|
91
|
+
Add it to the Gemfile of your Rails app:
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
gem 'riagent'
|
95
|
+
```
|
96
|
+
|
97
|
+
Run the install generator:
|
98
|
+
```bash
|
99
|
+
rails generate riagent:install
|
100
|
+
```
|
101
|
+
|
102
|
+
This creates a ```config/riak.yml``` file. Edit it to point to your running Riak instance.
|
103
|
+
|
104
|
+
### Controller and View helpers
|
105
|
+
Since they implement the ActiveModel API, when you use ActiveDocuments in
|
106
|
+
a Rails model, the usual ```link_to```/route-based helpers work:
|
107
|
+
```erb
|
108
|
+
# In a user view file
|
109
|
+
<%= link_to @user.username, @user %> # => <a href="/users/EmuVX4kFHxxvlUVJj5TmPGgGPjP">HieronymusBosch</a>
|
110
|
+
<%= link_to 'Edit', edit_user_path(@user) %> # => <a href="/users/EmuVX4kFHxxvlUVJj5TmPGgGPjP/edit">Edit</a>
|
111
|
+
# In a controller
|
112
|
+
redirect_to users_url
|
113
|
+
```
|
114
|
+
|
115
|
+
### Validations
|
116
|
+
ActiveDocument supports the full range of [ActiveModel validations](http://api.rubyonrails.org/classes/ActiveModel/Validations.html)
|
117
|
+
|
118
|
+
```ruby
|
119
|
+
# Try saving an invalid document
|
120
|
+
new_user = User.new
|
121
|
+
new_user.valid? # => false
|
122
|
+
puts user.errors.messages # => {:username=>["can't be blank"]}
|
123
|
+
new_user.save # => false (does not actually save, since document not valid)
|
124
|
+
new_user.save! # => raises Riagent::InvalidDocumentError exception
|
125
|
+
|
126
|
+
# Now make it valid
|
127
|
+
new_user.username = 'HieronymusBosch'
|
128
|
+
new_user.valid? # => true
|
129
|
+
new_user.save # => saves and loads the generated key into document
|
130
|
+
new_user.key # => 'EmuVX4kFHxxvlUVJj5TmPGgGPjP'
|
131
|
+
```
|
132
|
+
|
133
|
+
### Key/Value Persistence
|
134
|
+
The usual array of CRUD type k/v operations is available to an ActiveDocument model.
|
135
|
+
|
136
|
+
Create documents via ```save()``` and ```save!()```
|
137
|
+
```ruby
|
138
|
+
user = User.new({username: 'John', email: 'john@doe.com'})
|
139
|
+
# If you save without specifying a key, RiakJson generates a UUID type key automatically
|
140
|
+
user.save # => 'EmuVX4kFHxxvlUVJj5TmPGgGPjP'
|
141
|
+
```
|
142
|
+
|
143
|
+
To load a document by key, use ```find()```:
|
144
|
+
|
145
|
+
```ruby
|
146
|
+
user = User.find('EmuVX4kFHxxvlUVJj5TmPGgGPjP')
|
147
|
+
```
|
148
|
+
|
149
|
+
Update and Delete work in a similar fashion
|
150
|
+
```ruby
|
151
|
+
user.username = 'New Name'
|
152
|
+
user.update # update!() is also available
|
153
|
+
user.destroy # deletes the document
|
154
|
+
```
|
155
|
+
|
156
|
+
### Callbacks
|
157
|
+
ActiveDocument currently supports ```before_*``` and ```after_*``` [callbacks](http://api.rubyonrails.org/classes/ActiveSupport/Callbacks.html)
|
158
|
+
for the following events:
|
159
|
+
```[:create, :update, :save, :destroy]```
|
160
|
+
|
161
|
+
### Search and Querying
|
162
|
+
See the Querying sections of [RJ Ruby Client](https://github.com/basho-labs/riak_json_ruby_client#querying-riakjson---find_one-and-find)
|
163
|
+
and [RiakJson itself](https://github.com/basho-labs/riak_json/blob/master/docs/query.md)
|
164
|
+
```ruby
|
165
|
+
# All matching instances
|
166
|
+
us_users = User.where({ country: 'USA' }) # => array of US user instances
|
167
|
+
# One instance (the first)
|
168
|
+
user = User.find_one({ username: 'HieronymusBosch' })
|
169
|
+
```
|
170
|
+
|
171
|
+
## Search Schema Definition
|
172
|
+
RiakJson uses Solr/Yokozuna to provide indexing and search capability for its collections.
|
173
|
+
If you do not specify a collection schema explicitly, RiakJson creates one when you insert the first document to that collection
|
174
|
+
(it [infers the schema](https://github.com/basho-labs/riak_json/blob/master/docs/architecture.md#inferred-schemas)
|
175
|
+
based on the basic data types of the field values in the JSON).
|
176
|
+
However, if you do not want to use this default schema behavior (for example, because RJ tries to index all of the fields),
|
177
|
+
you can define and set a collection schema yourself, using RJ Ruby Client's [schema administration](https://github.com/basho-labs/riak_json_ruby_client#schema-administration)
|
178
|
+
API.
|
179
|
+
|
180
|
+
To make the process of schema definition even easier for developers, ActiveDocument provides the ```search_index``` attribute
|
181
|
+
option. This annotation allows you to specify which document fields you want added to your search schema, as well as the
|
182
|
+
Solr field type that will be used to index it.
|
183
|
+
|
184
|
+
For example, the following model:
|
185
|
+
```ruby
|
186
|
+
class User
|
187
|
+
include Riagent::ActiveDocument
|
188
|
+
|
189
|
+
attribute :username, String, required: true, search_index: { :as => :text }
|
190
|
+
attribute :email, String, search_index: { :as => :string }
|
191
|
+
attribute :country, String, default: 'USA'
|
192
|
+
end
|
193
|
+
```
|
194
|
+
will enable you to construct the following schema:
|
195
|
+
```ruby
|
196
|
+
User.schema # =>
|
197
|
+
# [{
|
198
|
+
# :name => "username",
|
199
|
+
# :type => "text",
|
200
|
+
# :require => true
|
201
|
+
# }, {
|
202
|
+
# :name => "email",
|
203
|
+
# :type => "string",
|
204
|
+
# :require => false
|
205
|
+
# }
|
206
|
+
# ]
|
207
|
+
# # Note that 'country' is not included in this schema, and so will not be indexed.
|
208
|
+
```
|
209
|
+
|
210
|
+
### Schema Administration
|
211
|
+
Note that if you use the ```search_index``` attribute annotations above, you will have to explicitly
|
212
|
+
notify RiakJson of your intent to save the schema. You will have to call the ```set_schema()``` collection
|
213
|
+
method before you start inserting documents (for example, in a ```db:setup``` Rake task).
|
214
|
+
|
215
|
+
```ruby
|
216
|
+
User.collection.set_schema(User.schema)
|
217
|
+
```
|
218
|
+
|
219
|
+
## Testing the Riagent gem
|
220
|
+
First, set up the Riak config file for (and make sure it's pointing to a running Riak instance)
|
221
|
+
|
222
|
+
```
|
223
|
+
cp test/config/riak.yml.example test/config/riak.yml
|
224
|
+
```
|
225
|
+
|
226
|
+
To run the tests:
|
227
|
+
|
228
|
+
```
|
229
|
+
bundle exec rake test
|
230
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
## -------------------------------------------------------------------
|
2
|
+
##
|
3
|
+
## Copyright (c) "2014" Dmitri Zagidulin and Basho Technologies, Inc.
|
4
|
+
##
|
5
|
+
## This file is provided to you under the Apache License,
|
6
|
+
## Version 2.0 (the "License"); you may not use this file
|
7
|
+
## except in compliance with the License. You may obtain
|
8
|
+
## a copy of the License at
|
9
|
+
##
|
10
|
+
## http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
##
|
12
|
+
## Unless required by applicable law or agreed to in writing,
|
13
|
+
## software distributed under the License is distributed on an
|
14
|
+
## "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
15
|
+
## KIND, either express or implied. See the License for the
|
16
|
+
## specific language governing permissions and limitations
|
17
|
+
## under the License.
|
18
|
+
##
|
19
|
+
## -------------------------------------------------------------------
|
20
|
+
|
21
|
+
require "bundler/gem_tasks"
|
22
|
+
require "rake"
|
23
|
+
require "rake/testtask"
|
24
|
+
|
25
|
+
task :default => :test
|
26
|
+
|
27
|
+
Rake::TestTask.new :test do |t|
|
28
|
+
t.libs << 'lib' << 'test'
|
29
|
+
t.pattern = 'test/**/*_test.rb'
|
30
|
+
end
|
31
|
+
|
32
|
+
Rake::TestTask.new :itest do |t|
|
33
|
+
t.libs << 'lib' << 'test'
|
34
|
+
t.pattern = 'test/integration/*_test.rb'
|
35
|
+
end
|
36
|
+
|
37
|
+
Rake::TestTask.new :unittest do |t|
|
38
|
+
t.libs << 'lib' << 'test'
|
39
|
+
t.pattern = 'test/unit/*_test.rb'
|
40
|
+
end
|
41
|
+
|
42
|
+
namespace :db do
|
43
|
+
task :seed do
|
44
|
+
seed_file = File.join('test', 'seeds.rb')
|
45
|
+
load(seed_file) if File.exist?(seed_file)
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
## -------------------------------------------------------------------
|
2
|
+
##
|
3
|
+
## Copyright (c) "2014" Dmitri Zagidulin and Basho Technologies, Inc.
|
4
|
+
##
|
5
|
+
## This file is provided to you under the Apache License,
|
6
|
+
## Version 2.0 (the "License"); you may not use this file
|
7
|
+
## except in compliance with the License. You may obtain
|
8
|
+
## a copy of the License at
|
9
|
+
##
|
10
|
+
## http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
##
|
12
|
+
## Unless required by applicable law or agreed to in writing,
|
13
|
+
## software distributed under the License is distributed on an
|
14
|
+
## "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
15
|
+
## KIND, either express or implied. See the License for the
|
16
|
+
## specific language governing permissions and limitations
|
17
|
+
## under the License.
|
18
|
+
##
|
19
|
+
## -------------------------------------------------------------------
|
20
|
+
|
21
|
+
require 'rails/generators'
|
22
|
+
|
23
|
+
module Riagent
|
24
|
+
module Generators
|
25
|
+
class InstallGenerator < Rails::Generators::Base
|
26
|
+
def self.source_root
|
27
|
+
@source_root ||= File.join(File.dirname(__FILE__), 'templates')
|
28
|
+
end
|
29
|
+
|
30
|
+
def create_config_file
|
31
|
+
template 'riak.yml', File.join('config', 'riak.yml')
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# Configure Riak and RiakJson connections for the client, by environment
|
2
|
+
development:
|
3
|
+
http_port: 8098
|
4
|
+
pb_port: 8097 # Protocol Buffer port
|
5
|
+
host: 127.0.0.1
|
6
|
+
|
7
|
+
# You should spin up another Riak instance to use as a test server
|
8
|
+
test:
|
9
|
+
http_port: 8098
|
10
|
+
pb_port: 8097
|
11
|
+
host: 127.0.0.1
|
12
|
+
|
13
|
+
production:
|
14
|
+
http_port: 8098
|
15
|
+
pb_port: 8097
|
16
|
+
host: 127.0.0.1
|