forked from cardmagic/dm-more
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add JsonObject type to facilitate storing object serialized in json t…
…o/from couch
- Loading branch information
Wesley Beary
committed
Jul 17, 2008
1 parent
9f0f338
commit 0a8eab9
Showing
3 changed files
with
37 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
adapters/dm-couchdb-adapter/lib/couchdb_adapter/json_object.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |