Class: IOStreams::Paths::File

Inherits:
IOStreams::Path show all
Defined in:
lib/io_streams/paths/file.rb

Instance Attribute Summary collapse

Attributes inherited from IOStreams::Path

#path

Attributes inherited from Stream

#io_stream

Instance Method Summary collapse

Methods inherited from IOStreams::Path

#<=>, #==, #absolute?, #children, #compressed?, #copy_from, #directory, #encrypted?, #inspect, #join, #partial_files_visible?, #relative?, #to_s

Methods inherited from Stream

#basename, #copy_from, #copy_to, #dirname, #each, #extension, #extname, #file_name, #file_name=, #format, #format=, #format_options, #format_options=, #option, #option_or_stream, #pipeline, #read, #reader, #remove_from_pipeline, #setting, #stream, #write, #writer

Constructor Details

#initialize(file_name, create_path: true) ⇒ File

Returns a new instance of File.



8
9
10
11
# File 'lib/io_streams/paths/file.rb', line 8

def initialize(file_name, create_path: true)
  @create_path = create_path
  super(file_name)
end

Instance Attribute Details

#create_pathObject

Returns the value of attribute create_path.



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

def create_path
  @create_path
end

Instance Method Details

#deleteObject



156
157
158
159
160
161
# File 'lib/io_streams/paths/file.rb', line 156

def delete
  return self unless exist?

  ::File.directory?(path) ? Dir.delete(path) : ::File.unlink(path)
  self
end

#delete_allObject



163
164
165
166
167
168
# File 'lib/io_streams/paths/file.rb', line 163

def delete_all
  return self unless exist?

  ::File.directory?(path) ? FileUtils.remove_dir(path) : ::File.unlink(path)
  self
end

#each_child(pattern = "*", case_sensitive: false, directories: false, hidden: false) ⇒ Object

Yields Paths within the current path.

Examples:

# Case Insensitive file name lookup: IOStreams.path(“ruby”).glob(“r*.md”) { |name| puts name }

# Case Sensitive file name lookup: IOStreams.path(“ruby”).each(“R*.md”, case_sensitive: true) { |name| puts name }

# Also return the names of directories found during the search: IOStreams.path(“ruby”).each(“R*.md”, directories: true) { |name| puts name }

# Case Insensitive recursive file name lookup: IOStreams.path(“ruby”).glob(“*/.md”) { |name| puts name }

Parameters:

pattern [String]
  The pattern is not a regexp, it is a string that may contain the following metacharacters:
  `*`      Matches all regular files.
  `c*`     Matches all regular files beginning with `c`.
  `*c`     Matches all regular files ending with `c`.
  `*c*`    Matches all regular files that have `c` in them.

  `**`     Matches recursively into subdirectories.

  `?`      Matches any one character.

  `[set]`  Matches any one character in the supplied `set`.
  `[^set]` Does not matches any one character in the supplied `set`.

  `\`      Escapes the next metacharacter.

  `{a,b}`  Matches on either pattern `a` or pattern `b`.

case_sensitive [true|false]
  Whether the pattern is case-sensitive.

directories [true|false]
  Whether to yield directory names.

hidden [true|false]
  Whether to yield hidden paths.

Examples:

Pattern: File name: match? Reason Options

================ ====== ============================= ===========================

“cat” “cat” true # Match entire string “cat” “category” false # Only match partial string

“cat,ubs” “cats” true # { } is supported

“c?t” “cat” true # “?” match only 1 character “c??t” “cat” false # ditto “c*” “cats” true # “*” match 0 or more characters “c*t” “c/a/b/t” true # ditto “ca” “cat” true # inclusive bracket expression “ca” “cat” false # exclusive bracket expression (“^” or “!”)

“cat” “CAT” false # case sensitive false “cat” “CAT” true # case insensitive

“?” “?” true # escaped wildcard becomes ordinary “a” “a” true # escaped ordinary remains ordinary “[?]” “?” true # can escape inside bracket expression

“*” “.profile” false # wildcard doesn’t match leading “*” “.profile” true # period by default. “.*” “.profile” true true

*/.rb” “main.rb” false “*/.rb” “./main.rb” false “*/.rb” “lib/song.rb” true “**.rb” “main.rb” true “**.rb” “./main.rb” false “**.rb” “lib/song.rb” true “*” “dave/.profile” true



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/io_streams/paths/file.rb', line 91

def each_child(pattern = "*", case_sensitive: false, directories: false, hidden: false)
  unless block_given?
    return to_enum(__method__, pattern,
                   case_sensitive: case_sensitive, directories: directories, hidden: hidden)
  end

  flags = 0
  flags |= ::File::FNM_CASEFOLD unless case_sensitive
  flags |= ::File::FNM_DOTMATCH if hidden

  # Dir.each_child("testdir") {|x| puts "Got #{x}" }
  full_pattern = ::File.join(path, pattern)

  results = Dir.glob(full_pattern, flags)

  # On some platforms or Ruby versions, FNM_CASEFOLD may not work properly
  # with complex patterns. If case-insensitive matching returns no results
  # but we expected some, try a more robust approach.
  if results.empty? && !case_sensitive && pattern.match?(/[A-Z]/)
    # Try converting the pattern to lowercase and re-matching
    lowercase_pattern = pattern.downcase
    lowercase_full_pattern = ::File.join(path, lowercase_pattern)
    results = Dir.glob(lowercase_full_pattern, flags)
  end

  results.each do |full_path|
    next if !directories && ::File.directory?(full_path)

    yield(self.class.new(full_path))
  end
end

#exist?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/io_streams/paths/file.rb', line 148

def exist?
  ::File.exist?(path)
end

#mkdirObject



143
144
145
146
# File 'lib/io_streams/paths/file.rb', line 143

def mkdir
  FileUtils.mkdir_p(path) unless ::File.exist?(path)
  self
end

#mkpathObject



137
138
139
140
141
# File 'lib/io_streams/paths/file.rb', line 137

def mkpath
  dir = ::File.dirname(path)
  FileUtils.mkdir_p(dir) unless ::File.exist?(dir)
  self
end

#move_to(target_path) ⇒ Object

Moves this file to the ‘target_path` by copying it to the new name and then deleting the current file.

Notes:

  • Can copy across buckets.



127
128
129
130
131
132
133
134
135
# File 'lib/io_streams/paths/file.rb', line 127

def move_to(target_path)
  target = IOStreams.new(target_path)
  return super(target) unless target.is_a?(self.class)

  target.mkpath
  # In case the file is being moved across partitions
  FileUtils.move(path, target.to_s)
  target
end

#realpathObject

Returns the real path by stripping ‘.`, `..` and expands any symlinks.



171
172
173
# File 'lib/io_streams/paths/file.rb', line 171

def realpath
  self.class.new(::File.realpath(path))
end

#sizeObject



152
153
154
# File 'lib/io_streams/paths/file.rb', line 152

def size
  ::File.size(path)
end