File: mimic.rb

package info (click to toggle)
ruby-naught 1.1.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 276 kB
  • sloc: ruby: 1,091; makefile: 6
file content (55 lines) | stat: -rw-r--r-- 1,747 bytes parent folder | download | duplicates (2)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
require 'naught/basic_object'
require 'naught/null_class_builder/command'

module Naught
  class NullClassBuilder
    module Commands
      class Mimic < Naught::NullClassBuilder::Command
        NULL_SINGLETON_CLASS = (class << Object.new; self; end)

        attr_reader :class_to_mimic, :include_super, :singleton_class

        def initialize(builder, class_to_mimic_or_options, options = {})
          super(builder)

          if class_to_mimic_or_options.is_a?(Hash)
            options          = class_to_mimic_or_options.merge(options)
            instance         = options.fetch(:example)
            @singleton_class = (class << instance; self; end)
            @class_to_mimic  = instance.class
          else
            @singleton_class = NULL_SINGLETON_CLASS
            @class_to_mimic  = class_to_mimic_or_options
          end
          @include_super = options.fetch(:include_super) { true }

          builder.base_class   = root_class_of(@class_to_mimic)
          class_to_mimic       = @class_to_mimic
          builder.inspect_proc = lambda { "<null:#{class_to_mimic}>" }
          builder.interface_defined = true
        end

        def call
          defer do |subject|
            methods_to_stub.each do |method_name|
              builder.stub_method(subject, method_name)
            end
          end
        end

      private

        def root_class_of(klass)
          klass.ancestors.include?(Object) ? Object : Naught::BasicObject
        end

        def methods_to_stub
          methods_to_mimic =
            class_to_mimic.instance_methods(include_super) |
            singleton_class.instance_methods(false)
          methods_to_mimic - Object.instance_methods
        end
      end
    end
  end
end