Module: Acfs::Global

Included in:
Acfs
Defined in:
lib/acfs/global.rb

Overview

Global Acfs module methods.

Instance Method Summary collapse

Instance Method Details

#add_callback(resource, &block) ⇒ Object

Add an additional callback hook to not loaded resource. If given resource already loaded callback will be invoked immediately.

This method will be replaced by explicit callback handling when query methods return explicit future objects.

Examples:

user = MyUser.find 1, &callback_one
Acfs.add_callback(user, &callback_two)


75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/acfs/global.rb', line 75

def add_callback(resource, &block)
  unless resource.respond_to?(:__callbacks__)
    raise ArgumentError.new 'Given resource is not an Acfs resource ' \
                            "delegator but a: #{resource.class.name}"
  end
  return false if block.nil?

  if resource.nil? || resource.loaded?
    yield resource
  else
    resource.__callbacks__ << block
  end
end

#configureundefined

Configure acfs using given block.

Returns:

  • (undefined)

See Also:



46
47
48
# File 'lib/acfs/global.rb', line 46

def configure(&)
  Configuration.current.configure(&)
end

#on(*resources) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/acfs/global.rb', line 89

def on(*resources)
  # If all resources have already been loaded, we run the callback immediately.
  if resources.all? {|res| res.nil? || res.loaded? }
    yield(*resources)
    return
  end

  # Otherwise, we add a callback to *each* resource with a guard that ensures
  # that only the very last resource being loaded executes the callback.
  resources.each do |resource|
    add_callback resource do |_|
      yield(*resources) if resources.all? {|res| res.nil? || res.loaded? }
    end
  end
end

#resetObject

Reset all queues, stubs and internal state.



54
55
56
57
58
59
60
61
# File 'lib/acfs/global.rb', line 54

def reset
  Telemetry.in_span('acfs.reset') do
    ::ActiveSupport::Notifications.instrument 'acfs.reset' do
      runner.clear
      Acfs::Stub.clear
    end
  end
end

#runundefined

Run all queued operations.

Returns:

  • (undefined)


30
31
32
33
34
35
36
37
# File 'lib/acfs/global.rb', line 30

def run
  Telemetry.in_span('acfs.run', attributes: {'code.stacktrace' => caller.join("\n")}) do
    ::ActiveSupport::Notifications.instrument 'acfs.before_run'
    ::ActiveSupport::Notifications.instrument 'acfs.run' do
      runner.start
    end
  end
end

#runnerRunner

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/acfs/global.rb', line 12

def runner
  Thread.current[:acfs_runner] ||= begin
    adapter = Configuration.current.adapter

    if adapter
      Runner.new adapter.call
    else
      Runner.new Adapter::Typhoeus.new
    end
  end
end