mirror of
https://github.com/haml/haml.git
synced 2022-11-09 12:33:31 -05:00
Optimize Hamlit::AB.build with Ruby
This commit is contained in:
parent
ff85bf94be
commit
fac00c05cb
1 changed files with 23 additions and 16 deletions
|
@ -14,24 +14,23 @@ module Hamlit::AttributeBuilder
|
||||||
# class methods which takes all options as arguments for performance.
|
# class methods which takes all options as arguments for performance.
|
||||||
class << self
|
class << self
|
||||||
def build(escape_attrs, quote, format, object_ref, *hashes)
|
def build(escape_attrs, quote, format, object_ref, *hashes)
|
||||||
buf = []
|
|
||||||
hashes = hashes.map { |h| stringify_keys(h) }
|
|
||||||
hashes << Hamlit::ObjectRef.parse(object_ref) if object_ref
|
hashes << Hamlit::ObjectRef.parse(object_ref) if object_ref
|
||||||
|
buf = []
|
||||||
|
hash = merge_all_attrs(hashes)
|
||||||
|
|
||||||
keys = hashes.map(&:keys).flatten.sort.uniq
|
keys = hash.keys.sort!
|
||||||
keys.each do |key|
|
keys.each do |key|
|
||||||
values = hashes.select { |h| h.has_key?(key) }.map { |h| h[key] }
|
|
||||||
case key
|
case key
|
||||||
when 'id'.freeze
|
when 'id'.freeze
|
||||||
buf << " id=#{quote}#{build_id(escape_attrs, *values)}#{quote}"
|
buf << " id=#{quote}#{build_id(escape_attrs, *hash[key])}#{quote}"
|
||||||
when 'class'.freeze
|
when 'class'.freeze
|
||||||
buf << " class=#{quote}#{build_class(escape_attrs, *values)}#{quote}"
|
buf << " class=#{quote}#{build_class(escape_attrs, *hash[key])}#{quote}"
|
||||||
when 'data'.freeze
|
when 'data'.freeze
|
||||||
buf << build_data(escape_attrs, quote, *values)
|
buf << build_data(escape_attrs, quote, *hash[key])
|
||||||
when *BOOLEAN_ATTRIBUTES, /\Adata-/
|
when *BOOLEAN_ATTRIBUTES, /\Adata-/
|
||||||
build_boolean!(escape_attrs, quote, format, buf, key, values)
|
build_boolean!(escape_attrs, quote, format, buf, key, hash[key])
|
||||||
else
|
else
|
||||||
buf << " #{key}=#{quote}#{escape_html(escape_attrs, values.last.to_s)}#{quote}"
|
buf << " #{key}=#{quote}#{escape_html(escape_attrs, hash[key].to_s)}#{quote}"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
buf.join
|
buf.join
|
||||||
|
@ -39,16 +38,24 @@ module Hamlit::AttributeBuilder
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def stringify_keys(hash)
|
def merge_all_attrs(hashes)
|
||||||
result = {}
|
merged = {}
|
||||||
hash.each do |key, value|
|
hashes.each do |hash|
|
||||||
result[key.to_s] = value
|
hash.each do |key, value|
|
||||||
|
key = key.to_s
|
||||||
|
case key
|
||||||
|
when 'id'.freeze, 'class'.freeze, 'data'.freeze
|
||||||
|
merged[key] ||= []
|
||||||
|
merged[key] << value
|
||||||
|
else
|
||||||
|
merged[key] = value
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
result
|
merged
|
||||||
end
|
end
|
||||||
|
|
||||||
def build_boolean!(escape_attrs, quote, format, buf, key, values)
|
def build_boolean!(escape_attrs, quote, format, buf, key, value)
|
||||||
value = values.last
|
|
||||||
case value
|
case value
|
||||||
when true
|
when true
|
||||||
case format
|
case format
|
||||||
|
|
Loading…
Add table
Reference in a new issue