Sha256: 204548a2134ca2ad6d77110521e57dd033bad16cce5910e106f47c19dee0cfd7

Contents?: true

Size: 615 Bytes

Versions: 5

Compression:

Stored size: 615 Bytes

Contents

# frozen_string_literal: true

# ##
#
# Ref: https://blog.appsignal.com/2018/10/02/ruby-magic-class-level-instance-variables.html

module SknUtils

  module Wrappable
    def wrap(mod)
      wrappers << mod
    end

    def wrappers
      @wrappers ||= []
    end

    def inherited_wrappers
      ancestors
          .grep(Wrappable)
          .reverse
          .flat_map(&:wrappers)
    end

    def new(*arguments, &block)
      instance = allocate
      inherited_wrappers.each { |mod|instance.singleton_class.include(mod) }
      instance.send(:initialize, *arguments, &block)
      instance
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
skn_utils-5.8.0 lib/skn_utils/wrappable.rb
skn_utils-5.7.0 lib/skn_utils/wrappable.rb
skn_utils-5.6.0 lib/skn_utils/wrappable.rb
skn_utils-5.5.0 lib/skn_utils/wrappable.rb
skn_utils-5.4.1 lib/skn_utils/wrappable.rb