how about doing something like this logic
scan your content looking for "{"
store everything until your next "}" into a string variable # now
string = "test foo:foo bar bar:bar foo"
split your string by spaces into an array # now array = ["test",
"foo:foo", "bar", "bar:bar", "foo"]
loop through your array
new_array = []
index = 0
array.each do |a|
if a does not contain ":"
index = index + 1
partial = a
args = {}
new_array[index] = [partial, args]
else
args = new_array[index][2] # should be a hash
name, value = a.split(/:/)
args[name.to_sym] = value # add new args to hash
new_array[index][2] = args # store hash back into the array
end
end
# now new_array = [["foo", {foo => "foo"}], ["bar", {bar => "bar"}],
["foo", {}]]
loop through this and render partials
new_array.each do |a|
partial = a[1]
arguments = a[2]
output << render(:partial => "drops/#{partial}", :locals =>
arguments)
end
i am sure there are errors and bugs but i think the basic logic should
work. it will probably turn out to be fairly clean and readable if
refactored into a few methods.
On Oct 15, 6:01 am, David Trasbo <[EMAIL PROTECTED]>
wrote:
> David Trasbo wrote:
> > ==========
> > def dropify(content)
> > s = StringScanner.new(content)
> > output = ""
> > previous_end = 0
> > while s.scan_until(/\{/)
> > output << content[previous_end, s.pointer - previous_end - 1]
> > partial = s.scan(/\w+/)
> > s.skip /\s+/
> > arguments = {}
> > while argument = s.scan(/\w+:\w+/)
> > name, value = argument.split(/:/)
> > arguments[name.to_sym] = value
> > s.skip /\s+/
> > end
> > s.skip_until /\}/
> > previous_end = s.pointer
> > output << render(:partial => "drops/#{partial}", :locals =>
> > arguments)
> > end
> > output << content[s.pointer, content.length - s.pointer]
> > end
> > ==========
>
> Okay, I actually have a concern about this method. Right now it only
> accepts this kind of syntax:
>
> {test foo:foo_bar}
>
> Since spaces are not covered by \w+ this is not accepted:
>
> {test foo:foo bar}
>
> So I tried changing \w+ to .+ but of course that is never going to work.
> E.g. if I try to pass multiple arguments to the partial like this:
>
> {test foo:foo bar bar:bar foo}
>
> then only one argument will only be passed (foo) and it will have this
> value: "foo bar bar" because the .+ includes that.
>
> But what about putting quotation marks around it?
>
> ".+"
>
> Still the same problem. Now foo will just have this value: "“foo bar”
> bar". So, here is my question: How can I make this method accept
> multiple arguments without being limited to that the values have to
> match "\w+"?
>
> --
> Posted viahttp://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" 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/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---