Class: Tapioca::Dsl::Compilers::AlchemrestData

Inherits:
Tapioca::Dsl::Compiler
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/tapioca/dsl/compilers/alchemrest_data.rb

Overview

This compiler generates RBI files for classes that inherit from ‘Alchemrest::Data`.

For example, given the following class:

class Profile < Alchemrest::Data
  schema do |s|
    {
      required: {
        name: s.string,
      },
    }
  end
end

This compiler will generate an RBI file ‘profile.rbi` with the following content:

sig { returns(::String) }
def name; end

Constant Summary collapse

Type =
T.type_alias { T.any(Class, T::Types::Base) }
ConstantType =
type_member { { upper: T.class_of(Alchemrest::Data) } }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.gather_constantsObject



37
38
39
# File 'lib/tapioca/dsl/compilers/alchemrest_data.rb', line 37

def self.gather_constants
  descendants_of(Alchemrest::Data)
end

Instance Method Details

#array?(transform) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/tapioca/dsl/compilers/alchemrest_data.rb', line 69

def array?(transform)
  case transform
  when Alchemrest::Transforms::Typed
    transform.collection
  when Morpher::Transform::Array
    true
  when Morpher::Transform::Maybe
    array?(transform.send(:transform))
  else
    false
  end
end

#decorateObject



42
43
44
45
46
47
48
49
# File 'lib/tapioca/dsl/compilers/alchemrest_data.rb', line 42

def decorate
  root.create_path(constant) do |rbi|
    constant.graph.fields.each do |name, field|
      type = T::Utils.coerce(extract_type(field.output_type))
      rbi.create_method(name.to_s, return_type: type.to_s)
    end
  end
end

#extract_output_type(types) ⇒ Object



59
60
61
# File 'lib/tapioca/dsl/compilers/alchemrest_data.rb', line 59

def extract_output_type(types)
  types.many? ? T.any(*T.unsafe(types)) : types.sole
end

#extract_type(output_type) ⇒ Object



52
53
54
55
56
# File 'lib/tapioca/dsl/compilers/alchemrest_data.rb', line 52

def extract_type(output_type)
  return T.untyped if output_type.nil?

  output_type.sorbet_type
end

#nilable?(field) ⇒ Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/tapioca/dsl/compilers/alchemrest_data.rb', line 64

def nilable?(field)
  !field.required || field.transform.is_a?(Morpher::Transform::Maybe)
end