Class: Tapioca::Dsl::Compilers::Skit

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

Overview

Tapioca::Dsl::Compilers::Skit decorates RBI files for ActiveRecord models that use Skit::Attribute for typed JSON attributes.

For example, with the following model:

class Address < T::Struct
  const :city, String
  const :zip, T.nilable(String)
end

class Customer < ActiveRecord::Base
  attribute :address, Skit::Attribute[Address]
end

this compiler will produce an RBI file with the following content:

# typed: strong

class Customer
  sig { returns(Address) }
  def address; end

  sig { params(value: T.untyped).returns(Address) }
  def address=(value); end
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.gather_constantsObject

: -> Enumerable



60
61
62
63
64
65
66
67
68
# File 'lib/tapioca/dsl/compilers/skit.rb', line 60

def gather_constants
  descendants = ActiveRecord::Base.descendants.reject(&:abstract_class?)

  descendants.select do |klass|
    klass.attribute_types.values.any? { |type| skit_attribute_type?(type) }
  rescue ActiveRecord::StatementInvalid
    false
  end
end

.skit_attribute_type?(type) ⇒ Boolean

: (untyped type) -> bool

Returns:

  • (Boolean)


71
72
73
# File 'lib/tapioca/dsl/compilers/skit.rb', line 71

def skit_attribute_type?(type)
  type.is_a?(::Skit::Attribute)
end

Instance Method Details

#decorateObject

: -> void



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/tapioca/dsl/compilers/skit.rb', line 43

def decorate
  attributes = constant.attribute_types.select { |_name, type| skit_attribute_type?(type) }

  return if attributes.empty?

  root.create_path(constant) do |klass|
    attributes.each do |attr_name, type|
      create_attribute_methods(klass, attr_name, type)
    end
  end
end