class Hanami::Utils::LoadPaths

A collection of loading paths.

@since 0.2.0

Attributes

paths[R]

@since 0.6.0 @api private

Public Class Methods

new(*paths) click to toggle source

Initialize a new collection for the given paths

@param paths [String, Pathname, Array<String>, Array<Pathname>] A single

or a collection of objects that can be converted into a Pathname

@return [Hanami::Utils::LoadPaths] self

@since 0.2.0

@see ruby-doc.org/stdlib/libdoc/pathname/rdoc/Pathname.html @see Hanami::Utils::Kernel.Pathname

# File lib/hanami/utils/load_paths.rb, line 22
def initialize(*paths)
  @paths = Utils::Kernel.Array(paths)
end

Public Instance Methods

<<(*paths)
Alias for: push
==(other) click to toggle source

@since 0.6.0 @api private

# File lib/hanami/utils/load_paths.rb, line 144
def ==(other)
  case other
  when self.class
    other.paths == paths
  else
    other == paths
  end
end
each() { |realpath(path)| ... } click to toggle source

Iterates through the collection and yields the given block. It skips duplications and raises an error in case one of the paths doesn’t exist.

@yield [pathname] the block of code that acts on the collection @yieldparam pathname [Pathname]

@return [void]

@raise [Errno::ENOENT] if one of the paths doesn’t exist

@since 0.2.0

# File lib/hanami/utils/load_paths.rb, line 65
def each
  @paths.each do |path|
    yield realpath(path)
  end
end
freeze() click to toggle source

It freezes the object by preventing further modifications.

@since 0.2.0

@see ruby-doc.org/core/Object.html#method-i-freeze

@example

require 'hanami/utils/load_paths'

paths = Hanami::Utils::LoadPaths.new
paths.freeze

paths.frozen?  # => true

paths.push '.' # => RuntimeError
Calls superclass method
# File lib/hanami/utils/load_paths.rb, line 137
def freeze
  super
  @paths.freeze
end
initialize_copy(original) click to toggle source

It specifies the policy for initialize copies of the object, when clone or dup are invoked.

@api private @since 0.2.0

@see ruby-doc.org/core/Object.html#method-i-clone @see ruby-doc.org/core/Object.html#method-i-dup

@example

require 'hanami/utils/load_paths'

paths  = Hanami::Utils::LoadPaths.new '.'
paths2 = paths.dup

paths  << '..'
paths2 << '../..'

paths
  # => #<Hanami::Utils::LoadPaths:0x007f84e0cad430 @paths=[".", ".."]>

paths2
  # => #<Hanami::Utils::LoadPaths:0x007faedc4ad3e0 @paths=[".", "../.."]>
# File lib/hanami/utils/load_paths.rb, line 49
def initialize_copy(original)
  @paths = original.instance_variable_get(:@paths).dup
end
push(*paths) click to toggle source

Adds the given path(s).

It returns self, so that multiple operations can be performed.

@param paths [String, Pathname, Array<String>, Array<Pathname>] A single

or a collection of objects that can be converted into a Pathname

@return [Hanami::Utils::LoadPaths] self

@raise [RuntimeError] if the object was previously frozen

@since 0.2.0

@see ruby-doc.org/stdlib/libdoc/pathname/rdoc/Pathname.html @see Hanami::Utils::Kernel.Pathname @see Hanami::Utils::LoadPaths#freeze

@example Basic usage

require 'hanami/utils/load_paths'

paths = Hanami::Utils::LoadPaths.new
paths.push '.'
paths.push '..', '../..'

@example Chainable calls

require 'hanami/utils/load_paths'

paths = Hanami::Utils::LoadPaths.new
paths.push('.')
     .push('..', '../..')

@example Shovel alias (<<)

require 'hanami/utils/load_paths'

paths = Hanami::Utils::LoadPaths.new
paths << '.'
paths << ['..', '../..']

@example Chainable calls with shovel alias (<<)

require 'hanami/utils/load_paths'

paths = Hanami::Utils::LoadPaths.new
paths << '.' << '../..'
# File lib/hanami/utils/load_paths.rb, line 114
def push(*paths)
  @paths.push(*paths)
  @paths = Kernel.Array(@paths)
  self
end
Also aliased as: <<

Private Instance Methods

realpath(path) click to toggle source

Allows subclasses to define their own policy to discover the realpath of the given path.

@since 0.2.0 @api private

# File lib/hanami/utils/load_paths.rb, line 166
def realpath(path)
  Utils::Kernel.Pathname(path).realpath
end