Class: SorbetRails::ModelPlugins::EnumerableCollections

Inherits:
Base
  • Object
show all
Defined in:
lib/sorbet-rails/model_plugins/enumerable_collections.rb

Constant Summary

Constants inherited from Base

Base::Parameter

Instance Attribute Summary

Attributes inherited from Base

#available_classes, #model_class

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods included from SorbetRails::ModelUtils

#add_relation_query_method, #exists_class_method?, #exists_instance_method?, #model_assoc_proxy_class_name, #model_assoc_relation_class_name, #model_class, #model_class_name, #model_module_name, #model_relation_class_name

Constructor Details

This class inherits a constructor from SorbetRails::ModelPlugins::Base

Instance Method Details

#create_enumerable_methods_for(class_rbi) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/sorbet-rails/model_plugins/enumerable_collections.rb', line 32

def create_enumerable_methods_for(class_rbi)
  class_rbi.create_include("Enumerable")
  class_rbi.create_method(
    "each",
    parameters: [
      Parameter.new("&block", type: "T.proc.params(e: #{self.model_class_name}).void")
    ],
    implementation: true,
  )
  class_rbi.create_method(
    "flatten",
    parameters: [ Parameter.new("level", type: "T.nilable(Integer)") ],
    return_type: "T::Array[#{self.model_class_name}]",
  )
  # this is an escape hatch when there are conflicts in signatures of Enumerable & ActiveRecord
  class_rbi.create_method(
    "to_a",
    return_type: "T::Array[#{self.model_class_name}]",
  )
  # TODO use type_parameters(:U) when parlour supports it
  class_rbi.create_arbitrary(
    code: <<~RUBY
      sig do
        type_parameters(:U).params(
            blk: T.proc.params(arg0: Elem).returns(T.type_parameter(:U)),
        )
        .returns(T::Array[T.type_parameter(:U)])
      end
      def map(&blk); end
    RUBY
  )
end

#generate(root) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/sorbet-rails/model_plugins/enumerable_collections.rb', line 6

def generate(root)
  # model relation & association proxy are enumerable
  # we need to implement "each" in these methods so that they work
  model_relation_class_rbi = root.create_class(self.model_relation_class_name)
  create_enumerable_methods_for(model_relation_class_rbi)

  model_assoc_relation_rbi = root.create_class(self.model_assoc_relation_class_name)
  create_enumerable_methods_for(model_assoc_relation_rbi)

  model_assoc_proxy_class_rbi = root.create_class(self.model_assoc_proxy_class_name)
  create_enumerable_methods_for(model_assoc_proxy_class_rbi)

  # following methods only exists in an association proxy
  ["<<", "append", "push", "concat"].each do |method_name|
    elem = self.model_class_name
    model_assoc_proxy_class_rbi.create_method(
      method_name,
      parameters: [
        Parameter.new("*records", type: "T.any(#{elem}, T::Array[#{elem}])"),
      ],
      return_type: "T.self_type",
    )
  end
end