Skip to content

Commit

Permalink
add JsonObject type to facilitate storing object serialized in json t…
Browse files Browse the repository at this point in the history
…o/from couch
  • Loading branch information
Wesley Beary committed Jul 17, 2008
1 parent 9f0f338 commit 0a8eab9
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
1 change: 1 addition & 0 deletions adapters/dm-couchdb-adapter/Manifest.txt
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions adapters/dm-couchdb-adapter/lib/couchdb_adapter.rb
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions 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

0 comments on commit 0a8eab9

Please sign in to comment.