Skip to content

Commit

Permalink
hashes with symbolized keys; persist classes (even inside hashes and …
Browse files Browse the repository at this point in the history
…arrays); updated readme
  • Loading branch information
Nico Hagenburger committed Oct 11, 2009
1 parent 26eee7d commit 78376f8
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -46,6 +46,7 @@ The server URL will default to http://localhost:5984/ unless specified:
Add to your config/environment.rb:

config.gem 'langalex-couch_potato', :lib => 'couch_potato', :source => 'http://gems.github.com'
config.frameworks -= [:active_record, :active_resource]

Then create a config/couchdb.yml:

Expand Down
13 changes: 13 additions & 0 deletions lib/core_ext/hash.rb
@@ -0,0 +1,13 @@
class Hash

# def self.json_create hash
# hash.symbolize_keys!
# if hash[:ruby_class]
# klass = eval(hash.delete(:ruby_class))
# klass.new(hash)
# else
# hash
# end
# end

end
47 changes: 43 additions & 4 deletions lib/couch_potato/persistence/simple_property.rb
Expand Up @@ -10,16 +10,55 @@ def initialize(owner_clazz, name, options = {})
define_accessors accessors_module_for(owner_clazz), name, options
end

# def build(object, json)
# value = json[name.to_s] || json[name.to_sym]
# typecast_value = if type
# type.json_create value
# else
# value
# end
# object.send "#{name}=", typecast_value
# end

def build(object, json)
value = json[name.to_s] || json[name.to_sym]
typecast_value = if type
type.json_create value
else
value
end
type.json_create value
else
build_value(value)
end
object.send "#{name}=", typecast_value
end

def build_value(value)
if value.is_a?(Hash)
build_hash(value)
elsif value.is_a?(Array)
build_array(value)
else
value
end
end

def build_array(array)
array.collect do |value|
build_value(value)
end
end

def build_hash(hash)
hash.symbolize_keys!
if hash[:ruby_class]
klass = eval(hash.delete(:ruby_class))
klass.new(hash)
else
hash.each_pair do |key, value|
hash[key] = build_value(value)
end
hash
end
end

def dirty?(object)
object.send("#{name}_changed?")
end
Expand Down
43 changes: 41 additions & 2 deletions spec/property_spec.rb
Expand Up @@ -54,12 +54,51 @@ class CuckooClock < Watch
end

it "should persist a hash" do
c = Comment.new :title => {'key' => 'value'}
c = Comment.new :title => {:key => 'value'}
CouchPotato.database.save_document! c
c = CouchPotato.database.load_document c.id
c.title.should == {'key' => 'value'}
c.title.should == {:key => 'value'}
end

def it_should_persist value
c = Comment.new :title => value
CouchPotato.database.save_document! c
c = CouchPotato.database.load_document c.id
c.title.should == value
end

it "should persist a child class" do
it_should_persist Child.new(:text => 'some text')
end

it "should persist a hash with a child class" do
it_should_persist :child => Child.new(:text => 'some text')
end

it "should persist an array with a child class" do
it_should_persist [Child.new(:text => 'some text')]
end

it "should persist something very complex" do
something_very_complex = [
[
[
{
:what => [
{
:ever => Child.new(:text => 'some text')
}
],
:number => 3
},
"string"
],
Child.new(:text => 'nothing')
]
]
it_should_persist something_very_complex
end

it "should persist a Time object" do
w = Watch.new :time => Time.now
CouchPotato.database.save_document! w
Expand Down
20 changes: 20 additions & 0 deletions spec/spec_helper.rb
Expand Up @@ -16,6 +16,12 @@ class Comment
property :title
end

class Child
include CouchPotato::Persistence

property :text
end

def recreate_db
CouchPotato.couchrest_database.delete! rescue nil
CouchPotato.couchrest_database.server.create_db CouchPotato::Config.database_name
Expand All @@ -27,3 +33,17 @@ def recreate_db
string =~ regex
end
end

# Tests should pass without Rails.
class Hash
def symbolize_keys
inject({}) do |options, (key, value)|
options[(key.to_sym rescue key) || key] = value
options
end
end

def symbolize_keys!
self.replace(self.symbolize_keys)
end
end

1 comment on commit 78376f8

@langalex
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, i did add your specs and they now pass without adding all that code, because of this http://github.com/railsbros/couch_potato/commit/ed9ea16bd6cf6bf6889ce074c1ac6679bb1132cc

Please sign in to comment.