Module: GRI::FormatHelper
Constant Summary collapse
- HTML_ESCAPE =
{'&' => '&', '>' => '>', '<' => '<', '"' => '"', "'" => '''}
- HTML_ESCAPE_ONCE_REGEXP =
/["><']|&(?!([a-zA-Z]+|(#\d+));)/
Instance Method Summary collapse
- #check_box(name = '', value = nil, checked = nil) ⇒ Object
- #escape_once(s) ⇒ Object
- #h(obj) ⇒ Object
- #hidden(name, value) ⇒ Object
- #mk_query(h) ⇒ Object
- #mk_tag(tag, attrs, body = nil) ⇒ Object
- #popup_menu(name, cl, *ary) ⇒ Object
- #radio_button(name = '', value = nil, checked = nil) ⇒ Object
- #render(template, b = nil) ⇒ Object
- #td(arg, options = {}) ⇒ Object
- #text_field(name = '', value = nil, size = 40, maxlength = nil, cl = 'form-control') ⇒ Object
- #to_scalestr(v, base = 1000) ⇒ Object
- #u(str) ⇒ Object
- #url_to(arg, q = {}) ⇒ Object
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 |
#popup_menu(name, cl, *ary) ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/gri/format_helper.rb', line 81 def 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 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, ={} tag = [:head] ? 'th' : 'td' args = (Array === arg) ? arg : [arg] res = [] for arg in args s = [:class, :colspan].map {|s| [s] ? "#{s}=#{[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 |