Class: SimpleMaster::Storage::Dataset
- Inherits:
-
Object
- Object
- SimpleMaster::Storage::Dataset
- Defined in:
- lib/simple_master/storage/dataset.rb
Constant Summary collapse
- DEFAULT_TABLE =
Table- DEFAULT_LOADER =
Loader::QueryLoader
Instance Attribute Summary collapse
-
#cache ⇒ Object
Returns the value of attribute cache.
-
#diff ⇒ Object
Returns the value of attribute diff.
-
#load_targets ⇒ Object
Returns the value of attribute load_targets.
-
#loader ⇒ Object
readonly
Returns the value of attribute loader.
-
#table_class ⇒ Object
readonly
Returns the value of attribute table_class.
-
#tables ⇒ Object
Returns the value of attribute tables.
Class Method Summary collapse
Instance Method Summary collapse
- #cache_delete(key) ⇒ Object
- #cache_fetch(key) ⇒ Object
-
#cache_read(key) ⇒ Object
Cache helper for other data sources.
- #cache_write(key, value) ⇒ Object
-
#duplicate(diff: nil) ⇒ Object
NOTE: Pass a empty hash to duplicate with empty diff.
-
#initialize(table_class: DEFAULT_TABLE, loader: nil) ⇒ Dataset
constructor
A new instance of Dataset.
- #initialize_cache ⇒ Object
- #load ⇒ Object
- #reload ⇒ Object
- #reload_klass(klass) ⇒ Object (also: #reload_class)
- #table(klass) ⇒ Object
- #unload ⇒ Object
Constructor Details
#initialize(table_class: DEFAULT_TABLE, loader: nil) ⇒ Dataset
Returns a new instance of Dataset.
35 36 37 38 39 40 41 |
# File 'lib/simple_master/storage/dataset.rb', line 35 def initialize(table_class: DEFAULT_TABLE, loader: nil) @table_class = table_class @loader = loader || DEFAULT_LOADER.new @diff = {} initialize_cache end |
Instance Attribute Details
#cache ⇒ Object
Returns the value of attribute cache.
32 33 34 |
# File 'lib/simple_master/storage/dataset.rb', line 32 def cache @cache end |
#diff ⇒ Object
Returns the value of attribute diff.
30 31 32 |
# File 'lib/simple_master/storage/dataset.rb', line 30 def diff @diff end |
#load_targets ⇒ Object
Returns the value of attribute load_targets.
33 34 35 |
# File 'lib/simple_master/storage/dataset.rb', line 33 def load_targets @load_targets end |
#loader ⇒ Object (readonly)
Returns the value of attribute loader.
29 30 31 |
# File 'lib/simple_master/storage/dataset.rb', line 29 def loader @loader end |
#table_class ⇒ Object (readonly)
Returns the value of attribute table_class.
28 29 30 |
# File 'lib/simple_master/storage/dataset.rb', line 28 def table_class @table_class end |
#tables ⇒ Object
Returns the value of attribute tables.
31 32 33 |
# File 'lib/simple_master/storage/dataset.rb', line 31 def tables @tables end |
Class Method Details
.after_load(&proc) ⇒ Object
16 17 18 |
# File 'lib/simple_master/storage/dataset.rb', line 16 def after_load(&proc) after_load_procs << proc end |
.after_load_procs ⇒ Object
12 13 14 |
# File 'lib/simple_master/storage/dataset.rb', line 12 def after_load_procs @after_load_procs ||= [] end |
.run_after_load ⇒ Object
20 21 22 |
# File 'lib/simple_master/storage/dataset.rb', line 20 def run_after_load after_load_procs.each(&:call) end |
Instance Method Details
#cache_delete(key) ⇒ Object
127 128 129 |
# File 'lib/simple_master/storage/dataset.rb', line 127 def cache_delete(key) cache.delete(key) end |
#cache_fetch(key) ⇒ Object
117 118 119 120 121 |
# File 'lib/simple_master/storage/dataset.rb', line 117 def cache_fetch(key) cache.fetch(key) do cache[key] = yield end end |
#cache_read(key) ⇒ Object
Cache helper for other data sources.
113 114 115 |
# File 'lib/simple_master/storage/dataset.rb', line 113 def cache_read(key) cache[key] end |
#cache_write(key, value) ⇒ Object
123 124 125 |
# File 'lib/simple_master/storage/dataset.rb', line 123 def cache_write(key, value) cache[key] = value end |
#duplicate(diff: nil) ⇒ Object
NOTE: Pass a empty hash to duplicate with empty diff.
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/simple_master/storage/dataset.rb', line 94 def duplicate(diff: nil) diff ||= @diff new_dataset = self.class.new(table_class: table_class, loader: loader) new_dataset.diff = diff.deep_dup tables.each do |klass, table| new_dataset.tables[klass] = table.duplicate_for(new_dataset) end new_dataset end |
#initialize_cache ⇒ Object
43 44 45 46 |
# File 'lib/simple_master/storage/dataset.rb', line 43 def initialize_cache self.tables = Hash.new { |hash, klass| hash[klass] = table_class.new(klass, self, loader) }.compare_by_identity self.cache = {} end |
#load ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/simple_master/storage/dataset.rb', line 60 def load memsize do cache.clear targets = @load_targets || SimpleMaster.targets tables = targets.map(&:base_class).uniq.map { table(_1) } timer("MasterData load") do tables.each(&:load_records) end timer("MasterData cache update") do SimpleMaster.use_dataset(self) do tables.each(&:update_class_method_cache) tables.each(&:tap_instance_methods) tables.each(&:freeze_all) end end timer("Cache update") do SimpleMaster.use_dataset(self) do self.class.run_after_load end end end self end |
#reload ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/simple_master/storage/dataset.rb', line 52 def reload if table_class <= SimpleMaster::Storage::OndemandTable unload else load end end |
#reload_klass(klass) ⇒ Object Also known as: reload_class
105 106 107 108 109 |
# File 'lib/simple_master/storage/dataset.rb', line 105 def reload_klass(klass) fail NotImplementedError unless table_class == SimpleMaster::Storage::OndemandTable tables.delete(klass) end |
#table(klass) ⇒ Object
48 49 50 |
# File 'lib/simple_master/storage/dataset.rb', line 48 def table(klass) @tables[klass] end |
#unload ⇒ Object
88 89 90 91 |
# File 'lib/simple_master/storage/dataset.rb', line 88 def unload cache.clear tables.clear end |