karmi/tire — pull request #865 by FestivalBobcats (2013-09-08 21:15:20+00:00)
Title: :class instance of a property value should be able to reference the "embedding" doc (or parent)

In the style of Mongoid's embedded documents, it would be nice (really nice) for property classes to reference their respective "owner" or "parent" docs (```article.comments.first.article```). The example below will hopefully illustrate the problem. The only existing solutions are accessor methods in the root model (e.g. ```article.comment_posted_within(comment)```) or storing the needed attributes from the root model (such as :created_at in the test) within each embedded object, which is probably a little overkill.

```ruby
class Comment
def initialize(article, attrs)
@article = article
@attributes = HashWithIndifferentAccess.new(attrs)
end

def posted_within
posted_at - @article.created_at
end

# ... method_missing and whatnot
end

class Article
include Tire::Model::Persistence

property :created_at, :type => 'date'
property :comments, :class => [Comment]
end

now = Time.now
posted_at = now + 10.minutes
article = Article.new :created_at => now,
:comments => [{:body => 'comment body', posted_at: posted_at}]
assert_equal article.comments.first.posted_within, 10.minutes
```