Class: Pathname

Inherits:
Object show all
Extended by:
Gorillib::Pathref
Defined in:
lib/gorillib/pathname.rb,
lib/gorillib/pathname/utils.rb

Constant Summary

Constants included from Gorillib::Pathref

Gorillib::Pathref::ROOT_PATHS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gorillib::Pathref

of, register_default_paths, register_path, register_paths, relpath_to, unregister_path

Class Method Details

.receive(obj) ⇒ Object



89
90
91
92
# File 'lib/gorillib/pathname.rb', line 89

def self.receive(obj)
  return obj if obj.nil?
  obj.is_a?(self) ? obj : new(obj)
end

Instance Method Details

#corenameObject

Returns the basename without extension (using self.extname as the extension).

Returns:

  • the basename without extension (using self.extname as the extension)



95
96
97
# File 'lib/gorillib/pathname.rb', line 95

def corename
  basename(self.extname)
end

#exists?(*args) ⇒ Boolean

same as #exist?

Returns:

  • (Boolean)


3
# File 'lib/gorillib/pathname/utils.rb', line 3

def exists?(*args) exist?(*args) ; end

#find_allObject

Like find, but returns an enumerable



19
20
21
# File 'lib/gorillib/pathname/utils.rb', line 19

def find_all
  Enumerator.new{|yielder| find{|path| yielder << path } }
end

#if_missing(options = {}, &block) ⇒ Object

Executes the block (passing the opened file) if the file does not exist. Ignores the block otherwise. The block is required.

Parameters:

  • options (defaults to: {})
  • options[:force] (Hash)

    a customizable set of options



31
32
33
34
35
36
37
38
# File 'lib/gorillib/pathname/utils.rb', line 31

def if_missing(options={}, &block)
  ArgumentError.block_required!(block)
  return self if exist? && (not options[:force])
  #
  mkparent
  open((options[:mode] || 'w'), &block)
  return self
end

#inspect_compactString

Returns compact string rendering.

Returns:

  • (String)

    compact string rendering



100
# File 'lib/gorillib/pathname.rb', line 100

def inspect_compact() to_path.dump ; end

#mkparentObject

Examples:

It chains nicely:

# put each file in eg. dest/f/foo.json
Pathname.of(:dest, slug[0..0], "#{slug}.json").mkparent.open('w') do |file|
  # ...
end


12
13
14
15
# File 'lib/gorillib/pathname/utils.rb', line 12

def mkparent
  dirname.mkpath
  return self
end