Class: Mocktail::DeclaresDryClass

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/mocktail/imitates_type/makes_double/declares_dry_class.rb,
lib/mocktail/sorbet/mocktail/imitates_type/makes_double/declares_dry_class.rb

Constant Summary collapse

DEFAULT_ANCESTORS =
T.let(T.must(Class.new(Object).ancestors[1..]), T::Array[T.any(T::Class[T.anything], Module)])

Instance Method Summary collapse

Constructor Details

#initializeDeclaresDryClass

Returns a new instance of DeclaresDryClass.



9
10
11
12
13
14
# File 'lib/mocktail/imitates_type/makes_double/declares_dry_class.rb', line 9

def initialize
  @raises_neato_no_method_error = RaisesNeatoNoMethodError.new
  @transforms_params = TransformsParams.new
  @stringifies_method_signature = StringifiesMethodSignature.new
  @grabs_original_method_parameters = GrabsOriginalMethodParameters.new
end

Instance Method Details

#declare(type, instance_methods) ⇒ Object



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
# File 'lib/mocktail/imitates_type/makes_double/declares_dry_class.rb', line 16

def declare(type, instance_methods)
  dry_class = Class.new(Object) {
    include type if type.is_a?(Module) && !type.is_a?(Class)

    define_method :initialize do |*args, **kwargs, &blk|
    end

    [:is_a?, :kind_of?].each do |method_name|
      define_method method_name, ->(thing) {
        # Mocktails extend from Object, so share the same ancestors, plus the passed type
        [type, *DEFAULT_ANCESTORS].include?(thing)
      }
    end

    if type.is_a?(Class)
      define_method :instance_of?, ->(thing) {
        type == thing
      }
    end
  }

  add_more_methods!(dry_class, type, instance_methods)

  dry_class  # This is all fake! That's the whole point—it's not a real Foo, it's just some new class that quacks like a Foo
end