Class: Orthoses::ActiveSupport::MattrAccessor

Inherits:
Object
  • Object
show all
Defined in:
lib/orthoses/active_support/mattr_accessor.rb

Overview

class Module

def mattr_reader(*syms, instance_reader: true, instance_accessor: true, default: nil, location: nil)
def mattr_writer(*syms, instance_writer: true, instance_accessor: true, default: nil, location: nil)
def mattr_accessor(*syms, instance_reader: true, instance_writer: true, instance_accessor: true, default: nil, &blk)

Instance Method Summary collapse

Constructor Details

#initialize(loader, if: nil) ⇒ MattrAccessor

Returns a new instance of MattrAccessor.



10
11
12
13
# File 'lib/orthoses/active_support/mattr_accessor.rb', line 10

def initialize(loader, if: nil)
  @loader = loader
  @if = binding.local_variable_get(:if)
end

Instance Method Details

#callObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/orthoses/active_support/mattr_accessor.rb', line 15

def call
  mattr_reader = CallTracer::Lazy.new
  mattr_writer = CallTracer::Lazy.new

  store = mattr_reader.trace('Module#mattr_reader') do
    mattr_writer.trace('Module#mattr_writer') do
      @loader.call
    end
  end

  mattr_reader.captures.each do |capture|
    base = Orthoses::Utils.module_name(capture.method.receiver) || next
    methods = []
    capture.argument[:syms].each do |sym|
      next unless @if.nil? || @if.call(method, sym)

      methods << "def self.#{sym}: () -> untyped"
      if capture.argument[:instance_reader] && capture.argument[:instance_accessor]
        methods << "def #{sym}: () -> untyped"
      end
    end
    next if methods.empty?

    store[base].concat(methods)
  end

  mattr_writer.captures.each do |capture|
    base = Orthoses::Utils.module_name(capture.method.receiver) || next
    methods = []
    capture.argument[:syms].each do |sym|
      next unless @if.nil? || @if.call(method, sym)

      methods << "def self.#{sym}=: (untyped val) -> untyped"
      if capture.argument[:instance_writer] && capture.argument[:instance_accessor]
        methods << "def #{sym}=: (untyped val) -> untyped"
      end
    end
    next if methods.empty?

    store[base].concat(methods)
  end

  store
end