Class: Wavy::Utils::FileSys

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.load(name, partial_check = true) ⇒ Object

Check and load file

Parameters:

  • name (String)

    File path

  • partial_check (Boolean) (defaults to: true)

    Also check for “_” on file



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/wavy/utils/filesys.rb', line 11

def self.load(name, partial_check = true)
  if File.exists?(name)
    open(name).read
  else
    if partial_check
      load_partial(name)
    else
      raise 'File not found.'
    end
  end
end

.load_partial(name) ⇒ Object

Check if a file with “_” exists

Parameters:

  • name (String)

    File path



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/wavy/utils/filesys.rb', line 26

def self.load_partial(name)
  a = name.split("/")
  new_name = "_" + a[-1]
  a = a.first a.size - 1

  if a.length > 1
    new_name = a.join("/") + "/" + new_name
  end

  load(new_name, false)
end

Instance Method Details

#guess_indent(file) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/wavy/utils/filesys.rb', line 38

def guess_indent(file)
  i = 0
  file.each_line.with_index {|line, i|
    break if i > 3
    match = /^\s+/.match(line)
    next unless match
    return {
      :tab? => line.start_with?("\t"),
      :indent => match[0].size
    }
  }
  return { # returns default settings
    :tab? => true,
    :indent => 4
  }
end