Module: Helpers

Includes:
Rack::Utils, Sinatra::ContentFor2, Sinatra::Partials
Defined in:
lib/runit-man/helpers.rb

Constant Summary collapse

KILOBYTE =
1024
MEGABYTE =
1024 * KILOBYTE
GIGABYTE =
1024 * MEGABYTE

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#even_or_odd_stateObject

Returns the value of attribute even_or_odd_state.



13
14
15
# File 'lib/runit-man/helpers.rb', line 13

def even_or_odd_state
  @even_or_odd_state
end

Instance Method Details

#all_files_to_viewObject



35
36
37
38
39
# File 'lib/runit-man/helpers.rb', line 35

def all_files_to_view
  (files_to_view + service_infos.map do |service|
    service.files_to_view
  end.flatten).uniq.sort
end

#even_or_oddObject



73
74
75
76
# File 'lib/runit-man/helpers.rb', line 73

def even_or_odd
  self.even_or_odd_state = !even_or_odd_state
  even_or_odd_state
end

#files_to_viewObject



27
28
29
30
31
32
33
# File 'lib/runit-man/helpers.rb', line 27

def files_to_view
  RunitMan.files_to_view.map do |f|
    File.symlink?(f) ? File.expand_path(File.readlink(f), File.dirname(f)) : f
  end.select do |f|
    File.readable?(f)
  end.uniq.sort
end

#host_nameObject



15
16
17
# File 'lib/runit-man/helpers.rb', line 15

def host_name
  Utils.host_name
end

#human_bytes(bytes) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/runit-man/helpers.rb', line 82

def human_bytes(bytes)
  sign = (bytes >= 0) ? '' : '-'
  suffix = 'b'
  bytes = bytes.abs.to_f
  if bytes > GIGABYTE
    bytes /= GIGABYTE
    suffix = 'Gb'
  elsif bytes > MEGABYTE
    bytes /= MEGABYTE
    suffix = 'Mb'
  elsif bytes > KILOBYTE
    bytes /= KILOBYTE
    suffix = 'Kb'
  end
  bytes = ((bytes * 100 + 0.5).to_i.to_f / 100)
  "#{sign}#{bytes}#{t("runit.services.log.#{suffix}")}"
end


69
70
71
# File 'lib/runit-man/helpers.rb', line 69

def log_downloads_link(name)
  "<a target=\"_blank\" href=\"/#{name}/log-downloads\/\">#{h(t('runit.services.log.downloads'))}&hellip;</a>"
end


58
59
60
61
62
63
64
65
66
67
# File 'lib/runit-man/helpers.rb', line 58

def log_link(name, options = {})
  count = (options[:count] || 100).to_i
  title = options[:title].to_s || count
  blank = options[:blank] || false
  hint  = options[:hint].to_s  || ''
  raw   = options[:raw] || false
  hint  = " title=\"#{h(hint)}\"" unless hint.empty?
  blank = blank ? ' target="_blank"' : ''
  "<a#{hint}#{blank} href=\"/#{name}/log#{ (count != 100) ? "/#{count}" : '' }#{ raw ? '.txt' : '' }#footer\">#{h(title)}</a>"
end

#service_action(name, action, label, enabled = true) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/runit-man/helpers.rb', line 41

def service_action(name, action, label, enabled = true)
  partial :service_action, :locals => {
    :name    => name,
    :action  => action,
    :label   => label,
    :enabled => enabled
  }
end

#service_infosObject



23
24
25
# File 'lib/runit-man/helpers.rb', line 23

def service_infos
  ServiceInfo.all
end

#service_signal(name, signal, label) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/runit-man/helpers.rb', line 50

def service_signal(name, signal, label)
  partial :service_signal, :locals => {
    :name   => name,
    :signal => signal,
    :label  => label
  }
end

#stat_subst(s) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/runit-man/helpers.rb', line 100

def stat_subst(s)
  s.split(/\s/).map do |s|
    if s =~ /(\w+)/
      word = $1
      if t("runit.services.table.subst.#{word}") !~ /translation missing/
        s.sub(word, t("runit.services.table.subst.#{word}"))
      else
        s
      end
    else
      s
    end
  end.join(' ')
end

#t(*args) ⇒ Object



19
20
21
# File 'lib/runit-man/helpers.rb', line 19

def t(*args)
  Utils.t(*args)
end