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

keychain[R]
project_name[R]
serializer[R]

Public Class Methods

new(project_name, keychain: Keychain.search(project_name), serializer: YAML) click to toggle source

@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

[](key) click to toggle source

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
[]=(key, value) click to toggle source

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
fetch(*args, &block) click to toggle source

@see Hash#fetch

# File lib/mellon/store.rb, line 42
def fetch(*args, &block)
  data.fetch(*args, &block)
end
to_h() click to toggle source

@return [Hash]

# File lib/mellon/store.rb, line 63
def to_h
  data
end

Private Instance Methods

data() click to toggle source
# File lib/mellon/store.rb, line 69
def data
  config = @keychain[@project_name]
  data = @serializer.load(config) if config
  data or {}
end
dump(hash) click to toggle source
# File lib/mellon/store.rb, line 75
def dump(hash)
  @keychain[@project_name] = @serializer.dump(hash)
end