Class: Formalizer::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/formalizer/utils.rb

Constant Summary collapse

SEARCH_PATHS =

SEARCH_PATHS constant Usually formalizer.yml is in root config directory Gem’s testing directory is test/dummy/config

%w(config test/dummy/config test/dummy)

Class Method Summary collapse

Class Method Details

.final_path(path) ⇒ Object

tries getting the full local path to a relative/absolute path (image src attribute, stylesheet rel attribute)

  • path - the original relative/absolute path



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/formalizer/utils.rb', line 40

def self.final_path path
  if path.start_with?'http'
    # external path, nothing to do, return path as is
    return path
  elsif path[0..6].include?'assets'
    # path is probably an asset, searching in assets
    final_path = self.find_file(Pathname.new(path).basename.to_s, false, Rails.application.config.assets.paths)
  else
    # path is a relative path
    begin
      final_path = self.find_file(path, false)
    rescue Formalizer::FileNotFound
      # path not found
      return ''
    end
  end
  return "file:///#{final_path}"
end

.find_file(file_path, read = true, search_paths = SEARCH_PATHS) ⇒ Object

Raises:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/formalizer/utils.rb', line 12

def self.find_file file_path, read = true, search_paths = SEARCH_PATHS

  if file_path[0] == '/'
    # probably absolute path
    # raise exception if file not found
    raise FileNotFound, 'file not exists' unless File.exists?file_path
    return read ? File.read(file_path) : fild_path
  else
    # probably relative path, searching in SEARCH_PATHS
    search_paths.each do |path|
      absolute_file_path = "#{File.expand_path(path)}/#{file_path}"
      begin
        return (read ? File.read(absolute_file_path) : absolute_file_path) if File.exists?(absolute_file_path)
      rescue Errno::EISDIR
        raise FileNotFound, 'path is a directory, not a file'
      end
    end
  end

  # not found anywhere
  raise FileNotFound, 'file not exists' unless File.exists?file_path
end