Google Groups Home
Help | Sign in
Invoke block in current binding?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Kip Cole  
View profile
 More options Jul 26, 7:43 pm
Newsgroups: comp.lang.ruby
From: Kip Cole <kipco...@gmail.com>
Date: Sat, 26 Jul 2008 18:43:08 -0500
Local: Sat, Jul 26 2008 7:43 pm
Subject: Invoke block in current binding?
I have a design pattern (Rails app, using RestfulAuthentication plugin
with modifications) that wants to have a method that will temporarily
elevate user privileges like so:

def sudo
  saved_current_user = current_user
  current_user = admin_user
  yield
  current_user = saved_current_user
end

And in a method, invoke it with

sudo do
  puts "Something done as admin user"
end

However I know this doesn't work because at the time the block is
passed, the binding is fixed to the defining context.

So the question is: what is the right way to achieve the desired result.
Where the desired result is to pass a block to a method to be executed
in a different context, without the caller having to know any of the
implementation details.

Thanks for any help,

--Kip
--
Posted via http://www.ruby-forum.com/.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Joel VanderWerf  
View profile
 More options Jul 27, 3:19 pm
Newsgroups: comp.lang.ruby
From: Joel VanderWerf <vj...@path.berkeley.edu>
Date: Sun, 27 Jul 2008 14:19:15 -0500
Local: Sun, Jul 27 2008 3:19 pm
Subject: Re: Invoke block in current binding?

Kip Cole wrote:
> So the question is: what is the right way to achieve the desired result.
> Where the desired result is to pass a block to a method to be executed
> in a different context, without the caller having to know any of the
> implementation details.

What about this?

Context = Struct.new :user

def sudo
   context = Context.new
   context.user = "admin_user"
   yield context
end

outer_context = Context.new
outer_context.user = "normal_user"
puts "I am #{outer_context.user}"

sudo do |context|
   puts "I am #{context.user}"
end

--
       vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kip Cole  
View profile
 More options Jul 28, 4:58 pm
Newsgroups: comp.lang.ruby
From: Kip Cole <kipco...@gmail.com>
Date: Mon, 28 Jul 2008 15:58:46 -0500
Local: Mon, Jul 28 2008 4:58 pm
Subject: Re: Invoke block in current binding?
Joel, thanks very much, much appreciated.

--Kip

--
Posted via http://www.ruby-forum.com/.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google