Class: Tapioca::Compilers::Dsl::FrozenRecord

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

Overview

‘Tapioca::Compilers::Dsl::FrozenRecord` generates RBI files for subclasses of [`FrozenRecord::Base`](github.com/byroot/frozen_record).

For example, with the following FrozenRecord class:

~~~rb # student.rb class Student < FrozenRecord::Base end ~~~

and the following YAML file:

~~~ yaml # students.yml

  • id: 1 first_name: John last_name: Smith

  • id: 2 first_name: Dan last_name: Lord

~~~

this generator will produce the RBI file ‘student.rbi` with the following content:

~~~rbi # Student.rbi # typed: strong class Student

include FrozenRecordAttributeMethods

module FrozenRecordAttributeMethods
  sig { returns(T.untyped) }
  def first_name; end

  sig { returns(T::Boolean) }
  def first_name?; end

  sig { returns(T.untyped) }
  def id; end

  sig { returns(T::Boolean) }
  def id?; end

  sig { returns(T.untyped) }
  def last_name; end

  sig { returns(T::Boolean) }
  def last_name?; end
end

end ~~~

Constant Summary

Constants included from Reflection

Reflection::ANCESTORS_METHOD, Reflection::CLASS_METHOD, Reflection::CONSTANTS_METHOD, Reflection::EQUAL_METHOD, Reflection::METHOD_METHOD, Reflection::NAME_METHOD, Reflection::OBJECT_ID_METHOD, Reflection::PRIVATE_INSTANCE_METHODS_METHOD, Reflection::PROTECTED_INSTANCE_METHODS_METHOD, Reflection::PUBLIC_INSTANCE_METHODS_METHOD, Reflection::SINGLETON_CLASS_METHOD, Reflection::SUPERCLASS_METHOD

Instance Attribute Summary

Attributes inherited from Base

#errors, #processable_constants

Instance Method Summary collapse

Methods inherited from Base

#add_error, #handles?, #initialize

Methods included from Reflection

#ancestors_of, #are_equal?, #class_of, #constants_of, #descendants_of, #inherited_ancestors_of, #method_of, #name_of, #name_of_type, #object_id_of, #private_instance_methods_of, #protected_instance_methods_of, #public_instance_methods_of, #qualified_name_of, #signature_of, #singleton_class_of, #superclass_of

Constructor Details

This class inherits a constructor from Tapioca::Compilers::Dsl::Base

Instance Method Details

#decorate(root, constant) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/tapioca/compilers/dsl/frozen_record.rb', line 69

def decorate(root, constant)
  attributes = constant.attributes
  return if attributes.empty?

  root.create_path(constant) do |record|
    module_name = "FrozenRecordAttributeMethods"

    record.create_module(module_name) do |mod|
      attributes.each do |attribute|
        mod.create_method("#{attribute}?", return_type: "T::Boolean")
        mod.create_method(attribute.to_s, return_type: "T.untyped")
      end
    end

    record.create_include(module_name)
  end
end

#gather_constantsObject



88
89
90
# File 'lib/tapioca/compilers/dsl/frozen_record.rb', line 88

def gather_constants
  descendants_of(::FrozenRecord::Base).reject(&:abstract_class?)
end