GithubHelp home page GithubHelp logo

Comments (5)

langalex avatar langalex commented on July 18, 2024

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.

michaelkirk avatar michaelkirk commented on July 18, 2024

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.

langalex avatar langalex commented on July 18, 2024

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.

michaelkirk avatar michaelkirk commented on July 18, 2024

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.

langalex avatar langalex commented on July 18, 2024

btw. so is couch potato. it uses callbacks and dirty tracking and is activemodel compliant.

from couch_potato.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.