Class: Siba::SibaFile

Inherits:
Object
  • Object
show all
Defined in:
lib/siba/siba_file.rb

Constant Summary collapse

DIR_REGEXP =
/^dir_/
FILE_REGEXP =
/^file_/
FILE_UTILS_REGEXP =
/^file_utils_/

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/siba/siba_file.rb', line 28

def method_missing(meth, *args, &block)
  file_class, class_meth = SibaFile.get_file_class meth
  if file_class
    file_class.send(class_meth, *args, &block)
  else
    super
  end
end

Class Method Details

.get_file_class(meth) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/siba/siba_file.rb', line 17

def self.get_file_class(meth)
  case meth
  when FILE_UTILS_REGEXP
    return FileUtils, meth.to_s.gsub(FILE_UTILS_REGEXP, "")
  when DIR_REGEXP
    return Dir, meth.to_s.gsub(DIR_REGEXP, "")
  when FILE_REGEXP
    return File, meth.to_s.gsub(FILE_REGEXP, "")
  end
end

Instance Method Details

#file_expand_path(file_name, dir_string = nil) ⇒ Object



45
46
47
48
# File 'lib/siba/siba_file.rb', line 45

def file_expand_path(file_name, dir_string=nil)
  file_utils_cd Siba.current_dir unless Siba.current_dir.nil?
  File.expand_path file_name, dir_string
end

#respond_to?(meth) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
40
41
42
43
# File 'lib/siba/siba_file.rb', line 37

def respond_to?(meth)
  if SibaFile.get_file_class meth
    true
  else
    super
  end
end

#run_shell(command, fail_message = nil) ⇒ Object

Runs shell command and raises error if it fails returns output (both stdout and stderr)



52
53
54
55
56
57
58
59
60
# File 'lib/siba/siba_file.rb', line 52

def run_shell(command, fail_message=nil)
  strout, status = Open3.capture2e command
  raise strout if status.to_i != 0
  return strout
rescue Exception => ex
  fail_message ||= "Failed to run the command: #{command}"
  raise Siba::Error, "#{fail_message}
#{ex.message}"
end

#run_this(name = "noname") ⇒ Object



9
10
11
# File 'lib/siba/siba_file.rb', line 9

def run_this(name="noname")
  yield
end

#shell_ok?(command) ⇒ Boolean

Runs the shell command. Works the same way as Kernel.system method but without showing the output. Returns true if it was successfull.

Returns:

  • (Boolean)


65
66
67
68
69
70
71
# File 'lib/siba/siba_file.rb', line 65

def shell_ok?(command)
  # Using Open3 instead of `` or system("cmd") in order to hide stderr output
  sout, status = Open3.capture2e command
  return status.to_i == 0
rescue
  return false
end