On 21/05/11 5:49 AM, Gazzy wrote:
Does anyone know how to get Ruby/Sinatra models to execute initialize
methods within DataMapper models?

I am trying to add a Mutex class variable
your code is adding an *instance* variable, they are very different things.

to one of my Datamapper
classes so I can syncronize access to some of the methods but I am
getting the error - undefined method `synchronize' for nil:NilClass

My code looks like ...

class Booking
   include DataMapper::Resource
   ...
   def initialize(app, options)
     super
     @lock = Mutex.new
   end
   ...
   def take_slot
     # Record there is 1 less available slot
     @lock.synchronize do
        ...
     end
   end

but the initialize class never gets called.  All the mutex examples on
the internet don't use Datamapper models.

I started with a simple
def initialize
   super
   @lock = Mutex.new
end

then I realised that DataMapper probably has a more complex set of
parameters for the initialise function.  Hours of googling led me to
the above code but any help much appreciated.  I haven't yet figured
out how to look into gems to see what is going on under the hood.

There is an old note related to this but I don't understand the
solution proposed.
http://datamapper.lighthouseapp.com/projects/20609/tickets/1043-datamapperresource-is-not-calling-initialize-definined-in-models

I've had to bodge it for now with code like
def take_slot
   if !defined?(@lock)
     @lock = Mutex.new
   end
   # Record there is 1 less available slot
   @lock.synchronize do
    ...
end
This doesn't answer your question, but:

def lock
  @lock ||= Mutex.new
end

def take_slot
  lock.synchronize do
    ...
  end
end

is not so ugly.


but its ugly :-(  Any help much appreciated.

I also tried posting on the Sinatrarb Google group but no joy there.


What is the actual problem you are trying to solve? I have a suspicion Mutex doesn't do what you think it does in this context.

Cheers,
Xavier

--
You received this message because you are subscribed to the Google Groups 
"DataMapper" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/datamapper?hl=en.

Reply via email to