Class: BatchLoader

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
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 batch_loader? 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.2"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item:) ⇒ BatchLoader

Returns a new instance of BatchLoader.



24
25
26
# File 'lib/batch_loader.rb', line 24

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)



52
53
54
# File 'lib/batch_loader.rb', line 52

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

Class Method Details

.for(item) ⇒ Object



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

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

Instance Method Details

#batch(cache: true, &batch_block) ⇒ Object



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

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

#batch_loader?Boolean

Returns:

  • (Boolean)


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

def batch_loader?
  true
end

#inspectObject



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

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

#respond_to?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


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

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