Class: Ld::File
- Inherits:
-
Object
show all
- Defined in:
- lib/ld/file.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(path) ⇒ File
Returns a new instance of File.
5
6
7
8
9
10
|
# File 'lib/ld/file.rb', line 5
def initialize path
@path = path
@name = File.basename @path
@base_name = name.split('.')[0]
@type = File.directory?(@path) ? 1 : 0
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name) ⇒ Object
105
106
107
|
# File 'lib/ld/file.rb', line 105
def method_missing name
find name
end
|
Instance Attribute Details
#base_name ⇒ Object
Returns the value of attribute base_name.
3
4
5
|
# File 'lib/ld/file.rb', line 3
def base_name
@base_name
end
|
#name ⇒ Object
Returns the value of attribute name.
3
4
5
|
# File 'lib/ld/file.rb', line 3
def name
@name
end
|
#path ⇒ Object
Returns the value of attribute path.
3
4
5
|
# File 'lib/ld/file.rb', line 3
def path
@path
end
|
#type ⇒ Object
Returns the value of attribute type.
3
4
5
|
# File 'lib/ld/file.rb', line 3
def type
@type
end
|
Class Method Details
.open_dir(path) ⇒ Object
12
13
14
|
# File 'lib/ld/file.rb', line 12
def self.open_dir path
Ld::File.new path
end
|
Instance Method Details
#brothers ⇒ Object
16
17
18
|
# File 'lib/ld/file.rb', line 16
def brothers
father.children
end
|
#children(remove = nil) ⇒ Object
20
21
22
23
24
25
26
27
28
29
30
31
|
# File 'lib/ld/file.rb', line 20
def children(remove = nil)
arr = []
Dir.foreach(@path)do |p|
removes = ['.','..','.DS_Store']
removes << remove if remove
if !removes.include?(p)
arr << Ld::File.new("#{@path}/#{p}")
end
end
arr.sort!{|a,b| b.type-a.type}
arr
end
|
#exist? ⇒ Boolean
101
102
103
|
# File 'lib/ld/file.rb', line 101
def exist?
File.exist? path
end
|
#father ⇒ Object
67
68
69
70
71
|
# File 'lib/ld/file.rb', line 67
def father
arr = @path.split('/')
arr.pop
Ld::File.new(arr.join('/'))
end
|
#find(name) ⇒ Object
73
74
75
76
77
78
79
80
81
|
# File 'lib/ld/file.rb', line 73
def find name
name = name.to_s
children.each do |f|
if f.name == name
return f
end
end
return nil
end
|
#iter_search_dir(arr) ⇒ Object
45
46
47
48
49
50
51
52
53
|
# File 'lib/ld/file.rb', line 45
def iter_search_dir arr
children.each do |f|
if f.type == 1
arr << f
f.iter_search_dir arr
end
end
self
end
|
#iter_search_files(regexp, arr) ⇒ Object
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/ld/file.rb', line 55
def iter_search_files regexp, arr
children.each do |f|
if f.type == 1
f.iter_search_files regexp, arr
end
if f.name.match(regexp)
arr << f
end
end
self
end
|
#lines ⇒ Object
95
96
97
98
99
|
# File 'lib/ld/file.rb', line 95
def lines
arr = []
File.new(path).each_line{|l| arr << l }
arr
end
|
#read ⇒ Object
83
84
85
|
# File 'lib/ld/file.rb', line 83
def read
File.open(@path).read
end
|
#readlines ⇒ Object
87
88
89
|
# File 'lib/ld/file.rb', line 87
def readlines
File.open(@path).readlines
end
|
#search_dirs ⇒ Object
39
40
41
42
43
|
# File 'lib/ld/file.rb', line 39
def search_dirs
arr = []
iter_search_dir arr
arr
end
|
#search_files(regexp) ⇒ Object
33
34
35
36
37
|
# File 'lib/ld/file.rb', line 33
def search_files regexp
arr = []
iter_search_files regexp, arr
arr
end
|
#size ⇒ Object
91
92
93
|
# File 'lib/ld/file.rb', line 91
def size
File.size path
end
|