Here my latest and greatest idea for improving DRYML in a more generic/
flexible fashion. Please provide some feedback ;)
Original DRYML output
<% concat(do_({:with => (User.new)}, { :default => proc { |
_do__default_content| new_context { %><% this.exempt_from_edit_checks
= true %>
<% concat(signup_form.to_s) %>
<% } }, })) %>
Assuming result generated by DRYML could instead be something like the
following erb:
do_tag(
{:a => 7}
,{
:context => {:b => 5},
:exec => do_tag(
{
:context => {:with => (User.new)},
:exec => do_
})
,{
:context => {:b => 5},
:exec => do_stuff
}
)
---
Then the following logic to render the page!
Here the idea is to pass down the parent context to each element
recursively
and for the parent element to retrieve the context built by its
children and act accordingly on the content to be generated if it
chooses
Children can also use parent context and own context to decide what to
"render" or behave (fx how to build context to return)
def do_tag(context, *args)
result_output = ""
args.each_with_index do |arg_item, index|
# add child context to context before call
child_context = arg_item[:context]
context.addContext(:before, child_context)
# call child element with full context
child_exec = arg_item[:exec]
arg_result = child_exec.call(context)
sub_result_output, return_context = handle_res(arg_res)
# add child output result to parent output result?
result_output << sub_result_output
# add child context to parent context
context.addContext(:after, return_context)
end
return result_output, context
end
# customize this function for different ways to handle different
tag results (and contexts)
# always return an output and a context (to be added to parent
context)
def handle_res(arg_res)
res = ""
context = arg_res[:context]
sep = context[:separator]
if sep
res = handle_sep(arg_res)
context = ...
elsif
...
end
return res, context
end
I'm sure there is a better pattern for this somewhere out there.
Perhaps the Command pattern? With a Command handler?
handle_sep(arg_res)
res = ""
# ignore empty output
if !arg_res.blank?
# make pretty output by concatenating newline (always newline
before next comma!)
str_arg = arg_item + "\n"
# first item should never have a comma before it!
if index == 0
res << str_arg
# comma before any other item :)
else
res << (sep + str_arg)
end
end
res
end
Perhaps only the root should be responsible for rendering the final
output based on the final context
and outputs generated by it children? This logic would apply be
recursively up the tree and so the root node can decide to return a
string in which case all the outputs of children would be rendered for
the page. If not, the whole output is cancelled... might output some
error instead if it so chooses!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Hobo
Users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/hobousers?hl=en
-~----------~----~----~----~------~----~------~--~---