Spira: method resolution of has_many resources not working sometimes

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?
--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
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?

Received on Thursday, 19 May 2011 22:08:21 UTC