Class: Orthoses::ActiveRecord::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/orthoses/active_record/scope.rb

Overview

def scope(name, body, &block)

Instance Method Summary collapse

Constructor Details

#initialize(loader) ⇒ Scope

Returns a new instance of Scope.



7
8
9
# File 'lib/orthoses/active_record/scope.rb', line 7

def initialize(loader)
  @loader = loader
end

Instance Method Details

#callObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/orthoses/active_record/scope.rb', line 11

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(parameters(body))} -> #{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