Module: Keyth
- Defined in:
- lib/keyth.rb
Overview
Keyhandling module with various functions for keeping keys out of configuration files.
Class Method Summary collapse
- .add_key(key, value) ⇒ Object
- .delete_key(key) ⇒ Object
- .fetch_keys(obj) ⇒ Object (also: apply_to)
- .get_key(key) ⇒ Object
- .get_key_safe(key) ⇒ Object
- .keyfile_location ⇒ Object
- .keys(application = nil) ⇒ Object
- .load_keyfile ⇒ Object
- .load_yaml(file) ⇒ Object
- .namespace(namespace) ⇒ Object
- .save_keyfile ⇒ Object
Class Method Details
.add_key(key, value) ⇒ Object
21 22 23 24 25 26 27 |
# File 'lib/keyth.rb', line 21 def self.add_key(key, value) load_keyfile unless @key_list application, key_name = key.split('/') @key_list[application] = {} unless @key_list.key?(application) @key_list[application][key_name] = value save_keyfile end |
.delete_key(key) ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/keyth.rb', line 29 def self.delete_key(key) load_keyfile unless @key_list application, key_name = key.split('/') @key_list[application].delete(key_name) @key_list.delete(application) if @key_list[application].empty? save_keyfile end |
.fetch_keys(obj) ⇒ Object Also known as: apply_to
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/keyth.rb', line 54 def self.fetch_keys(obj) load_keyfile unless @key_list case when obj.respond_to?(:keys) obj.each do |k, v| obj[k] = fetch_keys(v) end when obj.respond_to?(:each) obj.each_with_index do |v, i| obj[i] = fetch_keys(v) end when obj.is_a?(String) obj = obj.gsub(/^keyth\:(.*)/) { get_key(Regexp.last_match[1]) } end obj end |
.get_key(key) ⇒ Object
9 10 11 12 13 14 15 |
# File 'lib/keyth.rb', line 9 def self.get_key(key) load_keyfile unless @key_list application, key_name = key.split('/') fail "Application not found: #{application}!" unless @key_list[application] fail "Key not found: #{key}!" unless @key_list[application][key_name] @key_list[application][key_name] end |
.get_key_safe(key) ⇒ Object
17 18 19 |
# File 'lib/keyth.rb', line 17 def self.get_key_safe(key) get_key(key) rescue nil end |
.keyfile_location ⇒ Object
81 82 83 84 |
# File 'lib/keyth.rb', line 81 def self.keyfile_location @namespace = 'default' unless @namespace ENV['KEYTH_KEYFILE'] || File.join(Dir.home, '.keyth', @namespace + '.yml') end |
.keys(application = nil) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/keyth.rb', line 37 def self.keys(application = nil) load_keyfile unless @key_list keys = {} @key_list.each do |k1, v| v.each do |k2, v2| keys[k1 + '/' + k2] = v2 if k1 == application || application.nil? end end keys end |
.load_keyfile ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/keyth.rb', line 86 def self.load_keyfile if File.file?(keyfile_location) @key_list = YAML.pre_keyth_load(File.open(keyfile_location)) else @key_list = {} end end |
.load_yaml(file) ⇒ Object
48 49 50 51 52 |
# File 'lib/keyth.rb', line 48 def self.load_yaml(file) load_keyfile unless @key_list keys = YAML.pre_keyth_load(file) fetch_keys(keys) end |
.namespace(namespace) ⇒ Object
75 76 77 78 79 |
# File 'lib/keyth.rb', line 75 def self.namespace(namespace) @namespace = namespace @keylist = nil @namespace end |
.save_keyfile ⇒ Object
94 95 96 97 |
# File 'lib/keyth.rb', line 94 def self.save_keyfile load_keyfile unless @key_list File.open(keyfile_location, 'w') { |f| f.write @key_list.to_yaml } end |