|
| 1 | +# typed: strict |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +return unless defined?(FlagShihTzu) |
| 5 | + |
| 6 | +module Tapioca |
| 7 | + module Dsl |
| 8 | + module Compilers |
| 9 | + # `Tapioca::Dsl::Compilers::FlagShihTzu` decorates RBI files for models |
| 10 | + # using FlagShihTzu. |
| 11 | + # |
| 12 | + # For example, with FlagShihTzu installed and the following `ActiveRecord::Base` subclass: |
| 13 | + # |
| 14 | + # ~~~rb |
| 15 | + # class Post < ApplicationRecord |
| 16 | + # has_flags( |
| 17 | + # 1 => :published, |
| 18 | + # 2 => :deleted, |
| 19 | + # ) |
| 20 | + # end |
| 21 | + # ~~~ |
| 22 | + # |
| 23 | + # This compiler will produce the RBI file `post.rbi` with the following content: |
| 24 | + # |
| 25 | + # ~~~rbi |
| 26 | + # # post.rbi |
| 27 | + # # typed: true |
| 28 | + # class Post |
| 29 | + # include FlagShihTzu |
| 30 | + # include FlagShihTzuGeneratedMethods |
| 31 | + # |
| 32 | + # module FlagShihTzuGeneratedMethods |
| 33 | + # sig { returns(T::Boolean) } |
| 34 | + # def published; end |
| 35 | + # sig { params(value: T::Boolean).returns(T::Boolean) } |
| 36 | + # def published=(value); end |
| 37 | + # sig { returns(T::Boolean) } |
| 38 | + # def published?; end |
| 39 | + # |
| 40 | + # sig { returns(T::Boolean) } |
| 41 | + # def deleted; end |
| 42 | + # sig { params(value: T::Boolean).returns(T::Boolean) } |
| 43 | + # def deleted=(value); end |
| 44 | + # sig { returns(T::Boolean) } |
| 45 | + # def deleted?; end |
| 46 | + # end |
| 47 | + # end |
| 48 | + # ~~~ |
| 49 | + class FlagShihTzu < Tapioca::Dsl::Compiler |
| 50 | + extend T::Sig |
| 51 | + |
| 52 | + ConstantType = type_member { { fixed: T.all(T.class_of(::FlagShihTzu), ::FlagShihTzu::GeneratedClassMethods) } } |
| 53 | + |
| 54 | + InstanceMethodsModuleName = "FlagShihTzuGeneratedMethods" |
| 55 | + ClassMethodsModuleName = "::FlagShihTzu" |
| 56 | + |
| 57 | + class << self |
| 58 | + extend T::Sig |
| 59 | + |
| 60 | + sig { override.returns(T::Enumerable[Module]) } |
| 61 | + def gather_constants |
| 62 | + all_classes.select { |c| c < ::FlagShihTzu } |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + sig { override.void } |
| 67 | + def decorate |
| 68 | + return if constant.flag_mapping.blank? |
| 69 | + |
| 70 | + root.create_path(constant) do |klass| |
| 71 | + instance_module = RBI::Module.new(InstanceMethodsModuleName) |
| 72 | + |
| 73 | + # has_flags( |
| 74 | + # 1 => :warpdrive, |
| 75 | + # 2 => shields, |
| 76 | + # column: 'features', |
| 77 | + # ) |
| 78 | + constant.flag_mapping.each do |_, flags| |
| 79 | + # column: 'features', flags: { warpdrive: ..., shields: ... } |
| 80 | + flags.each do |flag_key, _| |
| 81 | + # .warpdrive |
| 82 | + # .warpdrive= |
| 83 | + # .warpdrive? |
| 84 | + # .warpdrive_changed? |
| 85 | + instance_module.create_method(flag_key.to_s, return_type: "T::Boolean") |
| 86 | + instance_module.create_method( |
| 87 | + "#{flag_key}=", |
| 88 | + parameters: [create_param("value", type: "T::Boolean")], |
| 89 | + return_type: "T::Boolean", |
| 90 | + ) |
| 91 | + instance_module.create_method("#{flag_key}?", return_type: "T::Boolean") |
| 92 | + instance_module.create_method("#{flag_key}_changed?", return_type: "T::Boolean") |
| 93 | + |
| 94 | + # .not_warpdrive |
| 95 | + # .not_warpdrive= |
| 96 | + # .not_warpdrive? |
| 97 | + instance_module.create_method("not_#{flag_key}", return_type: "T::Boolean") |
| 98 | + instance_module.create_method("not_#{flag_key}?", return_type: "T::Boolean") |
| 99 | + instance_module.create_method( |
| 100 | + "not_#{flag_key}=", |
| 101 | + parameters: [create_param("value", type: "T::Boolean")], |
| 102 | + return_type: "T::Boolean", |
| 103 | + ) |
| 104 | + |
| 105 | + # .has_warpdrive? |
| 106 | + instance_module.create_method("has_#{flag_key}?", return_type: "T::Boolean") |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + klass << instance_module |
| 111 | + klass.create_include(ClassMethodsModuleName) |
| 112 | + klass.create_include(InstanceMethodsModuleName) |
| 113 | + end |
| 114 | + end |
| 115 | + end |
| 116 | + end |
| 117 | + end |
| 118 | +end |
0 commit comments