Sha256: ccceece8bc7f9c2bcef6a3383ae6e191734926b5ad85afedc745ddb3bddff113

Contents?: true

Size: 1.34 KB

Versions: 11

Compression:

Stored size: 1.34 KB

Contents

# frozen_string_literal: true

module Orthoses
  module ActiveRecord
    # <= 6.0
    #   not implemented
    # >= 6.1
    #   def delegated_type(role, types:, **options)
    class DelegatedType
      def initialize(loader)
        @loader = loader
      end

      def call
        delegated_type = CallTracer::Lazy.new
        store = delegated_type.trace('ActiveRecord::DelegatedType#delegated_type') do
          @loader.call
        end

        delegated_type.captures.each do |capture|
          base_name = Utils.module_name(capture.method.receiver) or next
          role = capture.argument[:role]
          types = capture.argument[:types]
          options = capture.argument[:options]
          primary_key = options[:primary_key] || "id"

          content = store[base_name]
          content << "def #{role}_class: () -> (#{types.join(' | ')})"
          content << "def #{role}_name: () -> String"
          content << "def build_#{role}: () -> (#{types.join(' | ')})"
          types.each do |type|
            scope_name = type.tableize.gsub("/", "_")
            singular = scope_name.singularize
            content << "def #{singular}?: () -> bool"
            content << "def #{singular}: () -> #{type}?"
            content << "def #{singular}_#{primary_key}: () -> Integer?"
          end
        end

        store
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
orthoses-rails-1.7.0 lib/orthoses/active_record/delegated_type.rb
orthoses-rails-1.6.0 lib/orthoses/active_record/delegated_type.rb
orthoses-rails-1.5.0 lib/orthoses/active_record/delegated_type.rb
orthoses-rails-1.4.0 lib/orthoses/active_record/delegated_type.rb
orthoses-rails-1.3.0 lib/orthoses/active_record/delegated_type.rb
orthoses-rails-1.2.0 lib/orthoses/active_record/delegated_type.rb
orthoses-rails-1.1.0 lib/orthoses/active_record/delegated_type.rb
orthoses-rails-1.0.0 lib/orthoses/active_record/delegated_type.rb
orthoses-rails-0.9.0 lib/orthoses/active_record/delegated_type.rb
orthoses-rails-0.8.0 lib/orthoses/active_record/delegated_type.rb
orthoses-rails-0.7.0 lib/orthoses/active_record/delegated_type.rb