Comments (5)
the simplest way would be to just add objects of that class to the list. if you want some kind of guard to make sure no one adds objects of other types to the list you could overwrite the setter, e.g.
def my_list=(value)
raise ArgumentError if value.find{|item| !item.is_a?(MyListType)}
super
end
from couch_potato.
But when you load a document, the list won't be cast appropriately.
This is my current solution to that:
# book.rb
class Book
property :chapters, :type => ChapterArray, :default => ChapterArray.new
end
#chapter_array.rb
class ChapterArray < Array
def self.json_create(hashes)
chapters= hashes.map {|h| Chapter.json_create(h)}
return ChapterArray.new(chapters)
end
end
One of the things I like about couchdb vs. a relation database is the ability to store a one-to-many relationship within the document, without having to do a join.
Do you think it would make sense to allow the property method to have a cardinality parameter, and encapsulate this pattern of casting a list of a type? Or am I opening a can of worms?
from couch_potato.
yes it will, as long as the hashes in the document have the appropriate ruby_class
attribute.
CouchRest has an option where you can say something like this:
property :my_list, type: [MyType]
This could be added to couch potato, so far nobody has bothered yet.
from couch_potato.
Just in case anyone is following this, "property" was pulled out of CouchRest into the CouchRest Extended document gem, which in turn seems to have been superseded by CouchRest Model[1]
I haven't looked into much, but it seems like CouchRest Model is leveraging a lot of the ActiveModel code from Rails3.
Thanks for the lead Alexander.
[1]https://github.com/couchrest/couchrest_model
from couch_potato.
btw. so is couch potato. it uses callbacks and dirty tracking and is activemodel compliant.
from couch_potato.
Related Issues (20)
- how update field HOT 3
- Change format of #attributes HOT 2
- Custom port for database HOT 3
- Date patch is affecting lograge timestamp gem HOT 12
- CouchPotato.database.load_document does not instantiate the correct class HOT 6
- CouchDB Authentication HOT 1
- CouchRest 2.0 HOT 3
- Merge 1.x branch into master HOT 1
- Return result object from db queries
- Mango Support
- improve query interface HOT 1
- add caching
- validatations don't use default context
- Better error message for mismatching property type for [String]
- document.reload should not read from cache
- New release?
- write conflicts not detected because of dirty tracking and race condition
- Does sync/replication work?
- Confusion about the create_id
- Couchdb timeout option HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from couch_potato.