Class: Tapioca::Dsl::Compilers::MeasuredRails

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

Overview

Tapioca::Dsl::Compilers::MeasuredRails refines RBI files for subclasses of ActiveRecord::Base that utilize the measured-rails DSL. This compiler is only responsible for defining the methods that would be created for measured fields that are defined in the Active Record model.

For example, with the following model class:

class Package < ActiveRecord::Base
  measured Measured::Weight, :minimum_weight
  measured Measured::Length, :total_length
  measured Measured::Volume, :total_volume
end

this compiler will produce the following methods in the RBI file package.rbi:

# package.rbi
# typed: true

class Package
  include GeneratedMeasuredRailsMethods

  module GeneratedMeasuredRailsMethods
    sig { returns(T.nilable(Measured::Weight)) }
    def minimum_weight; end

    sig { params(value: T.nilable(Measured::Weight)).void }
    def minimum_weight=(value); end

    sig { returns(T.nilable(Measured::Length)) }
    def total_length; end

    sig { params(value: T.nilable(Measured::Length)).void }
    def total_length=(value); end

    sig { returns(T.nilable(Measured::Volume)) }
    def total_volume; end

    sig { params(value: T.nilable(Measured::Volume)).void }
    def total_volume=(value); end
  end
end

Constant Summary collapse

ConstantType =
type_member { {
  fixed: T.all(
    T.class_of(::ActiveRecord::Base),
    ::Measured::Rails::ActiveRecord::ClassMethods
  )
} }
MeasuredMethodsModuleName =
T.let("GeneratedMeasuredRailsMethods", String)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.gather_constantsObject



82
83
84
# File 'lib/tapioca/dsl/compilers/measured_rails.rb', line 82

def self.gather_constants
  descendants_of(::ActiveRecord::Base)
end

Instance Method Details

#decorateObject



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

def decorate
  return if constant.measured_fields.empty?

  root.create_path(constant) do |model|
    model.create_module(MeasuredMethodsModuleName) do |mod|
      populate_measured_methods(mod)
    end

    model.create_include(MeasuredMethodsModuleName)
  end
end