Class: Easyeditor::EasyEditor

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options = {}) ⇒ EasyEditor

Returns a new instance of EasyEditor.



11
12
13
14
15
16
17
18
19
# File 'lib/easyeditor.rb', line 11

def initialize(path,options={})
  @editor =  options[:editor]
  @target_files = []
  @dir = options[:directory]
  @sudo_flag = options[:sudo]
  parse_path(path)
  match_paths
  return self
end

Instance Attribute Details

#target_filesObject

Returns the value of attribute target_files.



10
11
12
# File 'lib/easyeditor.rb', line 10

def target_files
  @target_files
end

Instance Method Details

#is_match?(path) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/easyeditor.rb', line 41

def is_match?(path)
  !File.directory?(path) && /#{@path_str}/.match(path) && /#{@filename_str}.*/.match(File.basename(path))
end

#match_pathsObject



36
37
38
39
40
# File 'lib/easyeditor.rb', line 36

def match_paths
  traverse_dir(@dir) do |file|
    @target_files << file if is_match?(file)
  end
end

#openObject



20
21
22
23
24
25
26
27
# File 'lib/easyeditor.rb', line 20

def open
  if @target_files.length > 1
    ap @target_files  
    puts "Which file to open:[0-default]"
    @index = $stdin.gets.to_i 
  end
  exec "#{sudo}#{@editor} #{@target_files[@index || 0]}"
end

#parse_path(path) ⇒ Object



31
32
33
34
35
# File 'lib/easyeditor.rb', line 31

def parse_path(path)
  path_a = path.split('/')
  @filename_str = path_a.delete_at(-1)
  @path_str = path_a.join(".*/.*")
end

#sudoObject



28
29
30
# File 'lib/easyeditor.rb', line 28

def sudo
  "sudo " if @sudo_flag
end

#traverse_dir(dir_path) ⇒ Object

遍历文件夹



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/easyeditor.rb', line 45

def traverse_dir(dir_path)  
  if File.directory? dir_path  
    Dir.foreach(dir_path) do |file|  
      if file!="." and file!=".."  
        traverse_dir(dir_path+"/"+file){|x| yield x}  
      end  
    end  
  else  
    yield  dir_path  
  end  
end