Class: Ld::File

Inherits:
Object
  • Object
show all
Defined in:
lib/ld.rb,
lib/ld/file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ File

Returns a new instance of File.



5
6
7
8
9
# File 'lib/ld/file.rb', line 5

def initialize path
  @path = path
  @name = File.basename @path
  @type = File.directory?(@path) ? 1 : 0
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object



78
79
80
# File 'lib/ld/file.rb', line 78

def method_missing name
  find name
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/ld/file.rb', line 3

def name
  @name
end

#pathObject

Returns the value of attribute path.



3
4
5
# File 'lib/ld/file.rb', line 3

def path
  @path
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/ld/file.rb', line 3

def type
  @type
end

Instance Method Details

#brothersObject



11
12
13
# File 'lib/ld/file.rb', line 11

def brothers
  father.children
end

#childrenObject



15
16
17
18
19
20
21
22
23
24
# File 'lib/ld/file.rb', line 15

def children
  arr = []
  Dir.foreach(@path)do |p|
    if !['.','..','.DS_Store'].include?(p)
      arr << Ld::File.new("#{@path}/#{p}")
    end
  end
  arr.sort!{|a,b| b.type-a.type}
  arr
end

#fatherObject



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

def father
  arr = @path.split('/')
  arr.pop
  Ld::File.new(arr.join('/'))
end

#find(name) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/ld/file.rb', line 32

def find name
  name = name.to_s
  children.each do |f|
    if f.name == name
      return f
    end
  end
  return nil
end

#iter(arr) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/ld/file.rb', line 68

def iter arr
  children.each do |f|
    if f.type == 1
      f.iter arr
    end
    arr << f
  end
  self
end

#iter_search(regexp, arr) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ld/file.rb', line 56

def iter_search regexp, arr
  children.each do |f|
    if f.type == 1
      f.iter_search regexp, arr
    end
    if f.name.match(regexp)
      arr << f
    end
  end
  self
end

#readObject



42
43
44
# File 'lib/ld/file.rb', line 42

def read
  File.open(@path).read
end

#readlinesObject



46
47
48
# File 'lib/ld/file.rb', line 46

def readlines
  File.open(@path).readlines
end

#where(regexp) ⇒ Object



50
51
52
53
54
# File 'lib/ld/file.rb', line 50

def where regexp
  arr = []
  iter_search regexp, arr
  arr
end