Class: SecureConf::Config

Inherits:
SimpleDelegator
  • Object
show all
Defined in:
lib/secure_conf/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, encrypter: nil, serializer: nil, storage: nil, auto_commit: false) ⇒ Config

Returns a new instance of Config.



11
12
13
14
15
16
17
18
# File 'lib/secure_conf/config.rb', line 11

def initialize(path, encrypter: nil, serializer: nil, storage: nil, auto_commit: false)
  @path = path
  @encrypter = encrypter || SecureConf.default
  @serializer = serializer || Serializer::Marshal
  @storage = storage || Storage.fetch(path)
  @auto_commit = auto_commit
  super(@storage.load(path))
end

Instance Attribute Details

#auto_commitObject

Returns the value of attribute auto_commit.



9
10
11
# File 'lib/secure_conf/config.rb', line 9

def auto_commit
  @auto_commit
end

#encrypterObject (readonly)

Returns the value of attribute encrypter.



6
7
8
# File 'lib/secure_conf/config.rb', line 6

def encrypter
  @encrypter
end

#pathObject (readonly)

Returns the value of attribute path.



5
6
7
# File 'lib/secure_conf/config.rb', line 5

def path
  @path
end

#serializerObject (readonly)

Returns the value of attribute serializer.



7
8
9
# File 'lib/secure_conf/config.rb', line 7

def serializer
  @serializer
end

#storageObject (readonly)

Returns the value of attribute storage.



8
9
10
# File 'lib/secure_conf/config.rb', line 8

def storage
  @storage
end

Instance Method Details

#[](key) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/secure_conf/config.rb', line 47

def [](key)
  value = plain_get(key)
  if value && key.to_s.start_with?("enc:")
    value = @encrypter.decrypt(value)
    value = @serializer.load(value)
  end
  value
end

#delete(key) ⇒ Object

delete



58
59
60
61
# File 'lib/secure_conf/config.rb', line 58

def delete(key)
  __getobj__.delete(key)
  save if @auto_commit
end

#plain_get(key) ⇒ Object

get



43
44
45
# File 'lib/secure_conf/config.rb', line 43

def plain_get(key)
  __getobj__[key]
end

#plain_store(key, value) ⇒ Object

store



22
23
24
25
# File 'lib/secure_conf/config.rb', line 22

def plain_store(key, value)
  __getobj__.store(key, value)
  save if @auto_commit
end

#saveObject

save



65
66
67
# File 'lib/secure_conf/config.rb', line 65

def save
  @storage.save(path, __getobj__)
end

#secure_store(key, value) ⇒ Object



27
28
29
30
# File 'lib/secure_conf/config.rb', line 27

def secure_store(key, value)
  value = @serializer.dump(value)
  plain_store(key, @encrypter.encrypt(value))
end

#store(key, value) ⇒ Object Also known as: []=



32
33
34
35
36
37
38
# File 'lib/secure_conf/config.rb', line 32

def store(key, value)
  if key.to_s.start_with?("enc:")
    secure_store(key, value)
  else
    plain_store(key, value)
  end
end