Class: N1Loader::Loader

Inherits:
Object
  • Object
show all
Includes:
MonitorMixin, ArLazyPreload::LoaderPatch, Goldiloader::LoaderPatch
Defined in:
lib/n1_loader/core/loader.rb

Overview

Loader that performs the loading.

Subclasses must define perform method that accepts single argument and returns hash where key is the element and value is what we want to load.

Constant Summary collapse

UnsupportedGoldiloader =

Raised when a single object without Goldiloader context support was passed to an isolated loader.

Class.new(StandardError)
UnsupportedArLazyPreload =

Raised when a single object without ArLazyPreload context support was passed to an isolated loader.

Class.new(StandardError)

Class Attribute Summary collapse

Attributes included from ArLazyPreload::LoaderPatch

#context_setup

Attributes included from Goldiloader::LoaderPatch

#context_setup

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ArLazyPreload::LoaderPatch

#non_thread_safe_context_setting

Methods included from Goldiloader::LoaderPatch

#non_thread_safe_context_setting

Constructor Details

#initialize(elements, **args) ⇒ Loader

Returns a new instance of Loader.



44
45
46
47
# File 'lib/n1_loader/core/loader.rb', line 44

def initialize(elements, **args)
  @elements = elements
  @args = args
end

Class Attribute Details

.argumentsObject (readonly)

Returns the value of attribute arguments.



12
13
14
# File 'lib/n1_loader/core/loader.rb', line 12

def arguments
  @arguments
end

Class Method Details

.argument(name, **opts) ⇒ Object

Defines an argument that can be accessed within the loader.

First defined argument will have the value of first passed argument, meaning the order is important.

Parameters:

  • name (Symbol)
  • opts (Hash)

Options Hash (**opts):

  • optional (Boolean)

    false by default

  • default (Proc)


23
24
25
26
27
28
29
30
31
32
33
# File 'lib/n1_loader/core/loader.rb', line 23

def argument(name, **opts)
  opts[:optional] = true if opts[:default]

  @arguments ||= []

  define_method(name) do
    args.fetch(name) { args[name] = opts[:default]&.call }
  end

  @arguments << opts.merge(name: name)
end

.cache_key(&block) ⇒ Object

Defines a custom cache key that is calculated for passed arguments.



36
37
38
39
40
41
# File 'lib/n1_loader/core/loader.rb', line 36

def cache_key(&block)
  define_method(:cache_key) do
    check_arguments!
    instance_exec(&block)
  end
end

Instance Method Details

#cache_keyObject



62
63
64
65
# File 'lib/n1_loader/core/loader.rb', line 62

def cache_key
  check_arguments!
  args.values.map(&:object_id)
end

#for(element) ⇒ Object

Raises:



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/n1_loader/core/loader.rb', line 49

def for(element)
  return unless loaded?

  if loaded_by_identity.empty? && elements.any?
    raise NotFilled, "Nothing was preloaded, perhaps you forgot to use fulfill method"
  end

  return loaded_by_identity[element] if loaded_by_identity.key?(element)
  return loaded_by_value[element] if loaded_by_value.key?(element)

  raise NotLoaded, "The data was not preloaded for the given element"
end