Module: PireLoader::Patches::Relation

Defined in:
lib/pire_loader/patches/relation.rb

Instance Method Summary collapse

Instance Method Details

#after_load(&blk) ⇒ Object



54
55
56
# File 'lib/pire_loader/patches/relation.rb', line 54

def after_load(&blk)
  @values[:after_load] = blk
end

#after_load_callbackObject

After load methods #



50
51
52
# File 'lib/pire_loader/patches/relation.rb', line 50

def after_load_callback
  @values.fetch(:after_load, nil)
end

#inject_selfObject



69
70
71
# File 'lib/pire_loader/patches/relation.rb', line 69

def inject_self
  @records.each { |record| record.parent_relation = self } if lazy_load_values.present?
end

#lazy_load(*args) ⇒ Object

Lazy load interface #



9
10
11
# File 'lib/pire_loader/patches/relation.rb', line 9

def lazy_load(*args)
  spawn.lazy_load!(*args)
end

#lazy_load!(*args) ⇒ Object

:nodoc:



13
14
15
16
17
18
19
# File 'lib/pire_loader/patches/relation.rb', line 13

def lazy_load!(*args) # :nodoc:
  # args.compact_blank!
  args.flatten!

  self.lazy_load_values |= args
  self
end

#lazy_load_valuesObject



21
22
23
# File 'lib/pire_loader/patches/relation.rb', line 21

def lazy_load_values
  @values.fetch(:lazy_load, [])
end

#lazy_load_values=(value) ⇒ Object



25
26
27
28
# File 'lib/pire_loader/patches/relation.rb', line 25

def lazy_load_values=(value)
  assert_mutability!
  @values[:lazy_load] = value
end

#loadObject

Loading



62
63
64
65
66
67
# File 'lib/pire_loader/patches/relation.rb', line 62

def load(*)
  super.tap do
    inject_self
    run_after_load
  end
end

#parent_relationObject

Parent relation methods #



36
37
38
# File 'lib/pire_loader/patches/relation.rb', line 36

def parent_relation
  @values.fetch(:parent_relation, nil)
end

#parent_relation=(value) ⇒ Object



40
41
42
# File 'lib/pire_loader/patches/relation.rb', line 40

def parent_relation=(value)
  @values[:parent_relation] = value
end

#run_after_loadObject

This can also modify the @records if the callback returns a new collection!



75
76
77
78
79
80
# File 'lib/pire_loader/patches/relation.rb', line 75

def run_after_load
  return unless after_load_callback

  new_records = after_load_callback.call(@records)
  @records = new_records.freeze if new_records.present?
end