Skip to content

MaxPleaner/persisted_hash

Repository files navigation

This was originally written in gemmy but was extracted to a gem.

It's on rubygems. To install gem install persisted_hash and require 'persisted_hash' as expected.

There are three potential ways to create a 'persisted hash':

  1. via a global patch on hash
  2. via a scoped refinement on hash
  3. functionally, via class method

Globally:

Hash.include PersistedHash
hash = {}.persisted("path.yaml")

Refinements:

class MyClass
  using PersistedHash
  hash = {}.persisted("path.yaml")
end

Functionally:

hash = PersistedHash.new({})

The API to interact with the persisted hash is as follows:

  • get(*keys) reads
  • set(*keys) writes
  • data get all
  • set_state(hash) overwrite
  • clear empty

The persisted object inherits from hash, so methods like [] can still be called. They'll just work on the in-memory version, though.

About

Sync a hash's state with YAML file

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages