Prev | Current Page 41 | Next

Brad Ediger

"Advanced Rails"


This technique can manifest in the simplest ways, such as writing a shell script to
automate some tedious part of programming. For example, you may want to populate
your test fixtures with a sample project for each user:
brad_project:
id: 1
owner_id: 1
billing_status_id: 12
john_project:
id: 2
owner_id: 2
billing_status_id: 4
...
Metaprogramming Techniques | 25
If this were a language without scriptable test fixtures, you might be writing these by
hand. This gets messy when the data starts growing, and is next to impossible when
the fixtures have strange dependencies on the source data. Na??ve generative programming
would have you writing a script to generate this fixture from the source.
Although not ideal, this is a great improvement over writing the complete fixtures by
hand. But this is a maintenance headache: you have to incorporate the script into your
build process, and ensure that the fixture is regenerated when the source data changes.
This is rarely, if ever, needed in Ruby or Rails (thankfully). Almost every aspect of
Rails application configuration is scriptable, due in large part to the use of internal
domain-specific languages (DSLs). In an internal DSL, you have the full power of the
Ruby language at your disposal, not just the particular interface the library author
decided you should have.
Returning to the preceding example, ERb makes our job a lot easier.


Pages:
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53