Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Dynamic Method Calls (http://www.velocityreviews.com/forums/t862304-dynamic-method-calls.html)

Matt Mencel 04-01-2010 06:13 PM

Dynamic Method Calls
 
Right now I have some inefficient code that looks like this...

LdapAccount is my model.... and record.attributes is a hash of attribute name,value pairs.


records.each do |record|
LdapAccount.create(:uid => record.attributes["uid"].to_s) do |c|
c.buildingname = record.attributes["buildingName"].to_s if record.attributes.has_key?("buildingName")
c.cn = record.attributes["cn"].to_s if record.attributes.has_key?("cn")
c.department = record.attributes["department"].to_s if record.attributes.has_key?("department")
c.displayname = record.attributes["displayName"].to_s if record.attributes.has_key?("displayName")
c.employeetype = record.attributes["employeeType"].to_s if record.attributes.has_key?("employeeType")
c.givenname = record.attributes["givenName"].to_s if record.attributes.has_key?("givenName")
c.homedirectory = record.attributes["homeDirectory"].to_s if record.attributes.has_key?("homeDirectory")
c.mail = record.attributes["mail"].to_s if record.attributes.has_key?("mail")
end
end


There's probably 20 or more attributes I'll eventually be checking...I'm working with tens of thousands of records....and not all records have values for every attribute. So rather than do all the extra wasted work, I'd really like to be able to do something simpler like this...

records.each do |record|
LdapAccount.create(:uid => record.attributes["uid"].to_s) do |c|
record.attributes.each_pair do |k,v|
c.k = v
end
end
end

...but I get...
undefined method `k=' for #<LdapAccount:.....>

Did a little research and read about the send method....so I tried this...

c.send(k = v)


...but I get... (in this case the String value of v = "smtp.wiu.edu")
["smtp.wiu.edu"] is not a symbol


So I'm not sure I understand how to use the send method in this case. Can anyone offer some insight? Am I headed in the right direction or is there a better way to do this.

Thanks,
Matt





Andrea Dallera 04-01-2010 06:29 PM

Re: Dynamic Method Calls
 
Hei Matt,

try c.send(k, v)
I haven't tried that myself but it looks like what you want to accomplish.

Bye!

--
Andrea Dallera
http://github.com/bolthar/freightrain
http://usingimho.wordpress.com

On Thursday 01 April 2010 08:13:07 pm Matt Mencel wrote:
> Right now I have some inefficient code that looks like this...
>
> LdapAccount is my model.... and record.attributes is a hash of attribute
> name,value pairs.
>
>
> records.each do |record|
> LdapAccount.create(:uid => record.attributes["uid"].to_s) do |c|
> c.buildingname = record.attributes["buildingName"].to_s if
> record.attributes.has_key?("buildingName") c.cn =
> record.attributes["cn"].to_s if record.attributes.has_key?("cn")
> c.department = record.attributes["department"].to_s if
> record.attributes.has_key?("department") c.displayname =
> record.attributes["displayName"].to_s if
> record.attributes.has_key?("displayName") c.employeetype =
> record.attributes["employeeType"].to_s if
> record.attributes.has_key?("employeeType") c.givenname =
> record.attributes["givenName"].to_s if
> record.attributes.has_key?("givenName") c.homedirectory =
> record.attributes["homeDirectory"].to_s if
> record.attributes.has_key?("homeDirectory") c.mail =
> record.attributes["mail"].to_s if record.attributes.has_key?("mail") end
> end
>
>
> There's probably 20 or more attributes I'll eventually be checking...I'm
> working with tens of thousands of records....and not all records have
> values for every attribute. So rather than do all the extra wasted work,
> I'd really like to be able to do something simpler like this...
>
> records.each do |record|
> LdapAccount.create(:uid => record.attributes["uid"].to_s) do |c|
> record.attributes.each_pair do |k,v|
> c.k = v
> end
> end
> end
>
> ...but I get...
> undefined method `k=' for #<LdapAccount:.....>
>
> Did a little research and read about the send method....so I tried this...
>
> c.send(k = v)
>
>
> ...but I get... (in this case the String value of v = "smtp.wiu.edu")
> ["smtp.wiu.edu"] is not a symbol
>
>
> So I'm not sure I understand how to use the send method in this case. Can
> anyone offer some insight? Am I headed in the right direction or is there
> a better way to do this.
>
> Thanks,
> Matt
>



Andrea Dallera 04-01-2010 06:35 PM

Re: Dynamic Method Calls
 
Whoopsie, i'm really sorry. It should be something like:

c.send("#{k}=".to_sym, v)

On Thursday 01 April 2010 08:13:07 pm Matt Mencel wrote:
> Right now I have some inefficient code that looks like this...
>
> LdapAccount is my model.... and record.attributes is a hash of attribute
> name,value pairs.
>
>
> records.each do |record|
> LdapAccount.create(:uid => record.attributes["uid"].to_s) do |c|
> c.buildingname = record.attributes["buildingName"].to_s if
> record.attributes.has_key?("buildingName") c.cn =
> record.attributes["cn"].to_s if record.attributes.has_key?("cn")
> c.department = record.attributes["department"].to_s if
> record.attributes.has_key?("department") c.displayname =
> record.attributes["displayName"].to_s if
> record.attributes.has_key?("displayName") c.employeetype =
> record.attributes["employeeType"].to_s if
> record.attributes.has_key?("employeeType") c.givenname =
> record.attributes["givenName"].to_s if
> record.attributes.has_key?("givenName") c.homedirectory =
> record.attributes["homeDirectory"].to_s if
> record.attributes.has_key?("homeDirectory") c.mail =
> record.attributes["mail"].to_s if record.attributes.has_key?("mail") end
> end
>
>
> There's probably 20 or more attributes I'll eventually be checking...I'm
> working with tens of thousands of records....and not all records have
> values for every attribute. So rather than do all the extra wasted work,
> I'd really like to be able to do something simpler like this...
>
> records.each do |record|
> LdapAccount.create(:uid => record.attributes["uid"].to_s) do |c|
> record.attributes.each_pair do |k,v|
> c.k = v
> end
> end
> end
>
> ...but I get...
> undefined method `k=' for #<LdapAccount:.....>
>
> Did a little research and read about the send method....so I tried this...
>
> c.send(k = v)
>
>
> ...but I get... (in this case the String value of v = "smtp.wiu.edu")
> ["smtp.wiu.edu"] is not a symbol
>
>
> So I'm not sure I understand how to use the send method in this case. Can
> anyone offer some insight? Am I headed in the right direction or is there
> a better way to do this.
>
> Thanks,
> Matt
>



Ryan Davis 04-01-2010 06:42 PM

Re: Dynamic Method Calls
 

On Apr 1, 2010, at 11:35 , Andrea Dallera wrote:

> Whoopsie, i'm really sorry. It should be something like:
>
> c.send("#{k}=".to_sym, v)


to_sym is unnecessary




All times are GMT. The time now is 02:13 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.