Sha256: c1690e82dc15832819ea6256f05e970fa244adc984c2d0e3070c8f25b851d5f6

Contents?: true

Size: 1012 Bytes

Versions: 1

Compression:

Stored size: 1012 Bytes

Contents

module Bogus
  class MakesDucks
    extend Bogus::Takes

    takes :class_methods, :instance_methods, :makes_subtypes

    def make(first_class, *classes)
      duck = makes_subtypes.make(first_class)
      classes.each do |klass|
        remove_methods(class_methods.call(duck),
                       class_methods.call(klass))
        remove_methods(instance_methods.call(duck),
                       instance_methods.call(klass))
      end
      duck
    end

    private

    def remove_methods(duck_methods, klass_methods)
      not_in_klass = duck_methods.all - klass_methods.all
      not_in_klass.each { |name| duck_methods.remove(name) }

      duck_methods.all.each do |name|
        duck_method = duck_methods.get(name)
        klass_method = klass_methods.get(name)
        unless same_interface?(duck_method, klass_method)
          duck_methods.remove(name)
        end
      end
    end

    def same_interface?(method1, method2)
      method1.parameters == method2.parameters
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bogus-0.0.4 lib/bogus/makes_ducks.rb