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 ~~~

Instance Attribute Summary

Attributes inherited from Base

#processable_constants

Instance Method Summary collapse

Methods inherited from Base

#handles?, #initialize

Constructor Details

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

Instance Method Details

#decorate(root, constant) ⇒ Object



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

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

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

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

    record.create_include(module_name)
  end
end

#gather_constantsObject



90
91
92
# File 'lib/tapioca/compilers/dsl/frozen_record.rb', line 90

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