module MasterView 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 end end end end end