dimanche 29 mars 2015

Create automatically methods

Lets say I have this House class:



class House

def self.building_steps
[
[Worker, :buy_material],
[Worker, :blend_material],
[Truck, :remove_ground],
......
#more Tasks defined by [CLASS,METHOD]
]
end

def self.buy_material
check_process(Worker, __method__)
end

def self.blend_material
check_process(Worker, __method__)
end

def self.remove_ground
check_process(Truck, __method__)
end
............
#More Methods that have same Method names like the building steps

end


As you can see in my code I have a lot of repetitions.


My question is how i can automatically define the class methods from the building_steps list.


So that I dont have to add the methods manually!


I search something like:



House.building_steps.each do |step|
define_house_method_with_name( step[1] )
in this method do
check_process(step[0], step[1])
end
end


Is something like this possible? Thanks!


Aucun commentaire:

Enregistrer un commentaire