riagent-document 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 +18 -0
- data/Gemfile +24 -0
- data/Gemfile.lock +48 -0
- data/LICENSE.txt +22 -0
- data/README.md +125 -0
- data/Rakefile +31 -0
- data/lib/riagent/document.rb +69 -0
- data/lib/riagent/document/version.rb +25 -0
- data/riagent-document.gemspec +48 -0
- data/test/examples/models/address_book.rb +33 -0
- data/test/examples/models/user.rb +27 -0
- data/test/test_helper.rb +25 -0
- data/test/unit/document_test.rb +44 -0
- data/test/unit/embedded_test.rb +20 -0
- data/test/unit/json_conversion_test.rb +42 -0
- metadata +150 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1a09ba3c82e9fe9ea1f42ba80b76bf087e269143
|
4
|
+
data.tar.gz: d0d0b9cc0854aa4d65f9eadc28e14f200202f531
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 0863630345215d6f8481d292e3287b79a96036526536a5294215da7ef9c788eb999ee964be53645dfbdb5d759acce3a96f7d156b5574314f0fcf107b303da6ff
|
7
|
+
data.tar.gz: 2683c521e20c3ef5857ad5f605bca2cdc8e42fe6d8a898c045e8170c7049eff34614860300890fdfc59ee0933ba1ca5b83afa37521586700f62e222cfb626a2d
|
data/.gitignore
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
## -------------------------------------------------------------------
|
2
|
+
##
|
3
|
+
## Copyright (c) "2013" 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-document.gemspec
|
24
|
+
gemspec
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
riagent-document (0.0.1)
|
5
|
+
activesupport (~> 4.0)
|
6
|
+
virtus
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activesupport (4.0.2)
|
12
|
+
i18n (~> 0.6, >= 0.6.4)
|
13
|
+
minitest (~> 4.2)
|
14
|
+
multi_json (~> 1.3)
|
15
|
+
thread_safe (~> 0.1)
|
16
|
+
tzinfo (~> 0.3.37)
|
17
|
+
atomic (1.1.14)
|
18
|
+
axiom-types (0.0.5)
|
19
|
+
descendants_tracker (~> 0.0.1)
|
20
|
+
ice_nine (~> 0.9)
|
21
|
+
coercible (1.0.0)
|
22
|
+
descendants_tracker (~> 0.0.1)
|
23
|
+
descendants_tracker (0.0.3)
|
24
|
+
equalizer (0.0.9)
|
25
|
+
i18n (0.6.9)
|
26
|
+
ice_nine (0.11.0)
|
27
|
+
minitest (4.7.5)
|
28
|
+
minitest-spec-context (0.0.3)
|
29
|
+
multi_json (1.8.4)
|
30
|
+
rake (10.1.1)
|
31
|
+
thread_safe (0.1.3)
|
32
|
+
atomic
|
33
|
+
tzinfo (0.3.38)
|
34
|
+
virtus (1.0.1)
|
35
|
+
axiom-types (~> 0.0.5)
|
36
|
+
coercible (~> 1.0)
|
37
|
+
descendants_tracker (~> 0.0.1)
|
38
|
+
equalizer (~> 0.0.7)
|
39
|
+
|
40
|
+
PLATFORMS
|
41
|
+
ruby
|
42
|
+
|
43
|
+
DEPENDENCIES
|
44
|
+
bundler
|
45
|
+
minitest (~> 4.2)
|
46
|
+
minitest-spec-context
|
47
|
+
rake
|
48
|
+
riagent-document!
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Dmitri Zagidulin
|
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,125 @@
|
|
1
|
+
## riagent-document
|
2
|
+
|
3
|
+
Riagent::Document is a Ruby object to JSON document conversion format, for persistence to Riak db.
|
4
|
+
|
5
|
+
### Features
|
6
|
+
* Convenient model definition language that is familiar to anyone who has worked with Ruby MVC frameworks
|
7
|
+
* Support for embedded documents for ease of object composition
|
8
|
+
* Serialization to and from JSON (which can be saved in Riak)
|
9
|
+
|
10
|
+
### Model/Document Definition
|
11
|
+
Riagent::Document uses [Virtus](https://github.com/solnic/virtus) for document attributes, defaults and coercions.
|
12
|
+
|
13
|
+
To turn a Ruby class into a Document, add ```riagent-document``` to your Gemfile,
|
14
|
+
and ```include``` Riagent::Document in your model class.
|
15
|
+
|
16
|
+
```ruby
|
17
|
+
include 'riagent-document'
|
18
|
+
|
19
|
+
# models/user.rb
|
20
|
+
class User
|
21
|
+
include Riagent::Document
|
22
|
+
|
23
|
+
attribute :username, String
|
24
|
+
attribute :email, String
|
25
|
+
attribute :country, String, default: 'USA'
|
26
|
+
end
|
27
|
+
```
|
28
|
+
|
29
|
+
### Conversion to and from JSON
|
30
|
+
```ruby
|
31
|
+
# Documents accept mass attribute assignment in the initializer
|
32
|
+
user = User.new username: 'Test User', email: 'test@user.com'
|
33
|
+
user.country = 'Canada'
|
34
|
+
|
35
|
+
# Convert to JSON string
|
36
|
+
user_string = user.to_json_document
|
37
|
+
# => '{"username":"Test User", "email":"test@user.com", "country":"Canada"}'
|
38
|
+
|
39
|
+
# Convert from JSON string back to an object instance
|
40
|
+
new_user = User.from_json(user_string)
|
41
|
+
puts new_user.inspect
|
42
|
+
# => #<User:0x00000102a9aaa8 @username="Test User", @email="test@user.com", ...>
|
43
|
+
```
|
44
|
+
|
45
|
+
### Embedding Documents
|
46
|
+
You can compose Documents out of complex types.
|
47
|
+
|
48
|
+
For example, if you were implementing an ```AddressBook``` object, you could embed ```Contact``` objects directly, using a Set.
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
class Contact
|
52
|
+
include Riagent::Document
|
53
|
+
|
54
|
+
attribute :contact_name, String
|
55
|
+
attribute :contact_email, String
|
56
|
+
end
|
57
|
+
|
58
|
+
class AddressBook
|
59
|
+
include Riagent::Document
|
60
|
+
|
61
|
+
attribute :user_key, String
|
62
|
+
attribute :contacts, Set[Contact]
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
66
|
+
Which would then allow you to serialize the whole composite document:
|
67
|
+
|
68
|
+
```ruby
|
69
|
+
address_book = AddressBook.new user_key: 'test-user123'
|
70
|
+
c1 = Contact.new contact_name: 'Joe', contact_email: 'joe@test.net'
|
71
|
+
c2 = Contact.new contact_name: 'Jane', contact_email: 'jane@test.net'
|
72
|
+
address_book.contacts << c1
|
73
|
+
address_book.contacts << c2
|
74
|
+
json_str = address_book.to_json_document
|
75
|
+
puts json_str
|
76
|
+
# '{ "user_key":"test-user123",
|
77
|
+
# "contacts":[
|
78
|
+
# {"contact_name":"Joe","contact_email":"joe@test.net"},
|
79
|
+
# {"contact_name":"Jane","contact_email":"jane@test.net"}
|
80
|
+
# ]
|
81
|
+
# }'
|
82
|
+
```
|
83
|
+
|
84
|
+
And then, of course, re-hydrate the JSON string back into document instances:
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
new_book = AddressBook.from_json(json_str)
|
88
|
+
puts new_book.inspect
|
89
|
+
# <AddressBook:0x00000102a38fd8 @user_key="test-user123",
|
90
|
+
# @contacts=#<Set: {#<Contact:0x00000102a38df8 @contact_name="Joe", @contact_email="joe@test.net">, #<Contact:0x00000102a387e0 @contact_name="Jane", @contact_email="jane@test.net">}>, ...>
|
91
|
+
```
|
92
|
+
|
93
|
+
### Persistence
|
94
|
+
Now that you can convert model objects into JSON strings and back, how do you actually save them to Riak?
|
95
|
+
|
96
|
+
#### 1. Manual - Riak Client
|
97
|
+
You can manually persist them to Riak by using the [Riak Ruby Client](https://github.com/basho/riak-ruby-client):
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
require 'riak-client'
|
101
|
+
# Get an instance of the model object
|
102
|
+
user = User.new username: 'Test User', email: 'test@user.com'
|
103
|
+
|
104
|
+
# Initialize a Riak client, and save
|
105
|
+
client = Riak::Client.new
|
106
|
+
riak_object = client.bucket("users").get_or_new("test-user-123") # a Riak::RObject for bucket "users" and key "test-user-123"
|
107
|
+
riak_object.content_type = "application/json"
|
108
|
+
riak_object.raw_data = user.to_json_document
|
109
|
+
riak_object.store
|
110
|
+
```
|
111
|
+
|
112
|
+
#### 2. Manual - RiakJson Client
|
113
|
+
Or save documents to [Solr](https://github.com/basho/yokozuna/)-indexed collections via the
|
114
|
+
[RiakJson Client](https://github.com/basho-labs/riak_json_ruby_client):
|
115
|
+
|
116
|
+
```ruby
|
117
|
+
require 'riak_json'
|
118
|
+
# Get an instance of the model object
|
119
|
+
user = User.new username: 'Test User', email: 'test@user.com'
|
120
|
+
|
121
|
+
# Insert into a RiakJson collection
|
122
|
+
client = RiakJson::Client.new
|
123
|
+
collection = client.collection("users")
|
124
|
+
collection.insert(user)
|
125
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
## -------------------------------------------------------------------
|
2
|
+
##
|
3
|
+
## Copyright (c) "2013" 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
|
+
|
@@ -0,0 +1,69 @@
|
|
1
|
+
## -------------------------------------------------------------------
|
2
|
+
##
|
3
|
+
## Copyright (c) "2013" 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 "riagent/document/version"
|
22
|
+
require "active_support/concern"
|
23
|
+
require 'active_support/json'
|
24
|
+
require 'active_support/core_ext/object/to_json'
|
25
|
+
require "virtus"
|
26
|
+
require "virtus/attribute"
|
27
|
+
|
28
|
+
module Riagent
|
29
|
+
module Document
|
30
|
+
extend ActiveSupport::Concern
|
31
|
+
|
32
|
+
included do
|
33
|
+
include Virtus.model # Virtus is used to manage document attributes
|
34
|
+
|
35
|
+
attr_accessor :key
|
36
|
+
alias_method :id, :key # document.id same as document.key, to maintain Rails idiom
|
37
|
+
end
|
38
|
+
|
39
|
+
# Returns the JSON representation of the document.
|
40
|
+
# Does not include the document +key+
|
41
|
+
def to_json_document
|
42
|
+
self.attributes.to_json
|
43
|
+
end
|
44
|
+
|
45
|
+
module ClassMethods
|
46
|
+
# Returns a Riagent::Document instance, given a JSON string
|
47
|
+
# @param [String] json_object JSON Object string
|
48
|
+
# @param [String] key Optional document key. If not provided, will be loaded from +_id+ attribute in the JSON itself
|
49
|
+
# @return [Riagent::Document, nil] Riagent::Document instance, or nil if the JSON string is nil/empty
|
50
|
+
def from_json(json_obj, key=nil)
|
51
|
+
return nil if json_obj.nil? or json_obj.empty?
|
52
|
+
attributes_hash = JSON.parse(json_obj)
|
53
|
+
return nil if attributes_hash.empty?
|
54
|
+
instance = self.instantiate(attributes_hash)
|
55
|
+
if key.nil?
|
56
|
+
# Load key from the JSON object
|
57
|
+
key = attributes_hash['_id']
|
58
|
+
end
|
59
|
+
instance.key = key
|
60
|
+
instance
|
61
|
+
end
|
62
|
+
|
63
|
+
# Returns Riagent::Document instance, given a Hash of attributes
|
64
|
+
def instantiate(attributes)
|
65
|
+
self.new attributes
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
## -------------------------------------------------------------------
|
2
|
+
##
|
3
|
+
## Copyright (c) "2013" 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
|
+
module Riagent
|
22
|
+
module Document
|
23
|
+
VERSION = "0.0.1"
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
## -------------------------------------------------------------------
|
3
|
+
##
|
4
|
+
## Copyright (c) "2013" Dmitri Zagidulin and Basho Technologies, Inc.
|
5
|
+
##
|
6
|
+
## This file is provided to you under the Apache License,
|
7
|
+
## Version 2.0 (the "License"); you may not use this file
|
8
|
+
## except in compliance with the License. You may obtain
|
9
|
+
## a copy of the License at
|
10
|
+
##
|
11
|
+
## http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
##
|
13
|
+
## Unless required by applicable law or agreed to in writing,
|
14
|
+
## software distributed under the License is distributed on an
|
15
|
+
## "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
16
|
+
## KIND, either express or implied. See the License for the
|
17
|
+
## specific language governing permissions and limitations
|
18
|
+
## under the License.
|
19
|
+
##
|
20
|
+
## -------------------------------------------------------------------
|
21
|
+
|
22
|
+
lib = File.expand_path('../lib', __FILE__)
|
23
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
24
|
+
require 'riagent/document/version'
|
25
|
+
|
26
|
+
Gem::Specification.new do |spec|
|
27
|
+
spec.name = "riagent-document"
|
28
|
+
spec.version = Riagent::Document::VERSION
|
29
|
+
spec.authors = ["Dmitri Zagidulin"]
|
30
|
+
spec.email = ["dzagidulin@gmail.com"]
|
31
|
+
spec.summary = %q{Ruby object to JSON document conversion format, for persistence to Riak db}
|
32
|
+
spec.description = spec.summary
|
33
|
+
spec.homepage = "https://github.com/dmitrizagidulin/riagent-document"
|
34
|
+
spec.license = "Apache 2.0"
|
35
|
+
|
36
|
+
spec.files = `git ls-files -z`.split("\x0")
|
37
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
38
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
39
|
+
spec.require_paths = ["lib"]
|
40
|
+
|
41
|
+
spec.add_runtime_dependency "virtus"
|
42
|
+
spec.add_runtime_dependency "activesupport", "~> 4.0"
|
43
|
+
|
44
|
+
spec.add_development_dependency "bundler"
|
45
|
+
spec.add_development_dependency "rake"
|
46
|
+
spec.add_development_dependency "minitest", "~> 4.2"
|
47
|
+
spec.add_development_dependency "minitest-spec-context"
|
48
|
+
end
|
@@ -0,0 +1,33 @@
|
|
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
|
+
class Contact
|
22
|
+
include Riagent::Document
|
23
|
+
|
24
|
+
attribute :contact_name, String
|
25
|
+
attribute :contact_email, String
|
26
|
+
end
|
27
|
+
|
28
|
+
class AddressBook
|
29
|
+
include Riagent::Document
|
30
|
+
|
31
|
+
attribute :user_key, String
|
32
|
+
attribute :contacts, Set[Contact]
|
33
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
## -------------------------------------------------------------------
|
2
|
+
##
|
3
|
+
## Copyright (c) "2013" 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
|
+
class User
|
22
|
+
include Riagent::Document
|
23
|
+
|
24
|
+
attribute :username, String
|
25
|
+
attribute :email, String
|
26
|
+
attribute :language, String, default: 'en'
|
27
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
## -------------------------------------------------------------------
|
2
|
+
##
|
3
|
+
## Copyright (c) "2013" 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 'minitest/autorun'
|
22
|
+
require 'minitest-spec-context'
|
23
|
+
require 'riagent/document'
|
24
|
+
require 'examples/models/user'
|
25
|
+
require 'examples/models/address_book'
|
@@ -0,0 +1,44 @@
|
|
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 'test_helper'
|
22
|
+
|
23
|
+
describe "a Riagent::Document" do
|
24
|
+
# See test/examples/models/user.rb
|
25
|
+
# User class includes the Riagent::Document mixin
|
26
|
+
let(:user_document) { User.new }
|
27
|
+
|
28
|
+
it "has a key" do
|
29
|
+
user_document.key.must_be_nil # first initialized
|
30
|
+
test_key = 'george'
|
31
|
+
user_document.key = test_key
|
32
|
+
user_document.key.must_equal test_key
|
33
|
+
# Test for the .id alias
|
34
|
+
user_document.id.must_equal test_key
|
35
|
+
end
|
36
|
+
|
37
|
+
it "has model attributes" do
|
38
|
+
user_document.attributes.count.must_equal 3 # :username, :email, :language
|
39
|
+
end
|
40
|
+
|
41
|
+
it "should respond to to_json_document()" do
|
42
|
+
assert user_document.respond_to?(:to_json_document)
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe "a Riagent::Document" do
|
4
|
+
|
5
|
+
# See test/examples/models/address_book.rb
|
6
|
+
it "can be composed of other embedded Documents" do
|
7
|
+
address_book = AddressBook.new user_key: 'earl-123'
|
8
|
+
c1 = Contact.new contact_name: 'Joe', contact_email: 'joe@test.net'
|
9
|
+
c2 = Contact.new contact_name: 'Jane', contact_email: 'jane@test.net'
|
10
|
+
address_book.contacts << c1
|
11
|
+
address_book.contacts << c2
|
12
|
+
json_str = address_book.to_json_document
|
13
|
+
|
14
|
+
new_book = AddressBook.from_json json_str
|
15
|
+
new_book.contacts.count.must_equal 2
|
16
|
+
contact = new_book.contacts.first
|
17
|
+
contact.contact_name.must_equal 'Joe'
|
18
|
+
contact.must_be_kind_of Contact
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,42 @@
|
|
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 'test_helper'
|
22
|
+
|
23
|
+
describe "a Riagent::Document" do
|
24
|
+
# See test/examples/models/user.rb
|
25
|
+
# User class includes the Riagent::Document mixin
|
26
|
+
|
27
|
+
it "can be converted to JSON and back" do
|
28
|
+
user = User.new username: 'earl', email: 'earl@desandwich.com'
|
29
|
+
json_obj = user.to_json_document
|
30
|
+
json_obj.must_be_kind_of String
|
31
|
+
|
32
|
+
new_user = User.from_json(json_obj)
|
33
|
+
new_user.must_be_kind_of User
|
34
|
+
new_user.username.must_equal 'earl'
|
35
|
+
end
|
36
|
+
|
37
|
+
it "from_json() returns nil if passed in an empty result string" do
|
38
|
+
User.from_json(nil).must_be_nil
|
39
|
+
User.from_json('').must_be_nil
|
40
|
+
User.from_json([].to_json).must_be_nil
|
41
|
+
end
|
42
|
+
end
|
metadata
ADDED
@@ -0,0 +1,150 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: riagent-document
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dmitri Zagidulin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-06-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: virtus
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: minitest
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '4.2'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '4.2'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: minitest-spec-context
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Ruby object to JSON document conversion format, for persistence to Riak
|
98
|
+
db
|
99
|
+
email:
|
100
|
+
- dzagidulin@gmail.com
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- ".gitignore"
|
106
|
+
- Gemfile
|
107
|
+
- Gemfile.lock
|
108
|
+
- LICENSE.txt
|
109
|
+
- README.md
|
110
|
+
- Rakefile
|
111
|
+
- lib/riagent/document.rb
|
112
|
+
- lib/riagent/document/version.rb
|
113
|
+
- riagent-document.gemspec
|
114
|
+
- test/examples/models/address_book.rb
|
115
|
+
- test/examples/models/user.rb
|
116
|
+
- test/test_helper.rb
|
117
|
+
- test/unit/document_test.rb
|
118
|
+
- test/unit/embedded_test.rb
|
119
|
+
- test/unit/json_conversion_test.rb
|
120
|
+
homepage: https://github.com/dmitrizagidulin/riagent-document
|
121
|
+
licenses:
|
122
|
+
- Apache 2.0
|
123
|
+
metadata: {}
|
124
|
+
post_install_message:
|
125
|
+
rdoc_options: []
|
126
|
+
require_paths:
|
127
|
+
- lib
|
128
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - ">="
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '0'
|
133
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - ">="
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '0'
|
138
|
+
requirements: []
|
139
|
+
rubyforge_project:
|
140
|
+
rubygems_version: 2.2.1
|
141
|
+
signing_key:
|
142
|
+
specification_version: 4
|
143
|
+
summary: Ruby object to JSON document conversion format, for persistence to Riak db
|
144
|
+
test_files:
|
145
|
+
- test/examples/models/address_book.rb
|
146
|
+
- test/examples/models/user.rb
|
147
|
+
- test/test_helper.rb
|
148
|
+
- test/unit/document_test.rb
|
149
|
+
- test/unit/embedded_test.rb
|
150
|
+
- test/unit/json_conversion_test.rb
|