Class: Tapioca::Dsl::Compilers::JsonbOperations

Inherits:
Tapioca::Dsl::Compiler
  • Object
show all
Defined in:
lib/tapioca/dsl/compilers/jsonb_operations.rb

Overview

Generates RBI signatures for the JSONB chain methods on every ActiveRecord model whose connection adapter is PostgreSQL.

For each such model, two top-level modules are generated containing the ‘where.<op>(…)` signatures, one per WhereChain class. Each WhereChain class is then reopened, the corresponding module is `include`d, and a nested `JsonbExpression` subclass is emitted whose comparator and JSONB predicate methods return the model’s relation type.

class User
  module GeneratedJsonbRelationMethods
    sig { ...returns(User::PrivateRelation) } ; def contains(*args, **kwargs); end
    sig { ...returns(User::PrivateRelationWhereChain::JsonbExpression) } ; def json_field(*args, **kwargs); end
    # ...
  end

  class PrivateRelationWhereChain
    include GeneratedJsonbRelationMethods

    module GeneratedJsonbExpressionMethods
      sig { ...returns(User::PrivateRelation) } ; def equals(value); end
      # ...
    end

    class JsonbExpression < ::JsonbOperations::ActiveRecord::JsonbExpression
      include GeneratedJsonbExpressionMethods
    end
  end

  # …same again for PrivateAssociationRelationWhereChain returning User::PrivateAssociationRelation
end

: [ConstantType = singleton(::ActiveRecord::Base)]

Constant Summary collapse

PREDICATE_METHODS =
%w[
  contains contained_by contains_key contains_any_key contains_all_keys path_exists path_match
].freeze
FETCH_METHODS =

: Array

%w[
  json_element json_field json_element_text json_field_text json_path json_path_text
].freeze
COMPARATOR_METHODS =

: Array

%w[
  equals
  not_equals
  greater_than
  greater_than_or_equal_to
  less_than
  less_than_or_equal_to
  between
  included_in
  not_included_in
  matches
  does_not_match
].freeze
SINGLE_ARG_PREDICATE_METHODS =

: Array

%w[
  contains contained_by contains_key path_exists path_match
].freeze
VARIADIC_PREDICATE_METHODS =

: Array

%w[contains_any_key contains_all_keys].freeze
BASE_JSONB_EXPRESSION =

: Array

'::JsonbOperations::ActiveRecord::JsonbExpression'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.gather_constantsObject



73
74
75
76
77
# File 'lib/tapioca/dsl/compilers/jsonb_operations.rb', line 73

def gather_constants
  ::ActiveRecord::Base.descendants
    .reject(&:abstract_class?)
    .select { |model| model.connection_db_config&.adapter.to_s == 'postgresql' }
end

Instance Method Details

#decorateObject

: -> void



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/tapioca/dsl/compilers/jsonb_operations.rb', line 82

def decorate
  model_name = T.must(constant.name)

  root.create_path(constant) do |model|
    decorate_chain(
      model,
      model_name: model_name,
      chain_class_name: 'PrivateRelationWhereChain',
      relation_type: "#{model_name}::PrivateRelation",
      methods_module_name: 'GeneratedJsonbRelationMethods',
    )
    decorate_chain(
      model,
      model_name: model_name,
      chain_class_name: 'PrivateAssociationRelationWhereChain',
      relation_type: "#{model_name}::PrivateAssociationRelation",
      methods_module_name: 'GeneratedJsonbAssociationRelationMethods',
    )
  end
end