Class: Tapioca::Dsl::Compilers::ActiveRecordBitwise
- Inherits:
-
Compiler
- Object
- Compiler
- Tapioca::Dsl::Compilers::ActiveRecordBitwise
- Extended by:
- T::Sig
- Defined in:
- lib/tapioca/dsl/compilers/activerecord_bitwise.rb
Overview
Tapioca DSL Compiler for Sorbet static analysis of ActiveRecord::Bitwise attributes and scopes.
Constant Summary collapse
- ConstantType =
The fixed constant type that this compiler is responsible for.
type_member { { fixed: T.class_of(ActiveRecord::Base) } }
Class Method Summary collapse
Instance Method Summary collapse
Class Method Details
.gather_constants ⇒ Object
108 109 110 111 112 |
# File 'lib/tapioca/dsl/compilers/activerecord_bitwise.rb', line 108 def self.gather_constants descendants_of(ActiveRecord::Base).select do |klass| klass.respond_to?(:bitwise_definitions) && T.unsafe(klass).bitwise_definitions.any? end end |
Instance Method Details
#decorate ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/tapioca/dsl/compilers/activerecord_bitwise.rb', line 115 def decorate T.unsafe(root).create_path(constant) do |model| model.create_method('bitwise_schema', parameters: [create_param('col', type: 'T.any(Symbol, String)')], return_type: 'T.nilable(T::Hash[Symbol, Integer])', class_method: true) end T.unsafe(constant).bitwise_definitions.each do |column_name, config| T.unsafe(root).create_path(constant) do |model| model.create_method(column_name.to_s, return_type: 'T::Array[T.any(Symbol, String)]') model.create_method("#{column_name}=", parameters: [create_param('val', type: 'T::Array[T.any(Symbol, String)]')], return_type: 'T::Array[T.any(Symbol, String)]') prefix = config[:prefix] suffix = config[:suffix] mapping = config[:mapping] prefix_str = case prefix when true then "#{T.unsafe(column_name.to_s).singularize}_" when Symbol, String then "#{prefix}_" else '' end suffix_str = case suffix when true then "_#{T.unsafe(column_name.to_s).singularize}" when Symbol, String then "_#{suffix}" else '' end mapping.each_key do |key| method_name = "#{prefix_str}#{key}#{suffix_str}" model.create_method("#{method_name}?", return_type: 'T::Boolean') model.create_method("#{method_name}=", parameters: [create_param('val', type: 'T::Boolean')], return_type: 'T::Boolean') model.create_method("#{method_name}!", return_type: 'TrueClass') end # Instance-level atomic methods singular_col = T.unsafe(column_name.to_s).singularize model.create_method("add_#{singular_col}!", parameters: [create_rest_param('values', type: 'T.any(Symbol, String)')], return_type: 'void') model.create_method("add_#{column_name}!", parameters: [create_rest_param('values', type: 'T.any(Symbol, String)')], return_type: 'void') model.create_method("remove_#{singular_col}!", parameters: [create_rest_param('values', type: 'T.any(Symbol, String)')], return_type: 'void') model.create_method("remove_#{column_name}!", parameters: [create_rest_param('values', type: 'T.any(Symbol, String)')], return_type: 'void') # Class-level atomic methods model.create_method("add_#{column_name}!", parameters: [ create_rest_param('values', type: 'T.any(Symbol, String)'), create_kw_param('records', type: 'T.untyped') ], return_type: 'void', class_method: true) model.create_method("remove_#{column_name}!", parameters: [ create_rest_param('values', type: 'T.any(Symbol, String)'), create_kw_param('records', type: 'T.untyped') ], return_type: 'void', class_method: true) # Scopes model.create_method("with_#{column_name}", parameters: [create_rest_param('values', type: 'T.any(Symbol, String)')], return_type: 'ActiveRecord::Relation', class_method: true) model.create_method("with_any_#{column_name}", parameters: [create_rest_param('values', type: 'T.any(Symbol, String)')], return_type: 'ActiveRecord::Relation', class_method: true) model.create_method("with_exact_#{column_name}", parameters: [create_rest_param('values', type: 'T.any(Symbol, String)')], return_type: 'ActiveRecord::Relation', class_method: true) model.create_method("without_#{column_name}", parameters: [create_rest_param('values', type: 'T.any(Symbol, String)')], return_type: 'ActiveRecord::Relation', class_method: true) end end end |