Class: Tapioca::Dsl::Compilers::AttrJson

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

Overview

‘Tapioca::Dsl::Compilers::AttrJson` decorates RBI files for classes that use the `AttrJson` gem. github.com/jrochkind/attr_json

For example, with the following ActiveRecord model: ~~~rb class Product < ActiveRecord::Base

include AttrJson::Record

attr_json :price_cents, :integer

end ~~~

This compiler will generate the following RBI: ~~~rbi class Product

include AttrJsonGeneratedMethods
extend AttrJson::Record::ClassMethods

module AttrJsonGeneratedMethods
  sig { returns(::Integer) }
  def price_cents; end

  sig { params(value: Integer).returns(::Integer) }
  def price_cents=(value); end
end

end ~~~

Constant Summary collapse

ClassMethodsModuleName =

Class methods module is already defined in the gem rbi, so just reference it here.

"AttrJson::Record::ClassMethods"
InstanceMethodModuleName =
"AttrJsonGeneratedMethods"
ConstantType =
type_member { { fixed: T.any(T.class_of(::AttrJson::Record), T.class_of(::AttrJson::Model)) } }

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.gather_constantsObject



48
49
50
# File 'lib/tapioca/dsl/compilers/attr_json.rb', line 48

def gather_constants
  all_classes.select { |constant| constant < ::AttrJson::Record || constant < ::AttrJson::Model }
end

Instance Method Details

#decorateObject



54
55
56
57
58
59
60
61
62
63
# File 'lib/tapioca/dsl/compilers/attr_json.rb', line 54

def decorate
  rbi_class = root.create_path(constant)
  instance_module = RBI::Module.new(InstanceMethodModuleName)

  decorate_attributes(instance_module)

  rbi_class << instance_module
  rbi_class.create_include(InstanceMethodModuleName)
  rbi_class.create_extend(ClassMethodsModuleName) if constant < ::AttrJson::Record
end