In SimpleMaster, the dataset holds the actual data, and each master class maps to a table. The loader reads external data, and the table keeps records and caches.
Dataset
├─ Table (Weapon)
├─ Table (Armor)
└─ Table (Level)
- Load each
Tablevialoader - Keep
cachefor class/instance caches - Provide diff overrides via
diff
loader = SimpleMaster::Loader::QueryLoader.new
dataset = SimpleMaster::Storage::Dataset.new(loader: loader)
dataset.load
SimpleMaster.use_dataset(dataset) do
# work with this dataset
endload: load all target tables and update cachesreload: reload or unload depending on table classunload: clear tables and cacheduplicate(diff: nil): duplicate a dataset (diff included)table(klass): fetch table for a class
You can layer changes on top of loader data.
Set dataset.diff to a JSON/Hash and the diff is applied after load.
Table.apply_diff updates id_hash and overrides records.
dataset = SimpleMaster::Storage::Dataset.new
dataset.diff = {
"weapons" => {
"1" => { "name" => "Updated Name" },
"2" => nil
}
}
dataset.loadProvides cache_read / cache_fetch / cache_write / cache_delete.
Use it for lightweight external caches. Data stays in memory, so mind the size.
- Hold record array (
all) - Build
id_hash/grouped_hash - Update class/instance caches
- Keep STI sub tables
all: array of recordsid_hash:id=> recordgrouped_hash:group_key=> grouped recordsclass_method_cache: results ofcache_class_methodmethod_cache: results ofcache_method
When a class uses STI, sub_table returns a table per subclass.
update_sub_tables extracts subclasses from all and registers them.
- Loads all records when the dataset loads
- Builds
all/id_hash/grouped_hashon load - Records are frozen, so Copy-on-Write works well
- Builds
all/id_hash/grouped_hashon first access - Useful for large data or on-demand access
dataset = SimpleMaster::Storage::Dataset.new(
table_class: SimpleMaster::Storage::OndemandTable
)- Lightweight table for tests
- Assumes
update/record_updateddiffs
dataset = SimpleMaster::Storage::Dataset.new(
table_class: SimpleMaster::Storage::TestTable
)A loader implements read_raw and build_records.
Besides QueryLoader and MarshalLoader, you can define your own.
class JsonLoader < SimpleMaster::Loader
FIXTURE_DIR = Rails.root.join("fixtures/masters")
def read_raw(table)
File.read(FIXTURE_DIR.join("#{table.klass.table_name}.json"))
end
def build_records(klass, raw)
JSON.parse(raw).map { |attrs| klass.new(attrs) }
end
end