Class: Tapioca::Dsl::Compilers::FlagShihTzu
- Inherits:
-
Tapioca::Dsl::Compiler
- Object
- Tapioca::Dsl::Compiler
- Tapioca::Dsl::Compilers::FlagShihTzu
- Extended by:
- T::Sig
- Defined in:
- lib/tapioca/dsl/compilers/flag_shih_tzu.rb
Overview
‘Tapioca::Dsl::Compilers::FlagShihTzu` decorates RBI files for models using FlagShihTzu.
For example, with FlagShihTzu installed and the following ‘ActiveRecord::Base` subclass:
~~~rb class Post < ApplicationRecord
has_flags(
1 => :published,
2 => :deleted,
)
end ~~~
This compiler will produce the RBI file ‘post.rbi` with the following content:
~~~rbi # post.rbi # typed: true class Post
include FlagShihTzu
include FlagShihTzuGeneratedMethods
module FlagShihTzuGeneratedMethods
sig { returns(T::Boolean) }
def published; end
sig { params(value: T::Boolean).returns(T::Boolean) }
def published=(value); end
sig { returns(T::Boolean) }
def published?; end
sig { returns(T::Boolean) }
def deleted; end
sig { params(value: T::Boolean).returns(T::Boolean) }
def deleted=(value); end
sig { returns(T::Boolean) }
def deleted?; end
end
end ~~~
Constant Summary collapse
- ConstantType =
type_member { { fixed: T.all(T.class_of(::FlagShihTzu), ::FlagShihTzu::GeneratedClassMethods) } }
- InstanceMethodsModuleName =
"FlagShihTzuGeneratedMethods"
- ClassMethodsModuleName =
"::FlagShihTzu"
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.gather_constants ⇒ Object
61 62 63 |
# File 'lib/tapioca/dsl/compilers/flag_shih_tzu.rb', line 61 def gather_constants all_classes.select { |c| c < ::FlagShihTzu } end |
Instance Method Details
#decorate ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/tapioca/dsl/compilers/flag_shih_tzu.rb', line 67 def decorate return if constant.flag_mapping.blank? root.create_path(constant) do |klass| instance_module = RBI::Module.new(InstanceMethodsModuleName) # has_flags( # 1 => :warpdrive, # 2 => shields, # column: 'features', # ) constant.flag_mapping.each do |_, flags| # column: 'features', flags: { warpdrive: ..., shields: ... } flags.each do |flag_key, _| # .warpdrive # .warpdrive= # .warpdrive? # .warpdrive_changed? instance_module.create_method(flag_key.to_s, return_type: "T::Boolean") instance_module.create_method( "#{flag_key}=", parameters: [create_param("value", type: "T::Boolean")], return_type: "T::Boolean", ) instance_module.create_method("#{flag_key}?", return_type: "T::Boolean") instance_module.create_method("#{flag_key}_changed?", return_type: "T::Boolean") # .not_warpdrive # .not_warpdrive= # .not_warpdrive? instance_module.create_method("not_#{flag_key}", return_type: "T::Boolean") instance_module.create_method("not_#{flag_key}?", return_type: "T::Boolean") instance_module.create_method( "not_#{flag_key}=", parameters: [create_param("value", type: "T::Boolean")], return_type: "T::Boolean", ) # .has_warpdrive? instance_module.create_method("has_#{flag_key}?", return_type: "T::Boolean") end end klass << instance_module klass.create_include(ClassMethodsModuleName) klass.create_include(InstanceMethodsModuleName) end end |