Sha256: dbde8a2606aa87d2f1af7df5d9994ace674ead8d605bd4c24c55a8749566a1ea

Contents?: true

Size: 949 Bytes

Versions: 62

Compression:

Stored size: 949 Bytes

Contents

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

62 entries across 62 versions & 1 rubygems

Version Path
dynflow-1.3.0 lib/dynflow/testing/mimic.rb
dynflow-1.2.3 lib/dynflow/testing/mimic.rb
dynflow-1.2.2 lib/dynflow/testing/mimic.rb
dynflow-1.2.1 lib/dynflow/testing/mimic.rb
dynflow-1.2.0 lib/dynflow/testing/mimic.rb
dynflow-1.2.0.pre1 lib/dynflow/testing/mimic.rb
dynflow-1.1.6 lib/dynflow/testing/mimic.rb
dynflow-1.1.5 lib/dynflow/testing/mimic.rb
dynflow-1.1.4 lib/dynflow/testing/mimic.rb
dynflow-1.1.3 lib/dynflow/testing/mimic.rb
dynflow-1.1.2 lib/dynflow/testing/mimic.rb
dynflow-1.1.1 lib/dynflow/testing/mimic.rb
dynflow-1.1.0 lib/dynflow/testing/mimic.rb
dynflow-1.0.5 lib/dynflow/testing/mimic.rb
dynflow-1.0.4 lib/dynflow/testing/mimic.rb
dynflow-1.0.3 lib/dynflow/testing/mimic.rb
dynflow-1.0.2 lib/dynflow/testing/mimic.rb
dynflow-1.0.1 lib/dynflow/testing/mimic.rb
dynflow-1.0.0 lib/dynflow/testing/mimic.rb
dynflow-0.8.37 lib/dynflow/testing/mimic.rb