Sha256: c8f7f22a66c0b0d01e29d79af01cbc2a4d4d57abc23907ee54d11e1e6f569e44

Contents?: true

Size: 981 Bytes

Versions: 2

Compression:

Stored size: 981 Bytes

Contents

# frozen_string_literal: true

module Dynflow
  module Testing
    # when extended into Class or an_object it makes all instances of the class or the object
    # mimic the supplied types. It does so by hooking into kind_of? method.
    # @example
    #   m = mock('product')
    #   m.is_a? ::Product # => false
    #   m.extend Mimic
    #   m.mimic! ::Product
    #   m.is_a? ::Product # => true
    module Mimic
      class ::Module
        def ===(v)
          v.kind_of? self
        end
      end

      def mimic!(*types)
        define = ->_ do
          define_method :mimic_types do
            types
          end
          define_method :kind_of? do |type|
            types.any? { |t| t <= type } || super(type)
          end

          alias_method :is_a?, :kind_of?
        end

        if self.kind_of? ::Class
          self.class_eval(&define)
        else
          self.singleton_class.class_eval(&define)
        end

        self
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dynflow-1.9.0 lib/dynflow/testing/mimic.rb
dynflow-1.8.3 lib/dynflow/testing/mimic.rb