Class: Tapioca::Compilers::Dsl::SmartProperties

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

Overview

`Tapioca::Compilers::Dsl::SmartProperties` generates RBI files for classes that include [`SmartProperties`](github.com/t6d/smart_properties).

For example, with the following class that includes `SmartProperties`:

~~~rb # post.rb class Post

include(SmartProperties)

property :title, accepts: String
property! :description, accepts: String
property :published, accepts: [true, false], reader: :published?
property :enabled, accepts: [true, false], default: false

end ~~~

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

~~~rbi # post.rbi # typed: true class Post

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

sig { params(title: T.nilable(::String)).returns(T.nilable(::String)) }
def title=(title); end

sig { returns(::String) }
def description; end

sig { params(description: ::String).returns(::String) }
def description=(description); end

sig { returns(T.nilable(T::Boolean)) }
def published?; end

sig { params(published: T.nilable(T::Boolean)).returns(T.nilable(T::Boolean)) }
def published=(published); end

sig { returns(T.nilable(T::Boolean)) }
def enabled; end

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


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

def decorate(root, constant)
  properties = T.let(
    T.unsafe(constant).properties,
    ::SmartProperties::PropertyCollection
  )
  return if properties.keys.empty?

  instance_methods = constant.instance_methods(false).map(&:to_s).to_set

  root.create_path(constant) do |k|
    properties.values.each do |property|
      generate_methods_for_property(k, property) do |method_name|
        !instance_methods.include?(method_name.to_sym)
      end
    end
  end
end

#gather_constantsObject


86
87
88
89
90
91
92
# File 'lib/tapioca/compilers/dsl/smart_properties.rb', line 86

def gather_constants
  all_modules.select do |c|
    name_of(c) &&
      c != ::SmartProperties::Validations::Ancestor &&
      c < ::SmartProperties && ::SmartProperties::ClassMethods === c
  end
end