Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add JsonObject type to facilitate storing object serialized in json t…

…o/from couch
  • Loading branch information...
commit 0a8eab9da694666bc4544288bb729f4319e3ec6f 1 parent 9f0f338
Wesley Beary authored
View
1  adapters/dm-couchdb-adapter/Manifest.txt
@@ -5,6 +5,7 @@ README.txt
Rakefile
TODO
lib/couchdb_adapter.rb
+lib/couchdb_adapter/json_object.rb
lib/couchdb_adapter/version.rb
lib/couchdb_adapter/view.rb
spec/couchdb_adapter_spec.rb
View
1  adapters/dm-couchdb-adapter/lib/couchdb_adapter.rb
@@ -6,6 +6,7 @@
require 'net/http'
require 'pathname'
require 'uri'
+require Pathname(__FILE__).dirname + 'couchdb_adapter/json_object'
require Pathname(__FILE__).dirname + 'couchdb_adapter/view'
module DataMapper
View
35 adapters/dm-couchdb-adapter/lib/couchdb_adapter/json_object.rb
@@ -0,0 +1,35 @@
+require 'json'
+
+# Non-lazy objects that serialize to/from JSON, for use with couchdb
+module DataMapper
+ module Types
+ class JsonObject < DataMapper::Type
+ primitive String
+ size 65535
+
+ def self.load(value, property)
+ if value.nil?
+ nil
+ elsif value.is_a?(String)
+ ::JSON.load(value)
+ else
+ raise ArgumentError.new("+value+ must be nil or a String")
+ end
+ end
+
+ def self.dump(value, property)
+ if value.nil?
+ nil
+ elsif value.is_a?(String)
+ value
+ else
+ ::JSON.dump(value)
+ end
+ end
+
+ def self.typecast(value, property)
+ value
+ end
+ end # class JsonObject
+ end # module Types
+end # module DataMapper
Please sign in to comment.
Something went wrong with that request. Please try again.