Sha256: dd55c3c69c69c61fe80eb14039dd4724280ee3049ddd55b454f560e4e79f4f95
Contents?: true
Size: 1.41 KB
Versions: 2
Compression:
Stored size: 1.41 KB
Contents
# frozen_string_literal: true require_relative "errors" module Hanami # Calls `configure_for_slice(slice)` on the extended class whenever it is first # subclassed within a module namespace corresponding to a slice. # # @example # 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 # # @api private # @since 2.0.0 module SliceConfigurable class << self 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) slice = slice_for.(subclass) return unless slice subclass.configure_for_slice(slice) end end klass.singleton_class.prepend(inherited_mod) end private def slice_for(klass) return unless klass.name slices = Hanami.app.slices.to_a + [Hanami.app] slices.detect { |slice| klass.name.include?(slice.namespace.to_s) } end end def configure_for_slice(slice); end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
hanami-2.0.0.beta1.1 | lib/hanami/slice_configurable.rb |
hanami-2.0.0.beta1 | lib/hanami/slice_configurable.rb |