Class: Chitin::FSObject

Inherits:
Object show all
Includes:
Enumerable
Defined in:
lib/chitin/file.rb

Direct Known Subclasses

Directory, FileObject

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, opts = {}) ⇒ FSObject

The choice to not have File.expand_path here is explicitly done. Relativity is up to the user. Also it got really annoying seeing the same prefix over and over and over again on lists of files.



12
13
14
15
16
# File 'lib/chitin/file.rb', line 12

def initialize(path, opts={})
  @path = path
  @search = proc { path }
  @unknown = opts[:unknown]
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



5
6
7
# File 'lib/chitin/file.rb', line 5

def path
  @path
end

#searchObject

Returns the value of attribute search.



6
7
8
# File 'lib/chitin/file.rb', line 6

def search
  @search
end

Instance Method Details

#directory?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/chitin/file.rb', line 65

def directory?
  false
end

#each(&block) ⇒ Object



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

def each(&block)
  files.values.each &block
end

#filesObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/chitin/file.rb', line 22

def files
  return @files if @files
  files = Dir[search.call]
  @files = files.inject({}) do |h, f|
    f = f[2..-1] if f.start_with? './'
    h[f] = if File.directory? f
             D f
           elsif File.file? f
             F f
           elsif File.symlink? f
             S f
           else
             FSObject.new f, :unknown => true
           end
  
    h
  end
end

#lstatObject



53
54
55
# File 'lib/chitin/file.rb', line 53

def lstat
  File.lstat @path
end

#sizeObject



49
50
51
# File 'lib/chitin/file.rb', line 49

def size
  files.size
end

#symlink?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/chitin/file.rb', line 61

def symlink?
  false
end

#to_aObject



45
46
47
# File 'lib/chitin/file.rb', line 45

def to_a
  files.values
end

#to_sObject



57
58
59
# File 'lib/chitin/file.rb', line 57

def to_s
  path
end

#unknown_type?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/chitin/file.rb', line 18

def unknown_type?
  @unknown
end