Class: SimpleMaster::Storage::OndemandTable

Inherits:
Table
  • Object
show all
Defined in:
lib/simple_master/storage/ondemand_table.rb

Constant Summary

Constants inherited from Table

Table::METADATA_PREFIX

Instance Attribute Summary

Attributes inherited from Table

#applied_diff, #dataset, #digest, #klass, #loader, #method_cache, #sub_tables

Instance Method Summary collapse

Methods inherited from Table

#apply_diff, #apply_diff_to_id_hash, #diff, #duplicate_for, #freeze_all, #initialize, #run_on_sub_tables, #tap_instance_methods, #update_class_method_cache, #update_grouped_hash, #update_id_hash

Constructor Details

This class inherits a constructor from SimpleMaster::Storage::Table

Instance Method Details

#allObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/simple_master/storage/ondemand_table.rb', line 17

def all
  return @all if @all

  if klass.sti_sub_class?
    klass.sti_base_class.all
    @all
  else
    load_records
    SimpleMaster.use_dataset(dataset) do
      tap_instance_methods
      freeze_all
    end
  end
end

#class_method_cacheObject



44
45
46
47
48
49
50
51
# File 'lib/simple_master/storage/ondemand_table.rb', line 44

def class_method_cache
  return @class_method_cache if @class_method_cache

  # make sure @all is loaded to prevent errors
  all

  update_class_method_cache
end

#grouped_hashObject



38
39
40
41
42
# File 'lib/simple_master/storage/ondemand_table.rb', line 38

def grouped_hash
  return @grouped_hash if @grouped_hash

  update_grouped_hash
end

#id_hashObject



32
33
34
35
36
# File 'lib/simple_master/storage/ondemand_table.rb', line 32

def id_hash
  return @id_hash if @id_hash

  update_id_hash
end

#load_recordsObject



12
13
14
15
# File 'lib/simple_master/storage/ondemand_table.rb', line 12

def load_records
  @class_method_cache = nil
  super
end

#sub_table(sub_klass) ⇒ Object



8
9
10
# File 'lib/simple_master/storage/ondemand_table.rb', line 8

def sub_table(sub_klass)
  (@sub_tables ||= klass.descendants.reject(&:abstract_class).index_with { |k| self.class.new(k, dataset, loader) })[sub_klass]
end

#update_sub_tablesObject



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/simple_master/storage/ondemand_table.rb', line 53

def update_sub_tables
  sub_klasses = klass.descendants.reject(&:abstract_class)
  return if sub_klasses.empty?

  grouped = all.group_by(&:class)
  sub_klasses.each do |sub_klass|
    sub_sub_klasses = [sub_klass, *sub_klass.descendants].reject(&:abstract_class)

    sub_table = self.sub_table(sub_klass)
    sub_table.all = sub_sub_klasses.flat_map { grouped[_1] || EMPTY_ARRAY }.freeze
    sub_table.digest = digest
  end
end