Class: Mellon::Store

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

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_name) ⇒ Store #initialize(project_name, keychain_name) ⇒ Store #initialize(project_name, keychain) ⇒ Store

Returns a new instance of Store.

Examples:

use keychain where entry exists, or default keychain

Store.new("myapp")

automatically find keychain

Store.new("myapp", "shared_keychain")

explicitly use keychain

Store.new("myapp", Mellon::Keychain.new("/path/to/keychain.keychain"))

Parameters:

  • project_name (String)
  • keychain (String, Keychain, nil) (defaults to: Keychain.search(project_name))
  • serializer (#dump, #load) (defaults to: YAML)


29
30
31
32
33
34
35
36
37
38
39
# 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

Instance Attribute Details

#keychainObject (readonly)

Returns the value of attribute keychain.



10
11
12
# File 'lib/mellon/store.rb', line 10

def keychain
  @keychain
end

#project_nameObject (readonly)

Returns the value of attribute project_name.



9
10
11
# File 'lib/mellon/store.rb', line 9

def project_name
  @project_name
end

#serializerObject (readonly)

Returns the value of attribute serializer.



11
12
13
# File 'lib/mellon/store.rb', line 11

def serializer
  @serializer
end

Instance Method Details

#[](key) ⇒ String?

Retrieve a key from the store.

Parameters:

  • key (String)

Returns:

  • (String, nil)

    value stored, or nil



50
51
52
# File 'lib/mellon/store.rb', line 50

def [](key)
  data[key]
end

#[]=(key, value) ⇒ Object

Set a key in the store.

Parameters:

  • key (String)
  • value (String)


58
59
60
# File 'lib/mellon/store.rb', line 58

def []=(key, value)
  dump data.merge(key => value)
end

#fetch(*args, &block) ⇒ Object

See Also:

  • Hash#fetch


42
43
44
# File 'lib/mellon/store.rb', line 42

def fetch(*args, &block)
  data.fetch(*args, &block)
end

#to_hHash

Returns:

  • (Hash)


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

def to_h
  data
end