Module: Trusty::Environment::ClassMethods
- Includes:
- Utilities::MethodNameExtensions
- Included in:
- Trusty::Environment
- Defined in:
- lib/trusty/environment.rb
Instance Method Summary collapse
- #[](key) ⇒ Object
- #config(default_value = nil) ⇒ Object
- #default_env_section ⇒ Object
-
#env(env_section = default_env_section) ⇒ Object
downcases keys to method names.
- #env_source(env_section = default_env_section) ⇒ Object
-
#load_constants!(options = {}) ⇒ Object
load env.yml into constants (e.g. Vars::DATABASE_URL).
-
#load_env!(options = {}) ⇒ Object
load env.yml into ENV (e.g. ENV).
-
#method_missing(name, *args, &block) ⇒ Object
dynamically add methods that forward to config.
- #paths ⇒ Object
Methods included from Utilities::MethodNameExtensions
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
dynamically add methods that forward to config
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/trusty/environment.rb', line 67 def method_missing(name, *args, &block) if !method_name_info(name).special? instance_variable_name = :"@#{name}" instance_variable_value = config[name] instance_variable_set instance_variable_name, instance_variable_value define_singleton_method name do instance_variable_get instance_variable_name end instance_variable_value else super end end |
Instance Method Details
#[](key) ⇒ Object
36 37 38 |
# File 'lib/trusty/environment.rb', line 36 def [](key) env[key.to_s.downcase] end |
#config(default_value = nil) ⇒ Object
55 56 57 58 59 60 |
# File 'lib/trusty/environment.rb', line 55 def config(default_value = nil) # loads YAML on-the-fly when a key doesn't exist @config ||= Hashie::Mash.new do |hash, key| hash[key] = methodize_hash load_yaml_file("#{key}.yml", default_value) end end |
#default_env_section ⇒ Object
51 52 53 |
# File 'lib/trusty/environment.rb', line 51 def default_env_section @default_env_section ||= ENV['ENV_SECTION'] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] end |
#env(env_section = default_env_section) ⇒ Object
downcases keys to method names
41 42 43 44 |
# File 'lib/trusty/environment.rb', line 41 def env(env_section = default_env_section) @env ||= {} @env[env_section] ||= methodize_hash env_source(env_section).merge(ENV.to_hash) end |
#env_source(env_section = default_env_section) ⇒ Object
46 47 48 49 |
# File 'lib/trusty/environment.rb', line 46 def env_source(env_section = default_env_section) @env_source ||= {} @env_source[env_section] ||= load_yaml_file("env.yml").fetch(env_section, {}) end |
#load_constants!(options = {}) ⇒ Object
load env.yml into constants (e.g. Vars::DATABASE_URL)
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/trusty/environment.rb', line 23 def load_constants!( = {}) source = load_yaml_file("env.yml") source = source.fetch([:env_section] || env_section, {}) source.each do |key, value| constant_name = key.to_s.upcase.to_sym if !constants.include(constant_name) || [:overwrite] == true constant_set constant_name, value.to_s end end end |
#load_env!(options = {}) ⇒ Object
load env.yml into ENV (e.g. ENV)
11 12 13 14 15 16 17 18 19 20 |
# File 'lib/trusty/environment.rb', line 11 def load_env!( = {}) source = load_yaml_file("env.yml") source = source.fetch([:env_section] || env_section, {}) source.each do |key, value| if !ENV.has_key?(key) || [:overwrite] == true ENV[key.to_s.upcase] = value.to_s end end end |
#paths ⇒ Object
62 63 64 |
# File 'lib/trusty/environment.rb', line 62 def paths @paths ||= defined?(::Rails) ? [ ::Rails.root.join("config").to_s ] : [] end |