Sha256: 456767a57bd4d70b5f45986b6515eb11c237f0b0d27c72250de6aef2f1d4cd9d

Contents?: true

Size: 1.72 KB

Versions: 7

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

module Orthoses
  module ActiveRecord
    # def scope(name, body, &block)
    class Scope
      def initialize(loader)
        @loader = loader
      end

      def call
        scope = CallTracer::Lazy.new
        store = scope.trace('ActiveRecord::Scoping::Named::ClassMethods#scope') do
          @loader.call
        end

        scope.captures.each do |capture|
          base_name = Utils.module_name(capture.method.receiver) or next

          name = capture.argument[:name]
          body = capture.argument[:body]

          definition = "#{name}: #{parameters_to_type(body.parameters)} -> #{base_name}::ActiveRecord_Relation"
          store[base_name] << "def self.#{definition}"
          store["#{base_name}::GeneratedRelationMethods"].header = "module #{base_name}::GeneratedRelationMethods"
          store["#{base_name}::GeneratedRelationMethods"] << "def #{definition}"
        end

        store
      end

      private

      def parameters_to_type(parameters)
        # @type var res: Array[String]
        res = []
        # @type var block: String?
        block = nil
        parameters.each do |(type, name)|
          case type
          when :req
            res << "untyped #{name}"
          when :opt
            res << "?untyped #{name}"
          when :keyreq
            res << "#{name}: untyped"
          when :key
            res << "?#{name}: untyped"
          when :rest
            res << "*untyped #{name}"
          when :keyrest
            res << "**untyped #{name}"
          when :block
            block = " { (*untyped) -> untyped }"
          else
            raise "unexpected: #{type}"
          end
        end
        "(#{res.join(", ")})#{block}"
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
orthoses-rails-1.3.0 lib/orthoses/active_record/scope.rb
orthoses-rails-1.2.0 lib/orthoses/active_record/scope.rb
orthoses-rails-1.1.0 lib/orthoses/active_record/scope.rb
orthoses-rails-1.0.0 lib/orthoses/active_record/scope.rb
orthoses-rails-0.9.0 lib/orthoses/active_record/scope.rb
orthoses-rails-0.8.0 lib/orthoses/active_record/scope.rb
orthoses-rails-0.7.0 lib/orthoses/active_record/scope.rb