Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

hashes with symbolized keys; persist classes (even inside hashes and …

…arrays); updated readme
  • Loading branch information...
commit 78376f8ab77b8030e535e3a7086111aa32e50921 1 parent 26eee7d
Nico Hagenburger authored
View
1  README.md
@@ -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:
View
13 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
View
47 lib/couch_potato/persistence/simple_property.rb
@@ -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
View
43 spec/property_spec.rb
@@ -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
View
20 spec/spec_helper.rb
@@ -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
@@ -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

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.
Something went wrong with that request. Please try again.