Module: Saint::Utils

Includes:
Presto::Utils
Included in:
ClassApi, ClassApi::Subset, Column, FileServer, Filter, FilterInstance, FmExtender, Menu, Pager
Defined in:
lib/saint/utils.rb

Constant Summary

BOOLEAN_OPTIONS =
{true => 'Yes', false => 'No'}
BOOLEAN_FILTERS =
{1 => 'Yes', 0 => 'No'}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.column_format(arg, row) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/saint/utils.rb', line 92

def column_format arg, row

  chunks = []

  evaluate = lambda do |chunk|
    obj = row
    chunk.split('.').map { |m| m.to_sym }.each do |meth|
      unless obj.respond_to?(meth)
        obj = nil
        break
      end
      obj = obj.send(meth)
    end
    obj.to_s
  end

  if arg.is_a?(String)
    if arg =~ /#/
      chunk, valid = '', false
      arg.split(/(?<=[^\\])?(#[\w|\d|\.]+)/m).each do |s|
        if s =~ /^#/
          next unless (val = evaluate.call(s.sub('#', ''))).size > 0
          chunk << val
          valid = true
        else
          chunk << s
        end
      end
      chunks << chunk if valid
    else
      chunks << evaluate.call(arg.strip)
    end
  else
    chunks << evaluate.call(arg.to_s)
  end
  chunks.join
end

.escape_html(str) ⇒ Object



132
133
134
# File 'lib/saint/utils.rb', line 132

def escape_html str
  CGI::escapeHTML str
end

.normalize_string(str) ⇒ Object

remove any non-printable chars



32
33
34
35
36
37
38
# File 'lib/saint/utils.rb', line 32

def normalize_string str
  str.to_s.encode(
      invalid: :replace,
      undef: :replace,
      universal_newline: true
  )
end

.number_to_human_size(number, opts = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/saint/utils.rb', line 42

def number_to_human_size number, opts = {}
  k = 2.0**10
  m = 2.0**20
  g = 2.0**30
  t = 2.0**40
  p = 2.0**50
  e = 2.0**60
  z = 2.0**70
  y = 2.0**80
  max_digits = opts[:max_digits] || 3
  bytes = number || 0
  value, suffix, precision = case bytes
                               when 0...k
                                 [bytes, 'B', 0]
                               else
                                 value, suffix = case bytes
                                                   when k...m then
                                                     [bytes / k, 'KB']
                                                   when m...g then
                                                     [bytes / m, 'MB']
                                                   when g...t then
                                                     [bytes / g, 'GB']
                                                   when t...p then
                                                     [bytes / t, 'TB']
                                                   when p...e then
                                                     [bytes / p, 'PB']
                                                   when e...z then
                                                     [bytes / e, 'EB']
                                                   when z...y then
                                                     [bytes / z, 'ZB']
                                                   else
                                                     [bytes / y, 'YB']
                                                 end
                                 used_digits = case value
                                                 when 0...10 then
                                                   1
                                                 when 10...100 then
                                                   2
                                                 when 100...1000 then
                                                   3
                                               end
                                 leftover_digits = max_digits - used_digits.to_i
                                 [value, suffix, leftover_digits > 0 ? leftover_digits : 0]
                             end
  return "%.#{precision}f #{suffix}" % value unless opts[:split]
  ["%.#{precision}f" % value, suffix]
end

.saint_view(scope = self) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/saint/utils.rb', line 9

def saint_view scope = self
  api = Presto::ViewApi.new
  api.engine Saint.view.engine
  api.ext Saint.view.ext
  api.layout :layout
  api.root Saint.view.root
  api.scope scope
  api
end

.unescape_html(str) ⇒ Object



138
139
140
# File 'lib/saint/utils.rb', line 138

def unescape_html str
  CGI::unescapeHTML str
end

Instance Method Details

#format_date__time(type, val, with_timezone = false) ⇒ Object



21
22
23
24
25
26
27
28
29
# File 'lib/saint/utils.rb', line 21

def format_date__time type, val, with_timezone = false
  return val unless val.is_a?(Date) || val.is_a?(DateTime) || val.is_a?(Time)
  return unless format = {
      'date' => '%F',
      'date_time' => '%F %T.%L' << (with_timezone ? ' %Z' : ''),
      'time' => '%T.%L',
  }[type.to_s]
  val.strftime format
end