Class: Pathname

Inherits:
Object show all
Defined in:
lib/eac_ruby_utils/patches/pathname/child_of.rb,
lib/eac_ruby_utils/patches/pathname/if_exist.rb,
lib/eac_ruby_utils/patches/pathname/mkpath_s.rb,
lib/eac_ruby_utils/patches/pathname/parent_n.rb,
lib/eac_ruby_utils/patches/pathname/readlink_r.rb,
lib/eac_ruby_utils/patches/pathname/reqdir_glob.rb,
lib/eac_ruby_utils/patches/pathname/basename_sub.rb,
lib/eac_ruby_utils/patches/pathname/assert_parent.rb,
lib/eac_ruby_utils/patches/pathname/basename_noext.rb

Instance Method Summary collapse

Instance Method Details

#assert_parentself

Invoke mkpath for parent path and return self.

Returns:

  • (self)


8
9
10
11
# File 'lib/eac_ruby_utils/patches/pathname/assert_parent.rb', line 8

def assert_parent
  parent.mkpath
  self
end

#basename_noext(limit = 1) ⇒ Object



7
8
9
# File 'lib/eac_ruby_utils/patches/pathname/basename_noext.rb', line 7

def basename_noext(limit = 1)
  basename(::EacRubyUtils::Fs.extname(basename.to_path, limit))
end

#basename_sub(suffix = '') ⇒ Pathname

Parameters:

  • suffix (String) (defaults to: '')

Returns:



8
9
10
11
12
# File 'lib/eac_ruby_utils/patches/pathname/basename_sub.rb', line 8

def basename_sub(suffix = '')
  new_basename = basename(suffix)
  new_basename = yield(new_basename) if block_given?
  parent.join(new_basename)
end

#child_of?(parent_path) ⇒ Boolean

Indicate if self is child of parent_path.

Returns:

  • (Boolean)


9
10
11
12
13
14
15
# File 'lib/eac_ruby_utils/patches/pathname/child_of.rb', line 9

def child_of?(parent_path)
  self_parts = expand_path.each_filename.to_a
  parent_parts = parent_path.expand_path.each_filename.to_a
  return false if self_parts == parent_parts

  parent_parts.zip(self_parts).all? { |x, y| x == y }
end

#if_exist(default_value = nil) ⇒ Object



5
6
7
8
9
# File 'lib/eac_ruby_utils/patches/pathname/if_exist.rb', line 5

def if_exist(default_value = nil)
  return default_value unless exist?

  block_given? ? yield(self) : self
end

#mkpath_sPathname

Execute .mkpath and return self.

Returns:



8
9
10
11
# File 'lib/eac_ruby_utils/patches/pathname/mkpath_s.rb', line 8

def mkpath_s
  mkpath
  self
end

#parent_n(n) ⇒ Pathname

Apply .parent n times.

Returns:



8
9
10
# File 'lib/eac_ruby_utils/patches/pathname/parent_n.rb', line 8

def parent_n(n) # rubocop:disable Naming/MethodParameterName
  n.times.inject(self) { |a, _e| a.parent }
end


6
7
8
9
10
# File 'lib/eac_ruby_utils/patches/pathname/readlink_r.rb', line 6

def readlink_r
  r = self
  r = r.readlink while r.symlink?
  r
end

#reqdir_glob(*args) ⇒ Pathname

A .glob that raises a ::RuntimeError if self is not a directory.

Returns:



8
9
10
11
12
# File 'lib/eac_ruby_utils/patches/pathname/reqdir_glob.rb', line 8

def reqdir_glob(*args)
  raise "\"#{self}\" is not a directory" unless directory?

  glob(*args)
end