Class: SlimKeyfy::Slimutils::BaseKeyGenerator

Inherits:
Object
  • Object
show all
Defined in:
lib/slimkeyfy/slimutils/key_generator.rb

Class Method Summary collapse

Class Method Details

.filename(file_path) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/slimkeyfy/slimutils/key_generator.rb', line 17

def self.filename(file_path)
  return "" if file_path.nil?
  if file_path.end_with?(".html.slim") then
    fname = File.basename(file_path, ".html.slim")
  elsif file_path.end_with?(".slim") then
    fname = File.basename(file_path, ".slim")
  elsif file_path.end_with?(".rb") then
    fname = File.basename(file_path, ".rb")
  else return "" end
  fname = fname[1..-1] if fname.start_with?("_")
  fname
end

.generate_key_base_from_path(file_path, file_extension) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/slimkeyfy/slimutils/key_generator.rb', line 2

def self.generate_key_base_from_path(file_path, file_extension)
  key_name = case file_extension
    when "slim" then 
      [subdir_name(file_path), filename(file_path)].join(".") 
    when "rb" then 
      sub = subdir_name(file_path, "controllers")
      fname = filename(file_path)
      if sub != nil and !sub.strip.empty? then
        "#{sub}.#{fname}"
      else
        fname
      end
    else nil end
  key_name
end

.subdir_name(file_path, delim = "views") ⇒ Object



29
30
31
32
33
34
35
# File 'lib/slimkeyfy/slimutils/key_generator.rb', line 29

def self.subdir_name(file_path, delim="views")
  return "" if file_path.nil?
  dirs = file_path.split("/").drop_while{|e| e != delim }
  return "" if dirs.empty?
  strip_dirs_and_file = dirs.pop(dirs.size-1)
  strip_dirs_and_file[0..-2].join(".")
end