lib/masterview/directives/attr.rb in masterview-0.3.1 vs lib/masterview/directives/attr.rb in masterview-0.3.2
- old
+ new
@@ -2,25 +2,30 @@
module Directives
# takes the attr_value and parses it as a hash :foo => 'bar', :baz => 'cat'
# it sets/overrides the tag's attribute values for each value.
# To use erb output simply wrap the content in #{ foo } for example :foo => #{h product.name}
- #
+ #
class Attr < MasterView::DirectiveBase
# substitution to make it easy to parse erb in attr
SubstForErb = '#'+InlineErbStart+'= \1 '+InlineErbEnd+'#'
metadata :priority => 'MediumLow',
:category => 'general',
:description => 'Replaces attribute value(s) on an element with the value of an expression'
event :stag do
- attr_value.gsub!( /#\{([^}]*)\}/, SubstForErb ) #taking #{h product.name} and changing to #{{{= h product.name}}}# for easy parsing
- arr = attr_value.scan( /:(\w+)\s*=>\s*(['"#])([^,\2]*)\2,?/ )
- arr.each do |scn|
- n = scn[0]
- v = scn[2]
- element_attrs[n] = v
+ av = attr_value.gsub( /#\{([^}]*)\}/, SubstForErb ) #taking #{h product.name} and changing to #{{{= h product.name}}}# for easy parsing
+ av.strip!
+ av = av[1..-2] if (av[0,1]=='{' && av[-1,1]=='}') # explicit hash, make it implicit by removing surrounding braces so we can split it
+ hash_arr = MasterView::AttrStringParser.parse(av, true) # split_implicit_hash
+ hash_arr.each do |hash_kv|
+ arr = hash_kv.scan( /:(\w+)\s*=>\s*(['"#])([^\2]*)\2/ )
+ arr.each do |scn|
+ n = scn[0]
+ v = scn[2]
+ element_attrs[n] = v
+ end
end
end
end
end