Class: MittensUi::Store

Inherits:
Object
  • Object
show all
Defined in:
lib/mittens_ui/store.rb

Overview

A simple persistent key-value store backed by a JSON file. Data is saved to ~/.local/share/mittens_ui/<app_name>.json and automatically reloaded on the next instantiation.

Examples:

Basic usage

store = MittensUi::Store.new("my_app")
store.set(:theme, "dark")
store.get(:theme)  # => "dark"

Using a default value

store.get(:missing_key, "default")  # => "default"

Accessing via Application

MittensUi::Application.store.set(:window_width, 570)
MittensUi::Application.store.get(:window_width)  # => 570

Instance Method Summary collapse

Constructor Details

#initialize(app_name) ⇒ Store

Creates a new store for the given app name. If a store file already exists for this app, it will be loaded automatically.

Examples:

store = MittensUi::Store.new("contacts")

Parameters:

  • app_name (String)

    the name of the app, used as the JSON filename



28
29
30
31
32
33
# File 'lib/mittens_ui/store.rb', line 28

def initialize(app_name)
  @app_name = app_name
  @path = File.join(data_dir, "#{app_name}.json")
  FileUtils.mkdir_p(data_dir)
  @data = load
end

Instance Method Details

#allHash

Returns all stored key-value pairs with symbol keys.

Examples:

store.set(:theme, "dark")
store.set(:width, 570)
store.all  # => { theme: "dark", width: 570 }

Returns:

  • (Hash)

    all key-value pairs with symbol keys



89
90
91
# File 'lib/mittens_ui/store.rb', line 89

def all
  @data.transform_keys(&:to_sym)
end

#clearvoid

This method returns an undefined value.

Removes all data from the store and persists the change to disk.

Examples:

store.set(:theme, "dark")
store.clear
store.all  # => {}


100
101
102
103
# File 'lib/mittens_ui/store.rb', line 100

def clear
  @data = {}
  persist
end

#delete(key) ⇒ Object?

Deletes a key from the store and persists the change to disk. Returns nil if the key does not exist.

Examples:

store.set(:theme, "dark")
store.delete(:theme)  # => "dark"
store.get(:theme)     # => nil

Parameters:

  • key (Symbol, String)

    the key to delete

Returns:

  • (Object, nil)

    the deleted value, or nil if the key was not found



76
77
78
79
80
# File 'lib/mittens_ui/store.rb', line 76

def delete(key)
  value = @data.delete(key.to_s)
  persist
  value
end

#get(key, default = nil) ⇒ Object?

Retrieves a value by key. Returns the default value if the key does not exist. Symbol and string keys are treated as equivalent.

Examples:

store.get(:theme)               # => "dark"
store.get(:missing)             # => nil
store.get(:missing, "default")  # => "default"

Parameters:

  • key (Symbol, String)

    the key to look up

  • default (Object) (defaults to: nil)

    value to return if the key is not found (default: nil)

Returns:

  • (Object, nil)

    the stored value, or the default if not found



63
64
65
# File 'lib/mittens_ui/store.rb', line 63

def get(key, default = nil)
  @data.fetch(key.to_s, default)
end

#include?(key) ⇒ Boolean

Checks whether a key exists in the store. Symbol and string keys are treated as equivalent.

Examples:

store.set(:theme, "dark")
store.include?(:theme)    # => true
store.include?(:missing)  # => false

Parameters:

  • key (Symbol, String)

    the key to check

Returns:

  • (Boolean)

    true if the key exists, false otherwise



114
115
116
# File 'lib/mittens_ui/store.rb', line 114

def include?(key)
  @data.key?(key.to_s)
end

#set(key, value) ⇒ Object

Stores a value under the given key and persists it to disk. Symbol and string keys are treated as equivalent. Values must be JSON serializable (String, Integer, Float, Boolean, Array, Hash).

Examples:

store.set(:theme, "dark")
store.set(:window_width, 570)
store.set(:recent_files, ["a.txt", "b.txt"])

Parameters:

  • key (Symbol, String)

    the key to store under

  • value (Object)

    the value to store

Returns:

  • (Object)

    the value that was stored



46
47
48
49
50
# File 'lib/mittens_ui/store.rb', line 46

def set(key, value)
  @data[key.to_s] = value
  persist
  value
end