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)


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

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

#configure(&block) ⇒ undefined

Configure acfs using given block.

Returns:

  • (undefined)

See Also:



44
45
46
# File 'lib/acfs/global.rb', line 44

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

#on(*resources) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/acfs/global.rb', line 85

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.



52
53
54
55
56
57
# File 'lib/acfs/global.rb', line 52

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

#runundefined

Run all queued operations.

Returns:

  • (undefined)


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

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:



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