Class: FC

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/fc.rb,
lib/fc/version.rb

Defined Under Namespace

Modules: Error Classes: File

Constant Summary collapse

VERSION =
'0.1.1'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir_path, files: []) ⇒ FC

Returns a new instance of FC.



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

def initialize(dir_path, files: [])
  raise Error::DirectoryDoesNotExist unless ::File.directory?(dir_path)

  @path = dir_path
  @files = files
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



29
30
31
# File 'lib/fc.rb', line 29

def files
  @files
end

#pathObject (readonly)

Returns the value of attribute path.



29
30
31
# File 'lib/fc.rb', line 29

def path
  @path
end

Instance Method Details

#filter(recursive: false) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/fc.rb', line 42

def filter(recursive: false)
  clear
  return self unless block_given?

  index(recursive: recursive) { |file| @files << file if yield(file) }
  self
end

#index(recursive: false, &block) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/fc.rb', line 50

def index(recursive: false, &block)
  indexed_files = []
  Dir.each_child(path) do |filename|
    filepath = ::File.join(path, filename)
    if ::File.file?(filepath)
      tmp = FC::File.new(filepath)
      indexed_files << tmp
      block.call(tmp)
    elsif recursive
      tmp_array = FC
        .new(filepath)
        .index(recursive: recursive, &block)
      indexed_files.concat(tmp_array)
    end
  end
  indexed_files
end