Class: Hanami::Utils::LoadPaths

Inherits:
Object
  • Object
show all
Defined in:
lib/hanami/utils/load_paths.rb

Overview

A collection of loading paths.

Since:

  • 0.2.0

Instance Method Summary collapse

Constructor Details

#initialize(*paths) ⇒ Hanami::Utils::LoadPaths

Initialize a new collection for the given paths

Parameters:

  • paths (String, Pathname, Array<String>, Array<Pathname>)

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

See Also:

Since:

  • 0.2.0


22
23
24
# File 'lib/hanami/utils/load_paths.rb', line 22

def initialize(*paths)
  @paths = Utils::Kernel.Array(paths)
end

Instance Method Details

#==(other) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 0.6.0


144
145
146
147
148
149
150
151
# 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 {|pathname| ... } ⇒ void

This method returns an undefined value.

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.

Yields:

  • (pathname)

    the block of code that acts on the collection

Yield Parameters:

  • pathname (Pathname)

Raises:

  • (Errno::ENOENT)

    if one of the paths doesn't exist

Since:

  • 0.2.0


65
66
67
68
69
# File 'lib/hanami/utils/load_paths.rb', line 65

def each
  @paths.each do |path|
    yield realpath(path)
  end
end

#freezeObject

It freezes the object by preventing further modifications.

Examples:

require 'hanami/utils/load_paths'

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

paths.frozen?  # => true

paths.push '.' # => RuntimeError

See Also:

Since:

  • 0.2.0


137
138
139
140
# File 'lib/hanami/utils/load_paths.rb', line 137

def freeze
  super
  @paths.freeze
end

#initialize_copy(original) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Examples:

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=[".", "../.."]>

See Also:

Since:

  • 0.2.0


49
50
51
# File 'lib/hanami/utils/load_paths.rb', line 49

def initialize_copy(original)
  @paths = original.instance_variable_get(:@paths).dup
end

#push(*paths) ⇒ Hanami::Utils::LoadPaths Also known as: <<

Adds the given path(s).

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

Examples:

Basic usage

require 'hanami/utils/load_paths'

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

Chainable calls

require 'hanami/utils/load_paths'

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

Shovel alias (#<<)

require 'hanami/utils/load_paths'

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

Chainable calls with shovel alias (#<<)

require 'hanami/utils/load_paths'

paths = Hanami::Utils::LoadPaths.new
paths << '.' << '../..'

Parameters:

  • paths (String, Pathname, Array<String>, Array<Pathname>)

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

Returns:

Raises:

  • (RuntimeError)

    if the object was previously frozen

See Also:

Since:

  • 0.2.0


114
115
116
117
118
# File 'lib/hanami/utils/load_paths.rb', line 114

def push(*paths)
  @paths.push(*paths)
  @paths = Kernel.Array(@paths)
  self
end