Module: Smash::CloudPowers::Zenv
Overview
This module provides some environment variable management and functionality System ENV, dotenv ENV and instance variables are considered for now but this will also use elasticache/redis...some other stuff too, in the coming weeks
Instance Method Summary collapse
-
#env_vars(key = '') ⇒ Object
Search through the .env variables for a key or if no key is given, return all the .env-vars and their values.
-
#i_vars(key = '') ⇒ Object
Search through the instance variables for a key or if no key is given, return all the i-vars and their values @params: [key <String]: The key to search for @return:.
-
#project_root ⇒ Object
PROJECT_ROOT should be set as early as possible in this Node's initilize method.
-
#project_root=(var) ⇒ Object
Manually set the
@project_rooti-var as aPathnameobject. -
#system_vars(key = '') ⇒ Object
Search through the system environment variables for a key or if no key is given, return all the system-env-vars and their values @params: [key
]: The key to search for @return: Value for the given key or if no key was given, a Hash with { key => value, ... } is returned for all keys with a value. -
#zfind(key) ⇒ Object
ZFind looks for the key in a preditermined order of importance: * i-vars are considered first becuase they might be tracking different locations for multiple tasks or something like that.
Methods included from Helper
#attr_map!, #called_from, #create_logger, #errors, #format_error_message, #log_file, #logger, #smart_retry, #task_path, #task_require_path, #to_camel, #to_i_var, #to_pascal, #to_ruby_file_name, #to_snake, #update_message_body
Methods included from Auth
Instance Method Details
#env_vars(key = '') ⇒ Object
Search through the .env variables for a key or if no key is given, return all the .env-vars and their values
42 43 44 45 |
# File 'lib/cloud_powers/zenv.rb', line 42 def env_vars(key = '') return ENV if key.empty? ENV[to_snake(key).upcase] end |
#i_vars(key = '') ⇒ Object
Search through the instance variables for a key or if no key is given, return all the i-vars and their values @params: [key <String]: The key to search for @return:
31 32 33 34 35 36 37 38 |
# File 'lib/cloud_powers/zenv.rb', line 31 def i_vars(key = '') if key.empty? return self.instance_variables.inject({}) do |r,v| r.tap { |h| h[to_snake(v)] = self.instance_variable_get(to_i_var(v)) } end end self.instance_variable_get(to_i_var(key)) end |
#project_root ⇒ Object
PROJECT_ROOT should be set as early as possible in this Node's initilize
method. This method tries to search for it, using #zfind() and if a nil
result is returned from that search, pwd is used as the PROJECT_ROOT.
@return: Path to the project root or where ever pwd resolves to
77 78 79 80 81 82 83 84 |
# File 'lib/cloud_powers/zenv.rb', line 77 def project_root if @project_root.nil? pr = (PROJECT_ROOT rescue nil) or zfind('PROJECT_ROOT') or system('pwd') @project_root = Pathname.new(pr) else @project_root end end |
#project_root=(var) ⇒ Object
Manually set the @project_root i-var as a Pathname object.
@param: New path to the project root <String|Pathname>
@return: @project_root
89 90 91 |
# File 'lib/cloud_powers/zenv.rb', line 89 def project_root=(var) @project_root = Pathname.new(var) end |
#system_vars(key = '') ⇒ Object
Search through the system environment variables for a key or if no key
is given, return all the system-env-vars and their values
@params: [key
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/cloud_powers/zenv.rb', line 53 def system_vars(key = '') if key.empty? # Separate key-value pairs from the large string received by `ENV` separate_pairs = `ENV`.split(/\n/).map do |string_pair| string_pair.split('=') end # Separate key-value pairs from each other into a hash of # { key => value, other_key => other_value } # * keys with no value are removed separate_pairs.inject({}) do |res, pair| res.tap { |h_res| h_res[pair.first] = pair.last unless (pair.first == pair.last) } end else res = `printf "#{to_snake(key).upcase}"` return res.empty? ? nil : res end end |
#zfind(key) ⇒ Object
ZFind looks for the key in a preditermined order of importance:
* i-vars are considered first becuase they might be tracking different
locations for multiple tasks or something like that.
* dotenv files are second because they were manually set, so for sure
it's important
* System Env[@] variables are up next. Hopefully by this time we've found
our information but if not, it should "search" through the system env too.
@params: key
20 21 22 23 24 25 |
# File 'lib/cloud_powers/zenv.rb', line 20 def zfind(key) res = (i_vars[to_snake(key).upcase] or env_vars[to_snake(key).upcase] unless @project_root.nil?) or system_vars[to_snake(key).upcase] (res.nil? or res.empty?) ? nil : res end |