Module: Erector::Attributes

Included in:
AbstractWidget, Promise
Defined in:
lib/erector/attributes.rb

Instance Method Summary collapse

Instance Method Details

#format_attributes(attributes) ⇒ Object


3
4
5
6
7
8
9
# File 'lib/erector/attributes.rb', line 3

def format_attributes(attributes)
  if !attributes || attributes.empty?
    ""
  else
    format_sorted(sort_attributes(attributes))
  end
end

#format_sorted(sorted) ⇒ Object


11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/erector/attributes.rb', line 11

def format_sorted(sorted)
  results = ['']
  sorted.each do |key, value|
    if value
      if value.is_a?(Array)
        value = value.flatten
        next if value.empty?
        value = value.join(' ')
      end
      results << "#{key}=\"#{h(value)}\""
    end
  end
  results.join(' ')
end

#sort_attributes(attributes) ⇒ Object


26
27
28
29
30
31
32
# File 'lib/erector/attributes.rb', line 26

def sort_attributes(attributes)
  stringized = []
  attributes.each do |key, value|
    stringized << [key.to_s, value]
  end
  stringized.sort
end