Sha256: 4cb7b29d8c89ce1320b9efe47a8617566c938813f3b0433c26b055ffbc3bed1e

Contents?: true

Size: 1.29 KB

Versions: 4

Compression:

Stored size: 1.29 KB

Contents

module Flows
  module Plugin
    # Class extension with method `MyClass.call` which works like `MyClass.new.call`.
    #
    # @note This module must be injected into target class using `extend`, not `include`.
    #
    # @note Class inheritance is supported: each child class will inherit behaviour, but not data.
    #
    # @example Extending a class
    #   class SomeClass
    #     extend Flows::Plugin::ImplicitInit
    #
    #     def initialize(param: 'default')
    #       @param = param
    #     end
    #
    #     def call
    #       @param
    #     end
    #   end
    #
    #   SomeClass.call
    #   # => 'default'
    #
    #   SomeClass.default_instance.call
    #   # => 'default'
    # @since 0.4.0
    module ImplicitInit
      # Contains memoized instance of a host class or `nil`.
      attr_reader :default_instance

      # Creates an instance of a host class by calling `new` without arguments and
      # calls `#call` method on the instance with provided parameters and block.
      #
      # After first invocation the instance will be memoized in {.default_instance}.
      #
      # Child classes have separate default instances.
      def call(*args, **kwargs, &block)
        @default_instance ||= new

        default_instance.call(*args, **kwargs, &block)
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
flows-0.6.0 lib/flows/plugin/implicit_init.rb
flows-0.5.1 lib/flows/plugin/implicit_init.rb
flows-0.5.0 lib/flows/plugin/implicit_init.rb
flows-0.4.0 lib/flows/plugin/implicit_init.rb