Sorry for the lame title: I’m not sure how to sum this one up…
I have a set of classes that are formulas (or formulae if you like), and other set of classes (call them outer classes) that will utilize them.
The formula classes have many attributes (say about 20) and a calculate function. The outer class is a the persistence class and therefore has all the attributes of the formulae class and several more of its own.
In my system, users can configure which formulae class to use, and indeed may chose to calculate using more than one formula for a comparison report for example.
I’m trying to work out how I can transfer attribute values between the inner/formula class and the outer/persistence class without line after line of inner.x = outer.x code.
It seems to me that I can’t use:
class Outer
include Formula1
end
…because I want the actual Formula class to be configurable.
One idea that springs to mind is I could have an array of attributes to pass down from my outer class, and loop over them and send, something like this:
# not tested
['x', 'y', 'z'].each{|a|@formula.send("#{a.to_sym}=", self.send("#{a.to_sym}") }
Any other ruby magic or patterns I should be considering?
Thanks,
Sounds like you’re look for a Strategy pattern to plug in the formula and then maybe a Facade if you don’t want to call the formula’s attribute via the formula instance.
Easy way to make a Strategy is to make an instance of Formula one of Outer’s attributes:
to deal with lots of accessors perhaps you could try method missing to either call, or define and call the methods for you as they’re called?