Module: Helpers

Included in:
BasicField
Defined in:
lib/formalize/helpers.rb

Instance Method Summary collapse

Instance Method Details

#tag(name, opts = {}) ⇒ Object

TODO: This leaves a extra space when there are no attributes



4
5
6
7
# File 'lib/formalize/helpers.rb', line 4

def tag(name, opts = {})
  attributes = tag_attributes(opts)
  "<#{ name } #{ attributes }>"
end

#tag_attributes(opts) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/formalize/helpers.rb', line 15

def tag_attributes(opts)
  opts.sort.collect do |key, val|
    next if key == :label  # REVIEW: Move to a NonStandardAttributes constant?
    case val
    when TrueClass
      "#{ key }"
    when FalseClass
      ""
    when Array
      # REVIEW Comma or no comma? Not sure what attributes this could be used with
      "#{ key }='#{ val.join(',') }'"
    when Regexp
      pattern = val.inspect[1..-2]
      "#{ key }='#{ pattern }'"
    else
      "#{ key }='#{ val }'"
    end
  end.join(' ').squeeze(' ')
end

#tag_content(name, content, opts = {}) ⇒ Object

TODO: This leaves a extra space when there are no attributes



10
11
12
13
# File 'lib/formalize/helpers.rb', line 10

def tag_content(name, content, opts = {})
  attributes = tag_attributes(opts)
  "<#{ name } #{ attributes }>#{ content }</#{ name }>"
end