Class: Tapioca::Compilers::Dsl::ActiveModelAttributes

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

Overview

‘Tapioca::Compilers::Dsl::ActiveModelAttributes` decorates RBI files for all classes that use [`ActiveModel::Attributes`](edgeapi.rubyonrails.org/classes/ActiveModel/Attributes/ClassMethods.html).

For example, with the following class:

~~~rb class Shop

include ActiveModel::Attributes

attribute :name, :string

end ~~~

this generator will produce an RBI file with the following content: ~~~rbi # typed: true

class Shop

sig { returns(T.nilable(::String)) }
def name; end

sig { params(name: T.nilable(::String)).returns(T.nilable(::String)) }
def name=(name); 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



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

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

  root.create_path(constant) do |klass|
    attribute_methods.each do |method, attribute_type|
      generate_method(klass, method, attribute_type)
    end
  end
end

#gather_constantsObject



55
56
57
# File 'lib/tapioca/compilers/dsl/active_model_attributes.rb', line 55

def gather_constants
  all_classes.grep(::ActiveModel::Attributes::ClassMethods)
end