Module: Bureaucrat::Utils

Defined Under Namespace

Modules: SafeData Classes: SafeString, StringAccessHash

Constant Summary

ESCAPES =
{
  '&' => '&',
  '<' => '&lt;',
  '>' => '&gt;',
  '"' => '&quot;',
  "'" => '&#39;'
}

Instance Method Summary collapse

Instance Method Details

#blank_value?(value) ⇒ Boolean



52
53
54
# File 'lib/bureaucrat/utils.rb', line 52

def blank_value?(value)
  !value || value == ''
end

#conditional_escape(html) ⇒ Object



71
72
73
# File 'lib/bureaucrat/utils.rb', line 71

def conditional_escape(html)
  html.is_a?(SafeData) ? html : escape(html)
end

#escape(html) ⇒ Object



67
68
69
# File 'lib/bureaucrat/utils.rb', line 67

def escape(html)
  mark_safe(html.gsub(/[&<>"']/) {|match| ESCAPES[match]})
end

#flatatt(attrs) ⇒ Object



75
76
77
# File 'lib/bureaucrat/utils.rb', line 75

def flatatt(attrs)
  attrs.map {|k, v| " #{k}=\"#{conditional_escape(v)}\""}.join('')
end

#format_string(string, values) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/bureaucrat/utils.rb', line 79

def format_string(string, values)
  output = string.dup
  values.each_pair do |variable, value|
    output.gsub!(/%\(#{variable}\)s/, value.to_s)
  end
  output
end

#make_bool(value) ⇒ Object



96
97
98
# File 'lib/bureaucrat/utils.rb', line 96

def make_bool(value)
  !(value.respond_to?(:empty?) ? value.empty? : [0, nil, false].include?(value))
end

#make_float(value) ⇒ Object



91
92
93
94
# File 'lib/bureaucrat/utils.rb', line 91

def make_float(value)
  value += '0' if value.is_a?(String) && value != '.' && value[-1,1] == '.'
  Float(value)
end

#mark_safe(s) ⇒ Object



56
57
58
# File 'lib/bureaucrat/utils.rb', line 56

def mark_safe(s)
  s.is_a?(SafeData) ? s : SafeString.new(s.to_s)
end

#pretty_name(name) ⇒ Object



87
88
89
# File 'lib/bureaucrat/utils.rb', line 87

def pretty_name(name)
  name.to_s.capitalize.gsub(/_/, ' ')
end