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 =
"1.0.3"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item:) ⇒ BatchLoader

Returns a new instance of BatchLoader.



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

def initialize(item:)
  @item = item
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



60
61
62
# File 'lib/batch_loader.rb', line 60

def method_missing(method_name, *args, &block)
  __sync!.public_send(method_name, *args, &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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/batch_loader.rb', line 43

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(cache: true, &batch_block) ⇒ Object



25
26
27
28
29
30
31
32
33
# File 'lib/batch_loader.rb', line 25

def batch(cache: true, &batch_block)
  @cache = cache
  @batch_block = batch_block
  __executor_proxy.add(item: @item)

  __singleton_class.class_eval { undef_method(:batch) }

  self
end

#inspectObject



39
40
41
# File 'lib/batch_loader.rb', line 39

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

#respond_to?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/batch_loader.rb', line 35

def respond_to?(method_name)
  LEFT_INSTANCE_METHODS.include?(method_name) || method_missing(:respond_to?, method_name)
end