Class: Hanami::SliceName

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

Overview

Represents the name of an App or Slice.

Instance Method Summary collapse

Constructor Details

#initialize(slice, inflector:) ⇒ SliceName

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.

Returns a new SliceName for the slice or app.

You must provide an inflector for the manipulation of the name into various formats. This should be given in the form of a Proc that returns the inflector when called. The reason for this is that the inflector may be replaced by the user during the app configuration phase, so the proc should ensure that the current instance of the inflector is returned whenever needed.

Parameters:

  • slice (#name)

    the slice or app object

  • inflector (Proc)

    Proc returning the app’s inflector when called

Since:

  • 2.0.0



26
27
28
29
# File 'lib/hanami/slice_name.rb', line 26

def initialize(slice, inflector:)
  @slice = slice
  @inflector = inflector
end

Instance Method Details

#nameString Also known as: path, to_s

Returns the name of the slice as a downcased, underscored string.

This is considered the canonical name of the slice.

Examples:

slice_name.name # => "main"

Returns:

  • (String)

    the slice name

Since:

  • 2.0.0



42
43
44
# File 'lib/hanami/slice_name.rb', line 42

def name
  inflector.underscore(namespace_name)
end

#namespace_constModule Also known as: namespace

Returns the constant for the slice’s module namespace.

Examples:

slice_name.namespace_const # => Main

Returns:

  • (Module)

    the namespace module constant

Since:

  • 2.0.0



72
73
74
# File 'lib/hanami/slice_name.rb', line 72

def namespace_const
  inflector.constantize(namespace_name)
end

#namespace_nameString

Returns the name of the slice’s module namespace.

Examples:

slice_name.namespace_name # => "Main"

Returns:

  • (String)

    the namespace name

Since:

  • 2.0.0



59
60
61
# File 'lib/hanami/slice_name.rb', line 59

def namespace_name
  slice_name.split(MODULE_DELIMITER)[0..-2].join(MODULE_DELIMITER)
end

#to_symSymbol

Returns the name of a slice as a downcased, underscored symbol.

Examples:

slice_name.name # => :main

Returns:

  • (Symbol)

    the slice name

See Also:

  • to_s

Since:

  • 2.0.0



95
96
97
# File 'lib/hanami/slice_name.rb', line 95

def to_sym
  name.to_sym
end