Module: Coo::Helper::FileHunter

Defined in:
lib/coo/helper/file_hunter.rb

Class Method Summary collapse

Class Method Details

.create_file(file_name, content) ⇒ Object

根据文件名和内容创建对应文件

Parameters:

  • file_name (文件名)
  • content (文件内容)


56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/coo/helper/file_hunter.rb', line 56

def self.create_file(file_name, content)
  is_success = false
  if file_name.to_s.empty?
    puts 'file_name is wrong!'
  else
    new_file = File.new(file_name, 'w')
    if new_file
      new_file.syswrite(content)
      new_file.close
      is_success = true
    end
  end
  is_success
end

.file_directory_name(file) ⇒ Object

Returns script dir name.

Parameters:

  • file (文件名, 传入 __FILE__)

Returns:

  • script dir name



31
32
33
# File 'lib/coo/helper/file_hunter.rb', line 31

def self.file_directory_name(file)
  return File.dirname(file)
end

.file_name(file) ⇒ Object

Returns script name.

Parameters:

  • file (文件名, 传入 __FILE__)

Returns:

  • script name



25
26
27
# File 'lib/coo/helper/file_hunter.rb', line 25

def self.file_name(file)
  return File.basename(Pathname.new(file).realpath)
end

.file_path(file) ⇒ Object

Returns script path.

Parameters:

  • file (文件名, 传入 __FILE__)

Returns:

  • script path



19
20
21
# File 'lib/coo/helper/file_hunter.rb', line 19

def self.file_path(file)
  return Pathname.new(file).realpath
end

.get_file_content(file_name) ⇒ Object

获取文件内容

Parameters:

  • file_name (文件名)


37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/coo/helper/file_hunter.rb', line 37

def self.get_file_content(file_name)
  content = ''
  if file_name.to_s.empty?
    puts 'get_file_content file_name is wrong!'
  else
    if File.exist?(file_name) && !File.directory?(file_name)
      target_file = File.new(file_name, 'r')
      if target_file
        content = target_file.read
        target_file.close
      end
    end
  end
  content
end

.work_directory_nameObject

Returns 终端工作 dir name.

Returns:

  • 终端工作 dir name



13
14
15
# File 'lib/coo/helper/file_hunter.rb', line 13

def self.work_directory_name
  return File.basename(Dir.pwd)
end

.work_directory_pathObject

Returns 终端工作 dir path.

Returns:

  • 终端工作 dir path



8
9
10
# File 'lib/coo/helper/file_hunter.rb', line 8

def self.work_directory_path
  return Dir.pwd
end