Class: Trax::Core::FS::Directory
- Defined in:
- lib/trax/core/fs.rb
Instance Attribute Summary collapse
-
#directories(*args) ⇒ Object
Returns the value of attribute directories.
-
#files(recurse = false) ⇒ Object
Returns the value of attribute files.
-
#path ⇒ Object
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
- #[](arg) ⇒ Object
- #__getobj__ ⇒ Object
- #all ⇒ Object
- #each {|entries| ... } ⇒ Object
- #entries ⇒ Object
- #folders(recurse = false) ⇒ Object
- #glob(*args) ⇒ Object
- #images ⇒ Object
-
#initialize(file_path) ⇒ Directory
constructor
A new instance of Directory.
- #recursive_files ⇒ Object
- #recursive_folders ⇒ Object
- #reload ⇒ Object
Constructor Details
#initialize(file_path) ⇒ Directory
Returns a new instance of Directory.
66 67 68 69 |
# File 'lib/trax/core/fs.rb', line 66 def initialize(file_path) @path = ::Pathname.new(file_path) @dir = ::Dir[@path] end |
Instance Attribute Details
#directories(*args) ⇒ Object
Returns the value of attribute directories.
55 56 57 |
# File 'lib/trax/core/fs.rb', line 55 def directories @directories end |
#files(recurse = false) ⇒ Object
Returns the value of attribute files.
55 56 57 |
# File 'lib/trax/core/fs.rb', line 55 def files @files end |
#path ⇒ Object
Returns the value of attribute path.
55 56 57 |
# File 'lib/trax/core/fs.rb', line 55 def path @path end |
Class Method Details
.[](path) ⇒ Object
57 58 59 60 |
# File 'lib/trax/core/fs.rb', line 57 def self.[](path) file_path = path.is_a?(Pathname) ? path : ::Pathname.new(path) new(file_path) end |
Instance Method Details
#[](arg) ⇒ Object
62 63 64 |
# File 'lib/trax/core/fs.rb', line 62 def [](arg) self.class.new(@path.join(arg).to_s) end |
#__getobj__ ⇒ Object
132 133 134 |
# File 'lib/trax/core/fs.rb', line 132 def __getobj__ @dir end |
#all ⇒ Object
71 72 73 74 75 76 |
# File 'lib/trax/core/fs.rb', line 71 def all @all ||= ::Dir[path.join("*")].map do |folder_or_file| listing = ::Pathname.new(folder_or_file) listing.directory? ? self.class.new(listing) : ::Trax::Core::FS::Listing.new(listing) end end |
#each {|entries| ... } ⇒ Object
82 83 84 |
# File 'lib/trax/core/fs.rb', line 82 def each yield(entries) end |
#entries ⇒ Object
86 87 88 89 90 91 |
# File 'lib/trax/core/fs.rb', line 86 def entries @entries ||= ::Dir.entries(path).map do |file_path| directory_file_path = ::Pathname.new(file_path) unless file_path.is_a?(Pathname) directory_file_path.directory? ? ::Trax::Core::FS::Directory.new(directory_file_path) : ::Listing.new(directory_file_path) end end |
#folders(recurse = false) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/trax/core/fs.rb', line 110 def folders(recurse = false) @folders ||= begin if recurse current_directory_directories = all.select{|file_path| file_path.is_a?(::Trax::Core::FS::Directory) } current_directory_directories += folders(true).map do |dir| dir.directories(true).any? ? dir.directories(true) : [] end if directories(true) && directories(true).any? current_directory_directories else all.select{ |file_path| file_path.is_a?(::Trax::Core::FS::Directory) } end end end |
#glob(*args) ⇒ Object
126 127 128 129 130 |
# File 'lib/trax/core/fs.rb', line 126 def glob(*args) args.map do |arg| self.dup[arg].flatten.compact.uniq.map{|_path| ::Trax::Core::FS::Listing.new(_path) } end.flatten.compact.uniq end |
#images ⇒ Object
150 151 152 |
# File 'lib/trax/core/fs.rb', line 150 def images files.select{|file| file.image? } end |
#recursive_files ⇒ Object
136 137 138 |
# File 'lib/trax/core/fs.rb', line 136 def recursive_files files(true) end |
#recursive_folders ⇒ Object
140 141 142 |
# File 'lib/trax/core/fs.rb', line 140 def recursive_folders folders(true) end |
#reload ⇒ Object
144 145 146 147 148 |
# File 'lib/trax/core/fs.rb', line 144 def reload remove_instance_variable(:@folders) if defined?(:@folders) remove_instance_variable(:@files) if defined?(:@files) self end |