Class: BulkLoader::ClassAttribute

Inherits:
Object
  • Object
show all
Defined in:
lib/bulk_loader/class_attribute.rb

Instance Method Summary collapse

Constructor Details

#initializeClassAttribute

Returns a new instance of ClassAttribute.



7
8
9
10
11
# File 'lib/bulk_loader/class_attribute.rb', line 7

def initialize
  @loader_of = {}

  @no_autoload_of = {}
end

Instance Method Details

#autoload?(name) ⇒ Boolean

Returns:

  • (Boolean)


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

def autoload?(name)
  !@no_autoload_of[name]
end

#define_loader(name, loader, autoload: true) ⇒ Object



36
37
38
39
40
# File 'lib/bulk_loader/class_attribute.rb', line 36

def define_loader(name, loader, autoload: true)
  @loader_of[name] = loader
  @no_autoload_of[name] = true unless autoload
  define_singleton_method(name) { loader }
end

#dupObject



46
47
48
49
50
51
52
# File 'lib/bulk_loader/class_attribute.rb', line 46

def dup
  new_object = self.class.new
  each do |key, loader|
    new_object.define_loader(key, loader)
  end
  new_object
end

#each(&block) ⇒ Object



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

def each(&block)
  @loader_of.each(&block)
end

#include?(name) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/bulk_loader/class_attribute.rb', line 13

def include?(name)
  @loader_of.include?(name)
end

#inspectObject



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

def inspect
  "#<BulkLoader::ClassAttribute:#{object_id} #{loader_inspect}>"
end

#load(loader_names, attributes, *args) ⇒ Object



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

def load(loader_names, attributes, *args)
  attrs = convert_attributes(attributes)

  loader_names = [loader_names] unless loader_names.is_a?(Array)
  loader_names.each do |name|
    raise LoaderNotFoundError, "bulk_loader :#{name} is not defined" unless @loader_of[name]

    @loader_of[name].load(attrs.map { |attr| attr.lazy(name) }, *args)
  end
end