Partials every-where

March 3, 2010 at 11:57 am (Partials) (, , , )

Hi everybody,

Partials are not limited to be used for HTML rendering.

They can be used for XML too.

Here is an example file ( _details.builder )
xml.name user.name
xml.gender user.gender
xml.age user.age

This can be used in another regular builder file as follows:
xml.instruct! : xml, :version=>”1.0″
xml.users {
for user in users
xml <“users/details” , :locals=>{:user=>user})
end
}

Hope, this comes handy 🙂

Permalink 1 Comment

Undefining class at runtime

June 16, 2009 at 6:58 am (Model) (, , , , , , , )

This post explains undefining the class at runtime.

Im not gonna explain the live-cases where this might be used. Ask Uncle Google for that.

Lets jump in at d code directly.

When u create a new class, say “Person”, two things happen:

  1. A new object of type(class) Class is instantiated.
  2. A new constant called Person will be instantiated and will hold the value of the class-object created above.

Proof: Class Person;end;

Try, Person.class #=> Class. This means that the Person holds a value which is of class Class. (1 is proved).

Try, Person = 123 #=> Curses saying, Person is already initialized, which means Person is a constant. (2 is proved).

Now, All the constants(Person) and other are silently ducked into the class Object.

Proof: Object.class_eval{Person} #=> Person Class

So, we should understand by now that the newly created class Person resides as a constant of the class Object.

So if we could somehow remove that constant, then the class Person will also be removed.

There is a method called remove_const for the class Object.

So, try Object.remove_const : Person #=> Curses, NoMethodError: private method `remove_const’ called for Object:Class

This is becoz, remove_const is a private method of Object class.

So the worry now is, how to invoke a private method.

We have two solutions:

  1. Object.send(:remove_const , : Person)
  2. Object.class_eval{remove_const : Person}

Now try, Person.new. # Curses, uninitialized constant Person

Voila, job done. Very rarely does an ERROR MESSAGE makes us feel happy. Well, kind of an article.

You can learn much about class_eval from this very good article by Khaled.

Permalink Leave a Comment

add attribute at runtime

June 16, 2009 at 6:23 am (Model) (, , , , , , )

I’m all excited abt this post.

Coz this is something i learnt just now, and consider it my best learning till now in RUBY.

Q: How to add attributes to a class at runtime?

A:

class Class
def new_attr (attr_name)

class_eval {attr_accessor :"#{attr_name}"}
end
end

class_eval : Evaluates a string or a block in the context of the receiver, here the class.

Explanation:

1) We defined a new method to the CLASS called new_attr, which is intended to add new attributes at runtime.

2) This method needs the name of the new attribute to be passed.

3) In the definition, it calls class_eval, which means “DO SOMETHING AT THE CLASS LEVEL”

4) What to do is specified in the block after the class_eval, which says : attr_accessor “#{attr_name}”, which will become attr_accessor :name when we call new_attr(‘name’).

5) We know that attr_accessor is rails syntactic sugar to create the a specific instance variable, and its set/get (read/write) methods. So here, when we call new_attr(‘name’), its creating a new instance variable (@name) and 2 new methods ‘name” and “name=”.

Try this;

class Person; end;

This creates a bare class Person.

p = Person.new

Try, p.name, it throws an error cursing “Undefined method name”.

Its fairly expected coz, Person has no attribute called name.

Now, say Person.new_attr(‘name’)

now try, p.name, it will not curse you.

To check, try Person.singleton_methods.include?(“name”)

This says TRUE.

Hope this article comes handy.

Permalink 1 Comment

passing optional parameters to partials

May 13, 2009 at 4:38 pm (Partials) (, , , )

Today i got an instance where i want to use a parameter in the partial, which may or may not be passed.This partial is called from two places, one which passes while other does not pass a specific parameter. The partial just displays the parameter value.

<%= passed_parameter %>

If an instance variable is used, the <%= … %> will result in an empty string if the instance variable (@abcdef) id not defined. But if its a local variable like the one passed to the partials using LOCALS, it will throw an error.

In these cases, the best thing to do is, add this at the first line of the PARTIAL.

<% passed_parameter ||= DEFAULT_VALUE %>

This has to be done for whatever optional parameters might be used.

Permalink Leave a Comment