File: makes_ducks.rb

package info (click to toggle)
ruby-bogus 0.1.5-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 828 kB
  • ctags: 628
  • sloc: ruby: 4,124; makefile: 6; sh: 2
file content (36 lines) | stat: -rw-r--r-- 899 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
module Bogus
  class MakesDucks
    extend Takes

    takes :method_copiers, :makes_subtypes

    def make(first_class, *classes)
      duck = makes_subtypes.make(first_class)
      classes.each do |klass|
        method_copiers.each do |copier|
          remove_methods(copier.call(duck), copier.call(klass))
        end
      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