class Mellon::Store
Used for storing multiple values in a single Keychain
entry.
This is useful for configuring applications, e.g. having one entry per application, where each entry contains all configuration for said application.
Attributes
Public Class Methods
@example use keychain where entry exists, or default keychain
Store.new("myapp")
@example automatically find keychain
Store.new("myapp", "shared_keychain")
@example explicitly use keychain
Store.new("myapp", Mellon::Keychain.new("/path/to/keychain.keychain"))
@overload initialize(project_name
) @overload initialize(project_name
, keychain_name) @overload initialize(project_name
, keychain)
@param [String] project_name
@param [String, Keychain
, nil] keychain @param [#dump, load] serializer
# File lib/mellon/store.rb, line 29 def initialize(project_name, keychain: Keychain.search(project_name), serializer: YAML) @project_name = project_name.to_s @keychain = if keychain.is_a?(Keychain) keychain elsif keychain.nil? Keychain.default else Keychain.find(keychain.to_s) end @serializer = serializer end
Public Instance Methods
Retrieve a key from the store.
@param [String] key @return [String, nil] value stored, or nil
# File lib/mellon/store.rb, line 50 def [](key) data[key] end
Set a key in the store.
@param [String] key @param [String] value
# File lib/mellon/store.rb, line 58 def []=(key, value) dump data.merge(key => value) end
@see Hash#fetch
# File lib/mellon/store.rb, line 42 def fetch(*args, &block) data.fetch(*args, &block) end
@return [Hash]
# File lib/mellon/store.rb, line 63 def to_h data end
Private Instance Methods
# File lib/mellon/store.rb, line 69 def data config = @keychain[@project_name] data = @serializer.load(config) if config data or {} end
# File lib/mellon/store.rb, line 75 def dump(hash) @keychain[@project_name] = @serializer.dump(hash) end