Class: BatchLoader

Inherits:
Object
  • Object
show all
Defined in:
lib/batch_loader.rb,
lib/batch_loader/graphql.rb,
lib/batch_loader/version.rb,
lib/batch_loader/executor.rb,
lib/batch_loader/middleware.rb,
lib/batch_loader/executor_proxy.rb

Defined Under Namespace

Classes: Executor, ExecutorProxy, GraphQL, Middleware

Constant Summary collapse

IMPLEMENTED_INSTANCE_METHODS =
%i[object_id __id__ __send__ singleton_method_added __sync respond_to? batch inspect].freeze
REPLACABLE_INSTANCE_METHODS =
%i[batch inspect].freeze
LEFT_INSTANCE_METHODS =
(IMPLEMENTED_INSTANCE_METHODS - REPLACABLE_INSTANCE_METHODS).freeze
NoBatchError =
Class.new(StandardError)
VERSION =
"2.0.5"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item:, executor_proxy: nil) ⇒ BatchLoader

Returns a new instance of BatchLoader.



21
22
23
24
# File 'lib/batch_loader.rb', line 21

def initialize(item:, executor_proxy: nil)
  @item = item
  @__executor_proxy = executor_proxy
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, **kwargs, &block) ⇒ Object (private)



72
73
74
# File 'lib/batch_loader.rb', line 72

def method_missing(method_name, *args, **kwargs, &block)
  __sync!.public_send(method_name, *args, **kwargs, &block)
end

Class Method Details

.for(item) ⇒ Object



17
18
19
# File 'lib/batch_loader.rb', line 17

def self.for(item)
  new(item: item)
end

Instance Method Details

#__syncObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/batch_loader.rb', line 50

def __sync
  return @loaded_value if @synced

  __ensure_batched
  @loaded_value = __executor_proxy.loaded_value(item: @item)

  if @cache
    @synced = true
  else
    __purge_cache
  end

  @loaded_value
end

#batch(default_value: nil, cache: true, replace_methods: nil, key: nil, &batch_block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/batch_loader.rb', line 26

def batch(default_value: nil, cache: true, replace_methods: nil, key: nil, &batch_block)
  @default_value = default_value
  @cache = cache
  @replace_methods = replace_methods.nil? ? cache : replace_methods
  @key = key
  @batch_block = batch_block

  __executor_proxy.add(item: @item)

  __singleton_class.class_eval { undef_method(:batch) }

  self
end

#inspectObject



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

def inspect
  "#<BatchLoader:0x#{(object_id << 1)}>"
end

#respond_to?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
# File 'lib/batch_loader.rb', line 40

def respond_to?(method_name, include_private = false)
  return true if LEFT_INSTANCE_METHODS.include?(method_name)

  __loaded_value.respond_to?(method_name, include_private)
end