Skip to content

Commit

Permalink
Add documentation comments to code
Browse files Browse the repository at this point in the history
  • Loading branch information
KludgeKML committed Aug 19, 2014
1 parent 00c85dc commit 23c503f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
4 changes: 2 additions & 2 deletions keyth.gemspec
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# encoding: utf-8
Gem::Specification.new do |s|
s.name = 'keyth'
s.version = '0.2.1'
s.date = '2014-08-17'
s.version = '0.2.2'
s.date = '2014-08-19'
s.summary = 'Keyth!'
s.description = 'Handles named keys for use in config files'
s.authors = ['K M Lawrence']
Expand Down
42 changes: 33 additions & 9 deletions lib/keyth.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
# Keyhandling module with various functions for keeping keys
# out of configuration files.
module Keyth
# Retrieves a key from the store, raising errors if the key is missing
# Params:
# +key+:: name of the key (without the keyth: prefix)
def self.get_key(key)
load_keyfile unless @key_list
application, key_name = key.split('/')
Expand All @@ -14,10 +17,17 @@ def self.get_key(key)
@key_list[application][key_name]
end

# Retrieves a key from the store, returning nil if the key is missing
# Params:
# +key+:: name of the key (without the keyth: prefix)
def self.get_key_safe(key)
get_key(key) rescue nil
end

# Adds a key to the store
# Params:
# +key+:: name of the key (without the keyth: prefix)
# +value+:: the key value
def self.add_key(key, value)
load_keyfile unless @key_list
application, key_name = key.split('/')
Expand All @@ -26,6 +36,9 @@ def self.add_key(key, value)
save_keyfile
end

# Removes a key from the store
# Params:
# +key+:: name of the key (without the keyth: prefix)
def self.delete_key(key)
load_keyfile unless @key_list
application, key_name = key.split('/')
Expand All @@ -34,6 +47,9 @@ def self.delete_key(key)
save_keyfile
end

# Gets a list of keys in the store
# Params:
# +application+:: if not nil, only return keys where the part of the key before the slash matches.
def self.keys(application = nil)
load_keyfile unless @key_list
keys = {}
Expand All @@ -45,12 +61,18 @@ def self.keys(application = nil)
keys
end

# Reads a YAML file, automatically retrieving keys for any value prefixed with "keyth:"
# Params:
# +file+:: file object containing YAML to read
def self.load_yaml(file)
load_keyfile unless @key_list
keys = YAML.pre_keyth_load(file)
fetch_keys(keys)
end

# Fixes a string, array-alike, or hash-alike by automatically retrieving keys for any value prefixed with "keyth:"
# Params:
# +obj+:: the object to fix
def self.fetch_keys(obj)
load_keyfile unless @key_list
case
Expand All @@ -63,7 +85,7 @@ def self.fetch_keys(obj)
obj[i] = fetch_keys(v)
end
when obj.is_a?(String)
obj = obj.gsub(/^keyth\:(.*)/) { get_key(Regexp.last_match[1]) }
obj = obj.gsub(/^keyth\:(.*)/) { get_key_safe(Regexp.last_match[1]) || "Missing Key: [#{obj}]" }
end
obj
end
Expand All @@ -78,11 +100,8 @@ def self.namespace(namespace)
@namespace
end

def self.keyfile_location
@namespace = 'default' unless @namespace
ENV['KEYTH_KEYFILE'] || File.join(Dir.home, '.keyth', @namespace + '.yml')
end

# Load the keyfile. By default, the keystore is loaded if necessary by
# the using functions, so it is unnecessary to call this directly.
def self.load_keyfile
if File.file?(keyfile_location)
@key_list = YAML.pre_keyth_load(File.open(keyfile_location))
Expand All @@ -91,12 +110,17 @@ def self.load_keyfile
end
end

# Save the keyfile. By default, the keystore is saved when changes are
# made to it, so it is unnecessary to call this directly.
def self.save_keyfile
load_keyfile unless @key_list
File.open(keyfile_location, 'w') { |f| f.write @key_list.to_yaml }
end
end



private

def self.keyfile_location
@namespace = 'default' unless @namespace
ENV['KEYTH_KEYFILE'] || File.join(Dir.home, '.keyth', @namespace + '.yml')
end
end
7 changes: 6 additions & 1 deletion lib/keyth/dotenv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module Dotenv
# The two apply functions are all that need to be overwritten
class Environment
# sets all environment variables that are keyth: links with the
# appropriate key value, sets all missing env variables otherwise
def apply
each do |k, v|
if v =~ /^keyth\:(.*)/
Expand All @@ -13,7 +15,10 @@ def apply
end
end

def apply
# sets all environment variables that are keyth: links with the
# appropriate key value, overwrites all env variables with the
# contents from .env otherwise
def apply!
each do |k, v|
if v =~ /^keyth\:(.*)/
ENV[k] = Keyth.get_key_safe(Regexp.last_match[1]) || ''
Expand Down
6 changes: 5 additions & 1 deletion lib/keyth/yaml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ class << self
alias_method(:pre_keyth_load, :load)
end

# loads the yaml file, then replaces all keyth: links with the
# appropriate values.
# Params:
# +file+:: file object containing YAML to read
def self.load(file)
Keyth.fetch_keys(pre_keyth_load(file))
end
end
end

0 comments on commit 23c503f

Please sign in to comment.