Module: GRI::FormatHelper

Included in:
Clist, DSList, List, Page
Defined in:
lib/gri/format_helper.rb

Constant Summary collapse

HTML_ESCAPE =
{'&' => '&amp;', '>' => '&gt;', '<' => '&lt;',
'"' => '&quot;', "'" => '&#39;'}
HTML_ESCAPE_ONCE_REGEXP =
/["><']|&(?!([a-zA-Z]+|(#\d+));)/

Instance Method Summary collapse

Instance Method Details

#check_box(name = '', value = nil, checked = nil) ⇒ Object



76
77
78
79
# File 'lib/gri/format_helper.rb', line 76

def check_box name='', value=nil, checked=nil
  mk_tag 'input', 'type'=>'checkbox',
    'name'=>name, 'value'=>value, 'checked'=>checked
end

#escape_once(s) ⇒ Object



19
20
21
# File 'lib/gri/format_helper.rb', line 19

def escape_once s
  s.to_s.gsub(HTML_ESCAPE_ONCE_REGEXP) {HTML_ESCAPE[$&]}
end

#h(obj) ⇒ Object



10
11
12
13
14
15
16
17
# File 'lib/gri/format_helper.rb', line 10

def h obj
  case obj
  when String
    Rack::Utils.escape_html obj
  else
    Rack::Utils.escape_html obj.inspect
  end
end

#hidden(name, value) ⇒ Object



67
68
69
# File 'lib/gri/format_helper.rb', line 67

def hidden name, value
  mk_tag 'input', [['name', name], ['value', value], ['type', 'hidden']]
end

#mk_query(h) ⇒ Object



37
38
39
40
# File 'lib/gri/format_helper.rb', line 37

def mk_query h
  return '' unless Hash === h and !h.empty?
  '?' + h.map {|k, v| v && "#{k}=#{u v}"}.compact.join('&')
end

#mk_tag(tag, attrs, body = nil) ⇒ Object



54
55
56
57
58
# File 'lib/gri/format_helper.rb', line 54

def mk_tag tag, attrs, body=nil
  "<#{tag}" + attrs.map {|k, v|
    v ? ((v == true) ? ' ' + k : " #{k}=\"#{h v}\"") : nil
  }.join('') + (body ? ">#{body}</#{tag}>" : "/>")
end


81
82
83
84
85
86
87
88
# File 'lib/gri/format_helper.rb', line 81

def popup_menu name, cl, *ary
  body = ary.map {|value, s, selected_p|
    attrs = [['value', value]]
    attrs.push ['selected', true] if selected_p
    mk_tag 'option', attrs, s
  }.join ''
  mk_tag 'select', [['name', name], ['class', cl]], body
end

#radio_button(name = '', value = nil, checked = nil) ⇒ Object



71
72
73
74
# File 'lib/gri/format_helper.rb', line 71

def radio_button name='', value=nil, checked=nil
  mk_tag 'input', 'type'=>'radio',
    'name'=>name, 'value'=>value, 'checked'=>checked
end

#render(template, b = nil) ⇒ Object



90
91
92
# File 'lib/gri/format_helper.rb', line 90

def render template, b=nil
  ERB.new(template, nil, '-').result(b || binding)
end

#td(arg, options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/gri/format_helper.rb', line 42

def td arg, options={}
  tag = options[:head] ? 'th' : 'td'
  args = (Array === arg) ? arg : [arg]
  res = []
  for arg in args
    s = [:class, :colspan].map {|s|
      options[s] ? "#{s}=#{options[s]}" : nil}.compact.join ' '
    res.push "<#{tag}#{s.empty? ? '' : ' '+s}>#{arg}</#{tag}>"
  end
  res.join ''
end

#text_field(name = '', value = nil, size = 40, maxlength = nil, cl = 'form-control') ⇒ Object



60
61
62
63
64
65
# File 'lib/gri/format_helper.rb', line 60

def text_field name='', value=nil, size=40, maxlength=nil, cl='form-control'
  attrs = {'type'=>'text', 'name'=>name, 'value'=>value, 'size'=>size.to_s,
    'class'=>cl}
  attrs['maxlength'] = maxlength.to_s if maxlength
  mk_tag 'input', attrs
end

#to_scalestr(v, base = 1000) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/gri/format_helper.rb', line 94

def to_scalestr v, base=1000
  if v == nil or base == nil or base == 0
    return(v.to_i == v ? v.to_i : v)
  end
  v = v.to_f
  if v >= base ** 4
    "%gT" % (v / (base ** 4))
  elsif v >= base ** 3
    "%gG" % (v / (base ** 3))
  elsif v >= base ** 2
    "%gM" % (v / (base ** 2))
  elsif v >= base
    "%gK" % (v / base)
  else
    v.to_s
  end
end

#u(str) ⇒ Object



23
24
25
# File 'lib/gri/format_helper.rb', line 23

def u str
  Rack::Utils.escape str
end

#url_to(arg, q = {}) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/gri/format_helper.rb', line 27

def url_to arg, q={}
  script_name = ENV['SCRIPT_NAME'] || ''
  if String === arg
    (arg[0] == ??) ? script_name + arg :
      (script_name + '/' + arg + mk_query(q))
  else
    ''
  end
end