Assigning values to a session? Ben Curtis's OpenID example?

I’m taking apart Ben Curtis’s OpenID / RESTful authentication ‘head
start’ and trying to understand it enough to integrate it into my app.

registration is a hash containing the valid sreg keys given

above

use this to map them to fields of your user model

def assign_registration_attributes!(registration)
{ :login => ‘nickname’, :email => ‘email’ }.each do |
model_attribute, registration_attribute|
unless registration[registration_attribute].blank?
@user.send("#{model_attribute}=",
registration[registration_attribute])
end
end
@user.save!
end
I’ve never seen .each used in this fashion? Why is he using the
“.each” when he appears to be just operating on ONE record?
What does he mean by “sreg” keys?
What does he mean to “use this to map them to fields of your user
model”?
Could someone explain in ‘human words’ what is going on here?
What do these variable mean/do? “model_attribute,
registration_attribute”
Thank you,
David

BraveDave wrote:

I’m taking apart Ben Curtis’s OpenID / RESTful authentication ‘head
start’ and trying to understand it enough to integrate it into my app.

registration is a hash containing the valid sreg keys given

above

use this to map them to fields of your user model

def assign_registration_attributes!(registration)
{ :login => ‘nickname’, :email => ‘email’ }.each do |
model_attribute, registration_attribute|
unless registration[registration_attribute].blank?
@user.send("#{model_attribute}=",
registration[registration_attribute])
end
end
@user.save!
end
I’ve never seen .each used in this fashion? Why is he using the
“.each” when he appears to be just operating on ONE record?
What does he mean by “sreg” keys?
What does he mean to “use this to map them to fields of your user
model”?

I changed the code as follows

{ :login => ‘nickname’, :email => ‘email’ }.each do |
model_attribute, registration_attribute|
puts “In this iteration we have model_attribute = ‘#{model_attribute}’
and registration_attribute = ‘#{registration_attribute}’”
end

Let’s see what irb comes up with:

irb

Welcome to irb. Method suc performs User.current =
User.find_by_user_name(‘admin’)
irb(main):001:0>
irb(main):002:0* { :login => ‘nickname’, :email => ‘email’ }.each do
|
irb(main):003:1* model_attribute, registration_attribute|
irb(main):004:1* puts “In this iteration we have model_attribute =
‘#{model_attribute}’ and registration_attribute =
‘#{registration_attribute}’”
irb(main):005:1> end
In this iteration we have model_attribute = ‘email’ and
registration_attribute = ‘email’
In this iteration we have model_attribute = ‘login’ and
registration_attribute = ‘nickname’
=> {:email=>“email”, :login=>“nickname”}

Ignore the last line; irb gives you the result of the last expression.
You’re looking at a looping construct. Hope that explains a little.

Stephan

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs