Module: Hanami::SliceConfigurable Private

Included in:
Extensions::View::PartHelpers
Defined in:
lib/hanami/slice_configurable.rb

Overview

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

Calls ‘configure_for_slice(slice)` on the extended class whenever it is first subclassed within a module namespace corresponding to a slice.

Examples:

class BaseClass
  extend Hanami::SliceConfigurable
end

# slices/main/lib/my_class.rb
module Main
  class MyClass < BaseClass
    # Will be called with `Main::Slice`
    def self.configure_for_slice(slice)
      # ...
    end
  end
end

Since:

  • 2.0.0

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(klass) ⇒ 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:

  • 2.0.0



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/hanami/slice_configurable.rb', line 29

def extended(klass)
  slice_for = method(:slice_for)

  inherited_mod = Module.new do
    define_method(:inherited) do |subclass|
      unless Hanami.app?
        raise ComponentLoadError, "Class #{klass} must be defined within an Hanami app"
      end

      super(subclass)

      subclass.instance_variable_set(:@configured_for_slices, configured_for_slices.dup)

      slice = slice_for.(subclass)
      return unless slice

      unless subclass.configured_for_slice?(slice)
        subclass.configure_for_slice(slice)
        subclass.configured_for_slices << slice
      end
    end
  end

  klass.singleton_class.prepend(inherited_mod)
end

Instance Method Details

#configure_for_slice(slice) ⇒ 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:

  • 2.0.0



66
# File 'lib/hanami/slice_configurable.rb', line 66

def configure_for_slice(slice); end

#configured_for_slice?(slice) ⇒ Boolean

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:

  • (Boolean)

Since:

  • 2.0.0



68
69
70
# File 'lib/hanami/slice_configurable.rb', line 68

def configured_for_slice?(slice)
  configured_for_slices.include?(slice)
end

#configured_for_slicesObject

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:

  • 2.0.0



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

def configured_for_slices
  @configured_for_slices ||= []
end