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, line 64 def define_html_tag_method(*names) names.each { |name| class_eval " def #{name}_(*args, &block) tag_(#{name.inspect}, *args, &block) end ", __FILE__, __LINE__ + 1 } end
define_list_tag_method(*names)
click to toggle source
# File lib/sinatra/bells/helpers/view.rb, line 74 def define_list_tag_method(*names) names.each { |name| class_eval " def #{name}_(*args, &block) if args.first.respond_to?(:each) raise ArgumentError, 'no block given' unless block list, block_, block = args.shift, block, lambda { |tag| list.each { |*item| tag << li_(*block_[*item]) } } end tag_(#{name.inspect}, *args, &block) end ", __FILE__, __LINE__ + 1 } end
Protected Instance Methods
active?(path)
click to toggle source
# File lib/sinatra/bells/helpers/view.rb, line 137 def active?(path) 'active' if request.path_info =~ /\A#{Regexp.escape(path)}(?:\/|\?|\z)/ end
disabled?(condition)
click to toggle source
# File lib/sinatra/bells/helpers/view.rb, line 141 def disabled?(condition) 'disabled' unless condition end
format_label(label, hash = nil, &block)
click to toggle source
# File lib/sinatra/bells/helpers/view.rb, line 145 def format_label(label, hash = nil, &block) hash ||= block label.gsub(LABEL_FORMAT_RE) { field, separator, format = $1, $2 || DEFAULT_SEPARATOR, $3 || DEFAULT_FORMAT value = Array(hash[field]).map(&:to_s).delete_if(&:empty?) format % [value.join(separator), field] unless value.empty? } end
link_to(text, *args)
click to toggle source
# File lib/sinatra/bells/helpers/view.rb, line 96 def link_to(text, *args) options = args.last.is_a?(Hash) ? args.pop : {} href = uri(args.join('/')) if params = options.delete(:params) href << '?' << Array(params).flat_map { |key, value| key = CGI.escape(key.to_s) value.nil? ? key : Array(value) .map { |val| "#{key}=#{CGI.escape(val.to_s)}" } }.join('&') end if anchor = options.delete(:anchor) href << '#' << CGI.escape(anchor.to_s) end a_(text, options.merge(href: href)) end
link_to_if(condition, text, *args) { |text, *args| ... }
click to toggle source
# File lib/sinatra/bells/helpers/view.rb, line 117 def link_to_if(condition, text, *args) condition ? link_to(text, *args) : block_given? ? yield(text, *args) : text end
partial(name, locals = {}, layout = false)
click to toggle source
# File lib/sinatra/bells/helpers/view.rb, line 157 def partial(name, locals = {}, layout = false) send(settings.default_renderer, :"_#{name}", locals: locals, layout: layout) end
tag_(name, *args) { |args| ... }
click to toggle source
# File lib/sinatra/bells/helpers/view.rb, line 121 def tag_(name, *args) args.unshift('<', name, '>') if args.last.is_a?(Hash) attr = args.pop.map { |k, v| %Q{#{h(k)}="#{h(v)}"} } args.insert(2, ' ', attr.join(' ')) unless attr.empty? end yield args if block_given? args.push('</', name, '>').join end