Class: BulkLoader::Attribute

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

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ Attribute

Returns a new instance of Attribute.



5
6
7
8
# File 'lib/bulk_loader/attribute.rb', line 5

def initialize(target)
  @target = target
  @lazy_of ||= {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object (private)



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/bulk_loader/attribute.rb', line 36

def method_missing(name, *args)
  return super unless class_attribute.include?(name)

  names = [name].freeze
  define_singleton_method(name) do
    attr = lazy(name)
    class_attribute.load(names, [self]) if !attr.loaded? && class_attribute.autoload?(name)
    attr.get
  end
  public_send(name)
end

Instance Method Details

#class_attributeObject



30
31
32
# File 'lib/bulk_loader/attribute.rb', line 30

def class_attribute
  @target.class.bulk_loader
end

#lazy(name) ⇒ Object



10
11
12
# File 'lib/bulk_loader/attribute.rb', line 10

def lazy(name)
  @lazy_of[name] ||= BulkLoader::Lazy.new(@target, name: name)
end

#loaded?(name) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/bulk_loader/attribute.rb', line 14

def loaded?(name)
  lazy(name).loaded?
end

#marshal_dumpObject



18
19
20
21
22
23
# File 'lib/bulk_loader/attribute.rb', line 18

def marshal_dump
  {
    target: @target,
    lazy_of: @lazy_of
  }
end

#marshal_load(obj) ⇒ Object



25
26
27
28
# File 'lib/bulk_loader/attribute.rb', line 25

def marshal_load(obj)
  @target = obj[:target]
  @lazy_of = obj[:lazy_of]
end