How can I do this without eval...

J

J2M

def load_default_attributes(model_name, options = {})
fixture = options[:fixture] || :default
attributes = temp_attributes = model = {}
instance_eval <<-FIN
temp_attributes = #{model_name.to_s.pluralize}("#{fixture}")
model = #{model_name.to_s.capitalize}
FIN
 
E

Eero Saynatkari

James said:
def load_default_attributes(model_name, options = {})
fixture = options[:fixture] || :default
attributes = temp_attributes = model = {}
instance_eval <<-FIN
temp_attributes = #{model_name.to_s.pluralize}("#{fixture}")

send model_name.to_s.pluralize, fixture
model = #{model_name.to_s.capitalize}

self.class.const_get model_name.to_s.capitalize
FIN
.
.
.
end

I know there must be a neater way to get hold of a class (or in this
case a Rails model) and with the temp_attributes to get hold of a
fixture, can anybody shed some metaprogramming light?

The above should work in principle; however, take a
look at ri Object#send and Module#const_get in case
you run into any problems.

(Disclaimer: respondent is not proficient with Rails.)
 
J

Jan Svitok

def load_default_attributes(model_name, options = {})
fixture = options[:fixture] || :default
attributes = temp_attributes = model = {}
instance_eval <<-FIN
temp_attributes = #{model_name.to_s.pluralize}("#{fixture}")
model = #{model_name.to_s.capitalize}
FIN

Hi,

model = const_get(model_name.to_s.capitalize)

Classes are constants. So you can get them by name (string or symbol)
with Module#const_get

temp_attributes... I don't know what fixtures are, I'll just say that you can
- call a method with Object#send(name, arg1, arg2) or send(name, *args)
- get/set instance variable with
instance_variable_get/instance_variable_set (don't forget the @!
my_object.instance_variable_get('@name') )
- class variables with Module#class_variable_get/.._set

and generally, look into Module, Class, Object and Kernel for other
useful methods ;-)

finally http://www.rubycentral.com/book/ospace.html can be helpful.
 
P

Paul Duncan

--GyRA7555PLgSTuth
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

* J2M ([email protected]) said:
=20
Jan Svitok wrote:
=20

This might be more appropriate:

model_class =3D const_get(model_name.to_s.classify)

Classify will take care of things that capitalize misses, like
underscores and extraneous plurals. Take a look at the following
output:

# test strings and test methods
strings =3D %w{blog_post layers magazine_subscribers}
methods =3D %w{capitalize classify}.map { |m| m.intern }

# iterate over test strings and handle each one
puts strings.map { |str|
"'#{str}'\n" << methods.map { |meth|=20
" #{meth}:\t'#{str.send(meth)}'"=20
}.join("\n")
}

Produces the following:

'blog_post'
capitalize: 'Blog_post'
classify: 'BlogPost'
'layers'
capitalize: 'Layers'
classify: 'Layer'
'magazine_subscribers'
capitalize: 'Magazine_subscribers'
classify: 'MagazineSubscriber'

Hope that helps...

--=20
Paul Duncan <[email protected]> pabs in #ruby-lang (OPN IRC)
http://www.pablotron.org/ OpenPGP Key ID: 0x82C29562

--GyRA7555PLgSTuth
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: Digital signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (GNU/Linux)

iD8DBQFE+ecczdlT34LClWIRAnSXAJ9tlpxaaTBSuUugQDptEXUtoKK79QCfTrkf
09ZARqbi2tjeZ7Eur94AtoI=
=3rEl
-----END PGP SIGNATURE-----

--GyRA7555PLgSTuth--
 
E

Ezra Zygmuntowicz

This might be more appropriate:

model_class = const_get(model_name.to_s.classify)

Also if you are already within rails you may as well use #constantize

model_class = model_name.to_s.classify.constantize

-Ezra
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top