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, editor = "vim") ⇒ EasyEditor

Returns a new instance of EasyEditor.



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

def initialize(path,editor="vim")
	@editor =  editor
	@target_files = []
	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)


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

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

#match_pathsObject



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

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

#openObject



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

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

#parse_path(path) ⇒ Object



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

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

#traverse_dir(dir_path) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/easyeditor.rb', line 39

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