Module: Cosmo::Web::Helpers::Application
- Includes:
- Renderer
- Included in:
- Context
- Defined in:
- lib/cosmo/web/helpers/application.rb
Constant Summary
Constants included
from Renderer
Renderer::ASSETS_ROOT, Renderer::VIEWS_ROOT
Instance Method Summary
collapse
Methods included from Renderer
#no_content, #not_found, #ok, #redirect_to, #serve, #url_for
Instance Method Details
#current_page?(path) ⇒ Boolean
64
65
66
67
68
|
# File 'lib/cosmo/web/helpers/application.rb', line 64
def current_page?(path)
request_path = @request.path_info
request_path = "/" if request_path.empty?
request_path == url_for(path)
end
|
#elapsed(value) ⇒ Object
32
33
34
35
36
37
38
39
40
41
42
|
# File 'lib/cosmo/web/helpers/application.rb', line 32
def elapsed(value)
elapsed = Time.now.to_i - value.to_i
if elapsed < 60
"#{elapsed}s"
elsif elapsed < 3600
"#{elapsed / 60}m #{elapsed % 60}s"
else
"#{elapsed / 3600}h #{(elapsed % 3600) / 60}m"
end
end
|
11
12
13
14
15
16
17
18
|
# File 'lib/cosmo/web/helpers/application.rb', line 11
def format_bytes(bytes)
b = bytes.to_i
return "0 B" if b.zero?
sizes = %w[B KB MB GB TB]
i = [(Math.log(b) / Math.log(1024)).floor, sizes.size - 1].min
"#{(b / (1024.0**i)).round(2)} #{sizes[i]}"
end
|
20
21
22
|
# File 'lib/cosmo/web/helpers/application.rb', line 20
def format_numbers(num)
num.to_s.reverse.gsub(/(\d{3})(?=\d)/, "\\1,").reverse
end
|
24
25
26
27
28
29
30
|
# File 'lib/cosmo/web/helpers/application.rb', line 24
def format_timestamp(value)
return "N/A" unless value
Time.at(value.to_f).strftime("%Y-%m-%d %H:%M:%S")
rescue StandardError
value.to_s
end
|
#h(value) ⇒ Object
56
57
58
|
# File 'lib/cosmo/web/helpers/application.rb', line 56
def h(value)
Rack::Utils.escape_html(value.to_s)
end
|
#referrer?(path) ⇒ Boolean
70
71
72
|
# File 'lib/cosmo/web/helpers/application.rb', line 70
def referrer?(path)
URI(@request.referrer).path == path
end
|
#time_until(value) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/cosmo/web/helpers/application.rb', line 44
def time_until(value)
return "N/A" unless value
diff = value.to_f - Time.now.to_f
return "Ready" if diff <= 0
return "#{diff.to_i}s" if diff < 60
return "#{(diff / 60).to_i}m" if diff < 3_600
return "#{(diff / 3_600).to_i}h" if diff < 86_400
"#{(diff / 86_400).to_i}d"
end
|
#u(value) ⇒ Object
60
61
62
|
# File 'lib/cosmo/web/helpers/application.rb', line 60
def u(value)
Rack::Utils.escape(value.to_s)
end
|