Sha256: 55481da0bbaa5f3deca08436dcd350c95fd635ad541d22131d83fbf1052cb3ef

Contents?: true

Size: 1.16 KB

Versions: 3

Compression:

Stored size: 1.16 KB

Contents

require "declarative/deep_dup"

module Declarative
  class Heritage < Array
    # Record inheritable assignments for replay in an inheriting class.
    def record(method, *args, &block)
      self << {method: method, args: DeepDup.(args), block: block} # DISCUSS: options.dup.
    end

    # Replay the recorded assignments on inheritor.
    # Accepts a block that will allow processing the arguments for every recorded statement.
    def call(inheritor, &block)
      each { |cfg| call!(inheritor, cfg, &block) }
    end

    module DSL
      def heritage
        @heritage ||= Heritage.new
      end
    end

    # To be extended into classes using Heritage. Inherits the heritage.
    module Inherited
      def inherited(subclass)
        super
        heritage.(subclass)
      end
    end

    # To be included into modules using Heritage. When included, inherits the heritage.
    module Included
      def included(mod)
        super
        heritage.(mod)
      end
    end

  private
    def call!(inheritor, cfg)
      yield cfg if block_given? # allow messing around with recorded arguments.

      inheritor.send(cfg[:method], *cfg[:args], &cfg[:block])
    end
  end
end

Version data entries

3 entries across 3 versions & 3 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/declarative-0.0.20/lib/declarative/heritage.rb
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/declarative-0.0.20/lib/declarative/heritage.rb
declarative-0.0.20 lib/declarative/heritage.rb