Module: Madness::ServerHelper

Overview

All the methods that we may need inside of any server route. The module can also be included manually anywhere else.

Instance Method Summary collapse

Instance Method Details

#configObject



5
6
7
# File 'lib/madness/server_helper.rb', line 5

def config
  @config ||= Settings.instance
end

#disallowed_static?(path) ⇒ Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/madness/server_helper.rb', line 42

def disallowed_static?(path)
  path.end_with?('.md') || path.empty? || File.basename(path).start_with?('.')
end

#docrootObject



9
10
11
# File 'lib/madness/server_helper.rb', line 9

def docroot
  @docroot ||= File.expand_path(config.path, Dir.pwd)
end

#find_static_file(path) ⇒ Object

Search for static file, first in the users docroot, then in the template directory.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/madness/server_helper.rb', line 27

def find_static_file(path)
  return nil if disallowed_static?(path)

  candidates = [
    "#{config.path}/#{path}",
    "#{theme.public_path}/#{path}",
  ]

  candidates.each do |candidate|
    return candidate if File.file? candidate
  end

  nil
end

#log(obj) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/madness/server_helper.rb', line 17

def log(obj)
  # :nocov:
  open('madness.log', 'a') do |f|
    f.puts obj.inspect
  end
  # :nocov:
end

#themeObject



13
14
15
# File 'lib/madness/server_helper.rb', line 13

def theme
  @theme ||= Theme.new config.theme
end