I'm working on filling a large (155 fields) PDF using pdftk
, and I find myself creating a massive Hash with the PDF's field names, and the values to be filled in.
For my specific implementation, I'm using Ruby, and have a PdfJob
object which collects all of the data from a model object into a hash, and passes that hash into a wrapper around pdftk
stored in $PDFTK
.
class PdfJob
def perform(model)
$PDFTK.fill_form('path/to/fillable.pdf', Tempfile.new, Fields.call(model))
end
private
class Fields
def self.call(model)
{
OWNER_NAME: "#{model.first_name} #{model.last_name}",
TOTAL_PRICE: calculate_total_price(model),
FOO: 'bar',
# 152 more lines of key/value pairs.
}
end
def self.calculate_total_price(model)
# Most of these methods are multi-line.
model.item_relations.sum(&:price)
end
# about 50 more class methods to simplify assignment in #call()
end
end
This is a heavily-simplified example, but I think it illustrates where I've found myself.
What I'm looking for is a design pattern or some other way to break up this hash into multiple classes, methods, or some other unit so I don't end up with a Fields
class with nearly 250 lines of near-untestable key/value pairs and helper methods.
Aucun commentaire:
Enregistrer un commentaire