Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting a reference to an object's parent... possible? #15

Open
markrickert opened this issue Jan 2, 2015 · 1 comment
Open

Getting a reference to an object's parent... possible? #15

markrickert opened this issue Jan 2, 2015 · 1 comment

Comments

@markrickert
Copy link

Hey there, I'm using Dish in RubyMotion and I really love it! Thanks so much for creating this library and making it compatible with RM!

Question about getting a reference to an object's parent...

I have:

{
  id: 12,
  items: [{
    name: "test1"
  },{
    name: "test2"
  }]
}

Defined by:

class Order < Dish::Plate
  coerce :items, Item
end
class Item < Dish::Plate
end

Is there a way to get an item's parent (Order) id?

# my_item is an Item
my_item.parent # Order object
my_item.parent.id # 12

Or if not, is there perhaps a way to set a weakref to the parent when initializing the objects?

class Order < Dish::Plate
  # Something like this?
  coerce :items, ->(value) { Item.new(value, self) }
end
class Item < Dish::Plate
  def initialize(hash, parent)
    super
    @parent = WeafRef.new(parent)
  end
end

Any ideas? Thanks again for the great library!

@lassebunk
Copy link
Owner

It's not possible right now, but I'll keep it in mind. Think it's a good idea. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants