Class: SecureConf::Config

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Config.



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

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

Instance Attribute Details

#auto_commitObject

Returns the value of attribute auto_commit.



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

def auto_commit
  @auto_commit
end

#encripterObject (readonly)

Returns the value of attribute encripter.



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

def encripter
  @encripter
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/secure_conf/config.rb', line 3

def path
  @path
end

#serializerObject (readonly)

Returns the value of attribute serializer.



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

def serializer
  @serializer
end

#storageObject (readonly)

Returns the value of attribute storage.



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

def storage
  @storage
end

Instance Method Details

#[](key) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/secure_conf/config.rb', line 36

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

#plain_getObject



34
# File 'lib/secure_conf/config.rb', line 34

alias_method :plain_get, :[]

#plain_storeObject



18
# File 'lib/secure_conf/config.rb', line 18

alias_method :plain_store, :store

#saveObject



45
46
47
# File 'lib/secure_conf/config.rb', line 45

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

#secure_store(key, value) ⇒ Object



29
30
31
32
# File 'lib/secure_conf/config.rb', line 29

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

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



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

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