Pragmas in Squeak do not need to be declared, but I
would not
use them like suggested.
>> Post>>author
>> <description: #SingleOption label: 'Author'>
>> <options: #findAuthors>
>> <relatedTo: #SBAuthor>
>> <displayField: #asString>
>> <required: true>
>>
>> ^author
In my opinion pragmas should not be used to write code, but
to tag and configure code. I would suggest something like that:
Post>>author
<description: #authorDescription>
^ author
What tells Magritte to look at #authorDescription to get a
description for #author:
Post>>authorDescription
^ MASingleOption new
options: self findAuthors;
relatedTo: SBAuthor;
beRequired;
yourself
Note that there is no need anymore to define an accessor, as
this will be figured out automatically by Magritte.
Do we really want that? what are the benefits?
over class
side single
description?
The problem with class side descriptions is that they cannot
adapt themselves to the state of the object. Something that
currently enforces to override #description and to manually
patch the descriptions.
Cheers,
Lukas
Contrast Magritte with Glorp, both are very powerful, and very pluggable.
Magritte provides meta data about a class, Glorp needs that same metadata
for its descriptions. Magritte is a runtime meta data system, Pragmas are
compile time meta data. However, both Glorp and Magritte require explicit
meta data in the form of code, stored away from the accessor the meta data
belongs to.
Pragmas however, allow one to keep the metadata for a selector, in the
selector itself, a very pleasing option because while I don't mind the
description building bouncing around following description Pragmas, I'd
rather the developer didn't have to, and much of the meta data contained
within Magritte descriptions, really is just compile time meta data.
Obviously Pragmas are less flexible than class side descriptions, I'm not at
all suggesting there's anything wrong with Magritte, it's great, but it
could be simpler to configure, just like Glorp vs. Rails.
Most of the time, when doing database bound web applications, I think one
really could use Pragmas like Rails uses macros to give hints to the
description builder to automate the construction of "MOST" descriptions,
possibly all. It'd be really elegant if one made a field required by simply
adding the <required> Pragma to the accessor itself, and then having the
runtime generate both the Magritte and Glorp descriptions off the class meta
data allowing Rails like behavior and no code required by the developer.
For the more complex cases, you could always have the builder follow
<description: #authorDescription> to the manually configured class side
descriptor, but seriously, most cases will be simple mappings of Strings,
Numbers, Dates, and Memo fields directly to a matching field in the
database, and those, the most common cases, can be totally automated and
work off Pragmas alone. Rails has proven the attraction of zero
configuration to the masses, and if Ruby can do it, Smalltalk can do it
better.
I'm already building an ActiveRecord implementation that builds Glorp
descriptions from Magritte's meta data (working well so far), but while
doing so, it just occurred to me that most of what I need for Glorp could
come directly from Pragmas, and it'd be a shame to build the meta data
twice, so I just thought why not build Magritte descriptors from Pragmas as
well. It's just an idea, but one I think I'm going to explore a little to
see where it leads. Of course, I'll share anything worthy of sharing if
anything pans out, if not, oh well, I'm sure I'll have fun in the process.
Ramon Leon
http://onsmalltalk.com