Jeff Barczewski
2006-Jul-07 22:45 UTC
[Masterview-devel] Proposed DSL for creating directives
Deb,
Here is a first draft of a DSL for creating directives which should clean up
the API considerably, let me know what you think
class MyDirective < DirectiveBase
event :etag do |e|
e.arg :object # get arg[0] from parse(attr_value) and assign it to
@object
e.arg :method, :quote_simple_string => true # get arg[1] from
parse(attr_value) and if is not already quoted and is simple string then
quote it, store in @method
e.arg :options, :optional_default = {} # get arg[2] from
parse(attr_value), it is optional, but if we need to add it to outgoing arg
list then we can use {} for default if nil to @options
e.arg :html_options, :merge => [Common, :rows, :cols] # get arg[3] from
parse(attr_value), merge in common html attributes and rows and cols to
@html_options
# do more business logic here, use e.content, e.yield() and other
DirectiveBase methods
e.erb ''text_area'', args(:object, :method, :options,
:html_options) #
renders a <%= text_area ARGS_HERE %> it builds the args, joining with
comma,
eliminating if nil unless an arg after it is not nil in which case it will
use the optional_default.
end
Other methods
e.erb_code ''a = a + 1'' # generates <% a = a + 1 %>
e.render ''hello world'' # generates hello world
Also other methods on e.arg for manipulating the data
e.arg :foo {|o| o+''bar'' } # manipulate arg[x] by concatenating
''bar'' to it
and then finally store it in @foo, you could obviously do other things with
the data
e.arg :foo { e.content } # override the arg with element content and store
in @foo, link_tag uses something like this to allow users to enter link text
to be consistent with rails helpers, but then overrides it with whatever was
in the element text.
The basic idea is to wrap up much of the repetive work around building
directives into something simple yet allow custom things to be done as
necessary.
Much of the tediousness is simply parsing out args from the attr_value,
quoting if necessary, merging in html options and other simple logic.
Finally we build up output to write outputting <%= %> and joining together
args, accounting for the fact that some args might be missing and if later
args are suddenly non-empty because of a merge, then we need to output good
defaults for the nil previous ones.
So I was kind of patterning this dsl off of what we need to do and how rake
and active record migrations dsl''s work.
It seems clearer to me what happens doing this and much of the monkey
business is abstacted away using a few key words.
Let me know what you think.
Jeff
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
http://rubyforge.org/pipermail/masterview-devel/attachments/20060707/378e4a5a/attachment.html
Jeff - good stuff, I''ll have to time to get back to working on directives tomorrow and will give you some feedback after I''ve had a chance to play around with the proposal a bit. [been pretty swamped all week, but finally getting through the initial ramp-up learning curve for the company I''ve just started working for, so should be able to have more time again over the next week to get back to working on some of my rails stuff and MV] ~ Deb -------------- next part -------------- An HTML attachment was scrubbed... URL: http://rubyforge.org/pipermail/masterview-devel/attachments/20060708/5108a1a2/attachment-0001.html