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)

69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/acfs/global.rb', line 69

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.loaded?
    block.call resource
  else
    resource.__callbacks__ << block
  end
end

#configure(&block) ⇒ undefined

Configure acfs using given block.

Returns:

  • (undefined)

See Also:


42
43
44
# File 'lib/acfs/global.rb', line 42

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

#on(*resources) ⇒ Object


83
84
85
86
87
88
89
# File 'lib/acfs/global.rb', line 83

def on(*resources)
  resources.each do |resource|
    add_callback resource do |_|
      yield(*resources) unless resources.any? {|res| !res.loaded? }
    end
  end
end

#resetObject

Reset all queues, stubs and internal state.


50
51
52
53
54
55
# File 'lib/acfs/global.rb', line 50

def reset
  ::ActiveSupport::Notifications.instrument 'acfs.reset' do
    runner.clear
    Acfs::Stub.clear
  end
end

#runundefined

Run all queued operations.

Returns:

  • (undefined)

28
29
30
31
32
33
# File 'lib/acfs/global.rb', line 28

def run
  ::ActiveSupport::Notifications.instrument 'acfs.before_run'
  ::ActiveSupport::Notifications.instrument 'acfs.run' do
    runner.start
  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:


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

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