Re: Spira: method resolution of has_many resources not working sometimes

I'm not sure I understand what behavior you're expecting. I am not
snipping anything so you can respond to where I am missing something.

On Thu, May 19, 2011 at 5:07 PM, Christoph Badura <bad@bsd.de> wrote:
> While trying ot use Spira resources as models in Rails 3 I have hit a
> snag.  Instantiating a Spira::Resource with a has_many property pointing
> to another resource from a repository produces objects that are unsuable
> for my purposes.
>
> My immediate problem is that obj.has_many_relation.first.some_method
> fails to invoke the instance method that is defined by
> obj.has_many_relation.first.class instead it ends up going through
> promise's method_missing and possibly invoking the wrong method.
>
> Run the following example in irb to see what is happening.  While
> writing that test case I also discovered that
>    SomeModel.for('123', :has_many_property => other_model_instance)
> messes up the subject of the object recoded in the has_many_property.

That's to be expected; it wants a Set or Array. Try :has_many_property
=> [ other_instance]

> Any ideas?
> --chris
>
> require 'rubygems'
> require 'spira'
>
> EX = RDF::Vocabulary.new 'http://example.com/ns'
>
> class Object
>  def to_zorch
>    "boom!"
>  end
> end
>
> class A
>  include Spira::Resource
>
>  base_uri 'http://example.com/a/'
>  type URI.new 'http://example.com/types#B'
>
>  property :foo, :predicate => EX.foo
>
>  def to_zorch
>    subject.to_s
>  end
> end
>
> class B
>  include Spira::Resource
>
>  base_uri 'http://example.com/b/'
>  type URI.new 'http://example.com/types#B'
>
>  property :foo, :predicate => EX.foo
>  has_many :bar, :predicate => EX.bar, :type => :A
> end
>
> Spira.add_repository(:default, RDF::Repository.new)
>
> x = A.for('123', :foo => 'a foo').save!
> B.for('456', :foo => 'b foo', :bar => x).save!
>
> a = A.for '123'
> a.attributes
> b = B.for '456'
> b.attributes
> ab = b.bar.first
> puts 'boom!' if a.subject != ab.subject # XXX @subject wrong
> c = B.for '789'
> c.foo = 'c foo'
> c.bar << x
> c.save!
> c.attributes
> c.bar.first.super
> c.bar.first.to_zorch
> c.reload
> c.bar.first.super
> c.bar.first.to_zorch    # XXX fails

What fails? What is the expected behavior? For me, c.bar.first is an
A, which has a defined #to_zorch. When I call it, I get A's subject?

The relevent lines are:
x = A.for('123', :foo => 'a foo').save!
c = B.for '789'
c.bar << x
c.save!
c.reload
c.bar.first.super # ?? there is no such method?
c.bar.first.to_zorch    # returns A's subject, after calling A#to_zorch

Do you expect this to be "boom!"? Why?

Ben




> From: Christoph Badura <bad@k.bsd.de>
> To: public-rdf-ruby@w3.org
> Cc:
> Bcc:
> Subject: Spira: method resolution of has_many resources not working
>  sometimes
> Reply-To:
>
> While trying ot use Spira resources as models in Rails 3 I have hit a
> snag.  Instantiating a Spira::Resource with a has_many property pointing
> to another resource from a repository produces objects that are unsuable
> for my purposes.
>
> My immediate problem is that obj.has_many_relation.first.some_method fails
> to invoke the instance method that is defined by
> obj.has_many_relation.first.class instead it ends up going through
> promise's method_missing and possibly invoking the wrong method.
>
> Run the following example in irb to see what is happening.
> While writing that test case I also discovered that
>  SomeModel.for('123', :has_many_property => other_model_instance)
> messes up the subject of the object recoded in the has_many_property.
>
> Any ideas?
>
>



-- 
Ben Lavender | ben@dydra.com | http://dydra.com
twitter/github: bhuga | +15047221016

Received on Friday, 20 May 2011 06:56:32 UTC