module DohWeb::HtmlTags

Constants

SELECT_MONTH_ITEMS

Public Instance Methods

full_tag(tag, body, options = {}) click to toggle source
# File lib/dohweb/html_tags.rb, line 21
def full_tag(tag, body, options = {})
  "#{start_tag(tag, true, options)}#{body}</#{tag}>"
end
input_tag(name, type, options = {}) click to toggle source
# File lib/dohweb/html_tags.rb, line 25
def input_tag(name, type, options = {})
  options[:name] = name
  options[:type] = type
  start_tag(:input, false, options)
end
select_day(name, options = {}) click to toggle source
# File lib/dohweb/html_tags.rb, line 65
def select_day(name, options = {})
  options[:value] ||= 1
  select_tag(name, 1..31, options)
end
select_hour(name, options = {}) click to toggle source
# File lib/dohweb/html_tags.rb, line 80
def select_hour(name, options = {})
  options[:value] ||= 0
  select_tag(name, 0..23, options)
end
select_minute(name, options = {}) click to toggle source
# File lib/dohweb/html_tags.rb, line 85
def select_minute(name, options = {})
  options[:value] ||= 0
  select_tag(name, 0..59, options)
end
select_month(name, options = {}) click to toggle source
# File lib/dohweb/html_tags.rb, line 70
def select_month(name, options = {})
  options[:value] ||= 1
  select_tag(name, SELECT_MONTH_ITEMS, options)
end
select_tag(name, items, options = {}) click to toggle source
# File lib/dohweb/html_tags.rb, line 31
def select_tag(name, items, options = {})
  selected_value = options.delete(:value)

  # in case it's a numeric
  if selected_value
    unless selected_value.is_a?(Array)
      selected_value = selected_value.to_s
    end
    selected_value = nil if selected_value.empty?
  end

  options[:name] = name
  lines = [start_tag('select', true, options)]
  items.each do |elem|
    subopts = {}
    if elem.is_a?(Array)
      text, value = elem
      subopts[:value] = value
    else
      text = value = elem
    end
    value = value.to_s.strip
    if !selected_value
    elsif selected_value.is_a?(Array)
      subopts[:selected] = 'selected' if selected_value.include?(value)
    else
      subopts[:selected] = 'selected' if value == selected_value
    end
    lines.push("#{start_tag(:option, true, subopts)}#{text}</option>")
  end
  lines.push('</select>')
  lines.join("\n")
end
select_year(name, start_year, end_year, options = {}) click to toggle source
# File lib/dohweb/html_tags.rb, line 75
def select_year(name, start_year, end_year, options = {})
  options[:value] ||= start_year
  select_tag(name, start_year..end_year, options)
end
start_tag(tag, open, options = {}) click to toggle source
# File lib/dohweb/html_tags.rb, line 6
def start_tag(tag, open, options = {})
  if options.key?(:format_decimal)
    num_digits = options.delete(:format_decimal)
    valstr = options[:value].to_s.strip
    options[:value] = valstr.to_d.to_dig(num_digits) unless valstr.empty?
  end
  if options.empty?
    options_str = ''
  else
    options_str = ' ' + options.collect {|key, value| %(#{key}="#{value}") }.join(' ')
  end
  open_str = open ? '' : ' /'
  "<#{tag}#{options_str}#{open_str}>"
end