riak_json 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f4829b641aa58acf10a0b5f6b8bb465a2dda3ae7
4
+ data.tar.gz: bfe725495387a621908a165cac547cfc282bd0e2
5
+ SHA512:
6
+ metadata.gz: 82ff9f6dc5beba9b516ec396c0ab1fb12604f3ad922d45d09db5c426016654c692b14197f7411748ef27e542570460905ed8fe34400ae4672c7fc5044a0f32b0
7
+ data.tar.gz: c17cd830fc7dc717b138f0621016afecfbc8e2eebeb22956163f1be4b79b9797c4cf24e8a259f1782b0b6da5026b77553de045a2a5517253817ba809cb2fddc7
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,24 @@
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
+ source 'https://rubygems.org'
22
+
23
+ # Specify your gem's dependencies in riak_json.gemspec
24
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,16 @@
1
+ Copyright 2013 Basho Technologies, Inc.
2
+
3
+ Licensed under the Apache License, Version 2.0 (the "License");
4
+ you may not use this file except in compliance with the License.
5
+ You may obtain a copy of the License at
6
+
7
+ http://www.apache.org/licenses/LICENSE-2.0
8
+
9
+ Unless required by applicable law or agreed to in writing, software
10
+ distributed under the License is distributed on an "AS IS" BASIS,
11
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ See the License for the specific language governing permissions and
13
+ limitations under the License.
14
+
15
+ All of the files in this project are under the project-wide license
16
+ unless they are otherwise marked.
data/README.md ADDED
@@ -0,0 +1,178 @@
1
+ # Riak Json Ruby Client
2
+
3
+ A Ruby client for [Riak Json](https://github.com/basho-labs/riak_json/).
4
+ For ActiveModel integration for RiakJson, see [riak_json-active_model](https://github.com/dmitrizagidulin/rj-activemodel) gem.
5
+
6
+ ## Installation
7
+ Since this gem is not released to the public yet, build it locally:
8
+
9
+ ```bash
10
+ git clone git@github.com:basho/riak_json_ruby_client.git
11
+ cd riak_json_ruby_client
12
+ rake build
13
+ gem install pkg/riak_json-0.0.2.gem
14
+ ```
15
+ ## Unit Testing
16
+ Use bundler to install dev dependencies:
17
+ ```
18
+ bundle install
19
+ ```
20
+ To run both unit and integration tests:
21
+ ```
22
+ bundle exec rake test
23
+ ```
24
+ Note: By default, integration tests assume that Riak is listening on ```127.0.0.1:8098```
25
+ (the result of ```make rel```).
26
+
27
+ To specify alternate host and port, use the ```RIAK_HOST``` and ```RIAK_PORT``` env variables:
28
+ ```
29
+ RIAK_HOST=127.0.0.1 RIAK_PORT=10018 bundle exec rake
30
+ ```
31
+ To run just the unit tests:
32
+ ```
33
+ bundle exec rake unittest
34
+ ```
35
+ To run just the integration tests:
36
+ ```
37
+ bundle exec rake itest
38
+ ```
39
+ ## Usage
40
+ ### Loading the Client Config File
41
+ ```ruby
42
+ require 'riak_json'
43
+
44
+ config_file = 'test/examples/riak.yml' # Loads in a config hash, by environment
45
+ dev_config = config_file['development']
46
+ client = RiakJson::Client.new(host=dev_config['host'], port=dev_config['http_port'])
47
+ ```
48
+
49
+ ### Creating / Referencing a Collection
50
+ ```ruby
51
+ require 'riak_json'
52
+
53
+ client = RiakJson::Client.new('localhost', 8098)
54
+ # A new or existing collection
55
+ collection = client.collection("cities")
56
+ ```
57
+
58
+ ### Schema Administration
59
+ ```ruby
60
+ # You may set an optional schema
61
+ # (or skip this step and go straight to inserting documents)
62
+ # Supported field types:
63
+ # - string (no spaces, think of a url slug)
64
+ # - text (spaces allowed)
65
+ # - multi_string (an array of strings, no spaces)
66
+ # - integer
67
+ schema = RiakJson::CollectionSchema.new
68
+ schema.add_text_field(name='city', required=true)
69
+ schema.add_string_field('state', true)
70
+ schema.add_multi_string_field('zip_codes') # required: false
71
+ schema.add_integer_field('population', false)
72
+ schema.add_string_field('country', true)
73
+
74
+ # Store the schema
75
+ collection.set_schema(schema)
76
+
77
+ # Check to see if schema is present
78
+ collection.has_schema? # => true
79
+
80
+ # Read a stored schema for a collection
81
+ schema_result = collection.get_schema()
82
+ # [{
83
+ # :name => "city",
84
+ # :type => "text",
85
+ # :require => true
86
+ # }, {
87
+ # :name => "state",
88
+ # :type => "string",
89
+ # :require => true
90
+ # }, {
91
+ # :name => "zip_codes",
92
+ # :type => "multi_string",
93
+ # :require => false
94
+ # }, {
95
+ # :name => "population",
96
+ # :type => "integer",
97
+ # :require => false
98
+ # }, {
99
+ # :name => "country",
100
+ # :type => "string",
101
+ # :require => true
102
+ # }]
103
+
104
+ # Delete the schema (and the index) for the collection
105
+ # WARNING: This deletes the index for the collection, so previously saved documents
106
+ # will not show up in queries!
107
+ collection.delete_schema
108
+ ```
109
+
110
+ ### Reading and Writing Documents
111
+ ```ruby
112
+ # You can insert a document with no key
113
+ # RiakJson generates a UUID type key and returns it
114
+ doc = RiakJson::Document.new
115
+ doc.body = { 'city'=>"Cleveland", 'state'=>'OH', 'country'=>'USA'}
116
+ doc.key # => nil
117
+ collection.insert(doc)
118
+ doc.key # => e.g. 'EmuVX4kFHxxvlUVJj5TmPGgGPjP'
119
+
120
+ # Populate the cities collection with data
121
+ doc = RiakJson::Document.new(
122
+ key="nyc",
123
+ body={ 'city'=>"New York", 'state'=>"NY", 'country'=>"USA" })
124
+ collection.insert(doc)
125
+ doc = RiakJson::Document.new(
126
+ key="boston",
127
+ body={ 'city'=>"Boston", 'state'=>"MA", 'country'=>"USA" })
128
+ collection.insert(doc)
129
+ doc = RiakJson::Document.new(
130
+ key="sf",
131
+ body={ 'city'=>"San Francisco", 'state'=>"CA", 'country'=>"USA" })
132
+ collection.insert(doc)
133
+
134
+ # Read a document (load by key)
135
+ doc = collection.find_by_key("nyc")
136
+ doc['city'] # => 'New York'
137
+ ```
138
+
139
+ ### Querying RiakJson - find_one() and find_all()
140
+ See [RiakJson Query Docs](https://github.com/basho-labs/riak_json/blob/master/docs/query.md)
141
+ for a complete list of valid query parameters.
142
+ ```ruby
143
+ # Exact match on "city" field
144
+ query = {"city" => "San Francisco"}.to_json
145
+ doc = collection.find_one(query)
146
+ # collection.find_one returns a Document instance
147
+ doc['city'] # => 'San Francisco'
148
+
149
+ # Find all documents that match the "country" field exactly
150
+ query = {"country" => "USA"}.to_json
151
+ results = collection.find_all(query)
152
+ results.documents.count # => 3
153
+ results.num_pages # => 1 -- total pages in result set
154
+ results.page # => 0 -- current page (zero-indexed)
155
+ results.per_page # results per page, defaults to 100
156
+ ```
157
+ #### Limiting Query Results
158
+ ```ruby
159
+ # Find all US cities, limit results to 10 per page
160
+ query = {'country'=>'USA', '$per_page'=>10}.to_json
161
+ results = collection.find_all(query)
162
+ results.per_page # => 10
163
+ ```
164
+ #### Page Offsets
165
+ ```ruby
166
+ # Find all US cities, retrieve 2nd page (zero-offset)
167
+ query = {'country'=>'USA', '$per_page'=>10, '$page'=>1}.to_json
168
+ results = collection.find_all(query)
169
+ results.page # => 1
170
+ ```
171
+
172
+ ## Contributing
173
+
174
+ 1. Fork it
175
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
176
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
177
+ 4. Push to the branch (`git push origin my-new-feature`)
178
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,40 @@
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 "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
@@ -0,0 +1,139 @@
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 'rest-client'
22
+ require 'erb'
23
+ require 'yaml'
24
+
25
+ module RiakJson
26
+ # Default hostname of the RiakJson test server
27
+ RIAK_TEST_HOST = '127.0.0.1'
28
+ # Default http port of the RiakJson test server
29
+ RIAK_TEST_PORT = 8098
30
+
31
+ # RiakJson::Client makes REST calls to the Riak Json API endpoints,
32
+ # on behalf of a Collection.
33
+ # Stores the details of a Riak/RiakJson HTTP connection (host, port),
34
+ # and manages a cache of collection references.
35
+ # Uses a pluggable ClientTransport component to make the actual HTTP requests.
36
+ class Client
37
+ attr_accessor :collection_cache
38
+ attr_accessor :transport
39
+ attr_accessor :host, :port
40
+
41
+ def initialize(host=RiakJson::RIAK_TEST_HOST, port=RiakJson::RIAK_TEST_PORT)
42
+ @collection_cache = {}
43
+ @transport = RiakJson::ClientTransport.new
44
+ @host = host
45
+ @port = port
46
+ end
47
+
48
+ def base_collection_url
49
+ "#{self.base_riak_json_url}/collection"
50
+ end
51
+
52
+ def base_riak_url
53
+ "http://#{self.host}:#{self.port}"
54
+ end
55
+
56
+ def base_riak_json_url
57
+ "#{self.base_riak_url}/document"
58
+ end
59
+
60
+ def collection(name)
61
+ self.collection_cache[name] ||= RiakJson::Collection.new(name, self)
62
+ end
63
+
64
+ def delete_json_object(collection_name, key)
65
+ self.transport.send_request("#{self.base_collection_url}/#{collection_name}/#{key}", :delete)
66
+ end
67
+
68
+ def delete_schema(collection_name)
69
+ self.transport.send_request("#{self.base_collection_url}/#{collection_name}/schema", :delete)
70
+ end
71
+
72
+ def get_json_object(collection_name, key)
73
+ self.transport.send_request("#{self.base_collection_url}/#{collection_name}/#{key}", :get)
74
+ end
75
+
76
+ def get_query_all(collection_name, query_json)
77
+ self.transport.send_request("#{self.base_collection_url}/#{collection_name}/query/all", :put, query_json)
78
+ end
79
+
80
+ def get_query_one(collection_name, query_json)
81
+ self.transport.send_request("#{self.base_collection_url}/#{collection_name}/query/one", :put, query_json)
82
+ end
83
+
84
+ def get_schema(collection_name)
85
+ self.transport.send_request("#{self.base_collection_url}/#{collection_name}/schema", :get)
86
+ end
87
+
88
+ # Sends a JSON document to a collection resource
89
+ # If a key is specified, issues a PUT to that key
90
+ # If key is nil, issues a POST to the collection, and returns the
91
+ # key generated by RiakJson
92
+ #
93
+ # @param format [String]
94
+ # @param key - can be nil
95
+ # @param json [String]
96
+ # @return [String] Returns the key for the inserted document
97
+ def insert_json_object(collection_name, key, json)
98
+ if key.nil?
99
+ key = self.post_to_collection(collection_name, json)
100
+ else
101
+ self.transport.send_request("#{self.base_collection_url}/#{collection_name}/#{key}", :put, json)
102
+ key
103
+ end
104
+ end
105
+
106
+ # Load a config file in YAML format
107
+ def self.load_config_file(config_file)
108
+ config_file = File.expand_path(config_file)
109
+ config_hash = YAML.load(ERB.new(File.read(config_file)).result)
110
+ end
111
+
112
+ # Perform an HTTP ping to the Riak cluster
113
+ def ping
114
+ response = self.transport.get_request("#{self.base_riak_url}/ping")
115
+ end
116
+
117
+ def post_to_collection(collection_name, json)
118
+ response = self.transport.send_request("#{self.base_collection_url}/#{collection_name}", :post, json)
119
+ if response.code == 201
120
+ location = response.headers[:location]
121
+ key = location.split('/').last
122
+ else
123
+ raise Exception, "Error inserting document into collection - key not returned"
124
+ end
125
+ key
126
+ end
127
+
128
+ def set_schema_json(collection_name, json)
129
+ self.transport.send_request("#{self.base_collection_url}/#{collection_name}/schema", :put, json)
130
+ end
131
+
132
+ def update_json_object(collection_name, key, json)
133
+ if key.nil? or key.empty?
134
+ raise Exception, "Error: cannot update document, key missing"
135
+ end
136
+ self.transport.send_request("#{self.base_collection_url}/#{collection_name}/#{key}", :put, json)
137
+ end
138
+ end
139
+ end
@@ -0,0 +1,48 @@
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 'rest-client'
22
+
23
+ module RiakJson
24
+ # Used by RiakJson::Client to make HTTP requests
25
+ class ClientTransport
26
+ def get_request(url)
27
+ self.send_request(url, :get)
28
+ end
29
+
30
+ def send_request(url, http_method, data=nil)
31
+ begin
32
+ case http_method
33
+ when :get
34
+ response = RestClient.get url, {:content_type => :json}
35
+ when :put
36
+ response = RestClient.put url, data, {:content_type => :json, :accept => :json}
37
+ when :post
38
+ response = RestClient.post url, data, {:content_type => :json, :accept => :json}
39
+ when :delete
40
+ response = RestClient.delete url
41
+ else
42
+ raise ArgumentError, "Invalid HTTP :method - #{http_method}"
43
+ end
44
+ end
45
+ response
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,107 @@
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
+ module RiakJson
22
+ # Manages document read and write operations to RiakJson
23
+ # Also manages collection schema administration.
24
+ class Collection
25
+ attr_accessor :name
26
+ attr_accessor :client
27
+
28
+ def initialize(collection_name, client)
29
+ if collection_name.nil? or collection_name.empty?
30
+ raise ArgumentError, "Invalid collection name (must not be nil or empty)"
31
+ end
32
+ @name = collection_name
33
+ @client = client
34
+ end
35
+
36
+ def delete_raw_json(key)
37
+ self.client.delete_json_object(self.name, key)
38
+ end
39
+
40
+ def delete_schema
41
+ self.client.delete_schema(self.name)
42
+ end
43
+
44
+ # Retrieve all documents for a given query json object
45
+ # @param [String] JSON object representing the query
46
+ # @return [QueryResult]
47
+ def find_all(query_json)
48
+ json_obj = self.client.get_query_all(self.name, query_json)
49
+ RiakJson::QueryResult.new(json_obj)
50
+ end
51
+
52
+ def find_by_key(key)
53
+ json_obj = self.get_raw_json(key)
54
+ body_hash = JSON.parse(json_obj)
55
+ RiakJson::Document.new(key, body_hash)
56
+ end
57
+
58
+ def find_one(query_json)
59
+ json_obj = self.client.get_query_one(self.name, query_json)
60
+ return nil if json_obj.nil? or json_obj.empty?
61
+ body_hash = JSON.parse(json_obj)
62
+ return nil if body_hash.empty?
63
+ key = body_hash['_id']
64
+ RiakJson::Document.new(key, body_hash)
65
+ end
66
+
67
+ def get_raw_json(key)
68
+ self.client.get_json_object(self.name, key)
69
+ end
70
+
71
+ def get_schema
72
+ self.client.get_schema(self.name)
73
+ end
74
+
75
+ def has_schema?
76
+ return true if get_schema rescue false
77
+ end
78
+
79
+ def insert(document)
80
+ key = self.insert_raw_json(document.key, document.to_json_document)
81
+ document.key = key
82
+ end
83
+
84
+ def insert_raw_json(key, json_obj)
85
+ key = self.client.insert_json_object(self.name, key, json_obj)
86
+ end
87
+
88
+ def remove(document)
89
+ self.delete_raw_json(document.key)
90
+ end
91
+
92
+ def set_schema(schema)
93
+ if schema.kind_of? RiakJson::CollectionSchema
94
+ schema = schema.build
95
+ end
96
+ self.client.set_schema_json(self.name, schema)
97
+ end
98
+
99
+ def update(document)
100
+ self.update_raw_json(document.key, document.to_json)
101
+ end
102
+
103
+ def update_raw_json(key, json_obj)
104
+ self.client.update_json_object(self.name, key, json_obj)
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,61 @@
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
+ module RiakJson
22
+ # Helper object for creating RiakJson schemas.
23
+ class CollectionSchema
24
+ attr_accessor :fields
25
+
26
+ def initialize
27
+ @fields = []
28
+ end
29
+
30
+ def add_field(field_type, field_name, required=false)
31
+ unless self.class.valid_field_types.include? field_type.to_sym
32
+ raise Exception, "Invalid field type"
33
+ end
34
+ self.fields << { name: field_name.to_s, type: field_type.to_s, require: required }
35
+ end
36
+
37
+ def add_integer_field(field_name, required=false)
38
+ self.add_field(:integer, field_name, required)
39
+ end
40
+
41
+ def add_multi_string_field(field_name, required=false)
42
+ self.add_field(:multi_string, field_name, required)
43
+ end
44
+
45
+ def add_string_field(field_name, required=false)
46
+ self.add_field(:string, field_name, required)
47
+ end
48
+
49
+ def add_text_field(field_name, required=false)
50
+ self.add_field(:text, field_name, required)
51
+ end
52
+
53
+ def build
54
+ self.fields.to_json
55
+ end
56
+
57
+ def self.valid_field_types
58
+ [:text, :string, :multi_string, :integer]
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,54 @@
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
+ module RiakJson
22
+ # Implements the RiakJson::Collection api (.key, .to_json_document)
23
+ # to easily store documents in a RiakJson::Collection
24
+ class Document
25
+ attr_accessor :key
26
+ attr_accessor :body
27
+ alias_method :attributes, :body
28
+
29
+ def initialize(key=nil, body={})
30
+ @key = key
31
+ @body = body
32
+ end
33
+
34
+ def [](key)
35
+ @body[key]
36
+ end
37
+
38
+ def []=(key, value)
39
+ @body[key] = value
40
+ end
41
+
42
+ # Returns a JSON string representation
43
+ def to_json
44
+ self.body.to_json
45
+ end
46
+
47
+ # Returns a JSON string representation.
48
+ # Invoked by RiakJson::Collection to serialize
49
+ # an object for writing to RiakJson
50
+ def to_json_document
51
+ self.to_json
52
+ end
53
+ end
54
+ end