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, #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



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

def delete
  return self unless exist?

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

#delete_allObject



144
145
146
147
148
149
# File 'lib/io_streams/paths/file.rb', line 144

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
# File 'lib/io_streams/paths/file.rb', line 91

def each_child(pattern = "*", case_sensitive: false, directories: false, hidden: false)
  flags = 0
  flags |= ::File::FNM_CASEFOLD unless case_sensitive
  flags |= ::File::FNM_DOTMATCH if hidden

  # Dir.each_child("testdir") {|x| puts "Got #{x}" }
  Dir.glob(::File.join(path, pattern), flags) do |full_path|
    next if !directories && ::File.directory?(full_path)

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

#exist?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/io_streams/paths/file.rb', line 129

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

#mkdirObject



124
125
126
127
# File 'lib/io_streams/paths/file.rb', line 124

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

#mkpathObject



118
119
120
121
122
# File 'lib/io_streams/paths/file.rb', line 118

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.



108
109
110
111
112
113
114
115
116
# File 'lib/io_streams/paths/file.rb', line 108

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.



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

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

#sizeObject



133
134
135
# File 'lib/io_streams/paths/file.rb', line 133

def size
  ::File.size(path)
end