module Sinatra::Bells::Helpers::View
Constants
- DEFAULT_FORMAT
- DEFAULT_SEPARATOR
- HTML_ELEMENTS
- LABEL_FORMAT_RE
- LIST_ELEMENTS
Public Class Methods
define_html_tag_method(*names)
click to toggle source
# File lib/sinatra/bells/helpers/view.rb 65 def define_html_tag_method(*names) 66 names.each { |name| 67 class_eval <<-EOT, __FILE__, __LINE__ + 1 68 def #{name}_(*args, &block) 69 tag_(#{name.inspect}, *args, &block) 70 end 71 72 def #{name}__(*args) 73 tag__(#{name.inspect}, *args) 74 end 75 EOT 76 } 77 end
define_list_tag_method(*names)
click to toggle source
# File lib/sinatra/bells/helpers/view.rb 79 def define_list_tag_method(*names) 80 names.each { |name| 81 class_eval <<-EOT, __FILE__, __LINE__ + 1 82 def #{name}_(*args, &block) 83 if args.first.respond_to?(:each) 84 raise ArgumentError, 'no block given' unless block 85 86 list, block_, block = args.shift, block, lambda { |tag| 87 list.each { |*item| tag << li_(*block_[*item]) } 88 } 89 end 90 91 tag_(#{name.inspect}, *args, &block) 92 end 93 94 def #{name}__(*args) 95 tag__(#{name.inspect}, *args) 96 end 97 EOT 98 } 99 end
Protected Instance Methods
active?(path)
click to toggle source
# File lib/sinatra/bells/helpers/view.rb 150 def active?(path) 151 'active' if request.path_info =~ /\A#{Regexp.escape(path)}(?:\/|\?|\z)/ 152 end
disabled?(condition)
click to toggle source
# File lib/sinatra/bells/helpers/view.rb 154 def disabled?(condition) 155 'disabled' unless condition 156 end
format_label(label, hash = nil, &block)
click to toggle source
# File lib/sinatra/bells/helpers/view.rb 158 def format_label(label, hash = nil, &block) 159 hash ||= block 160 161 label.gsub(LABEL_FORMAT_RE) { 162 field, separator, format = 163 $1, $2 || DEFAULT_SEPARATOR, $3 || DEFAULT_FORMAT 164 165 value = Array(hash[field]).map(&:to_s).delete_if(&:empty?) 166 format % [value.join(separator), field] unless value.empty? 167 } 168 end
link_to(text, *args)
click to toggle source
# File lib/sinatra/bells/helpers/view.rb 105 def link_to(text, *args) 106 options = args.last.is_a?(Hash) ? args.pop : {} 107 108 href = uri(args.join('/')) 109 110 if params = options.delete(:params) 111 href << '?' << Array(params).flat_map { |key, value| 112 key = CGI.escape(key.to_s) 113 114 value.nil? ? key : Array(value) 115 .map { |val| "#{key}=#{CGI.escape(val.to_s)}" } 116 }.join('&') 117 end 118 119 if anchor = options.delete(:anchor) 120 href << '#' << CGI.escape(anchor.to_s) 121 end 122 123 a_(text, options.merge(href: href)) 124 end
link_to_if(condition, text, *args) { |text, *args| ... }
click to toggle source
# File lib/sinatra/bells/helpers/view.rb 126 def link_to_if(condition, text, *args) 127 condition ? link_to(text, *args) : block_given? ? yield(text, *args) : text 128 end
partial(name, locals = {}, layout = false)
click to toggle source
# File lib/sinatra/bells/helpers/view.rb 170 def partial(name, locals = {}, layout = false) 171 send(settings.default_renderer, :"_#{name}", locals: locals, layout: layout) 172 end
tag_(name, *args) { |args| ... }
click to toggle source
# File lib/sinatra/bells/helpers/view.rb 130 def tag_(name, *args) 131 args = [tag__(name, *args)] 132 yield args if block_given? 133 args.push('</', name, '>').join 134 end
tag__(name, *args)
click to toggle source
# File lib/sinatra/bells/helpers/view.rb 136 def tag__(name, *args) 137 args.unshift('<', name, '>') 138 139 if args.last.is_a?(Hash) 140 attr = args.pop.map { |k, v| %Q{#{h(k)}="#{h(v)}"} } 141 args.insert(2, ' ', attr.join(' ')) unless attr.empty? 142 end 143 144 args.join 145 end