Module: Environ
- Defined in:
- lib/environ.rb,
lib/environ/version.rb
Overview
Public: Main handler for accessing variables
Constant Summary collapse
- VERSION =
"1.0.2"
Class Method Summary collapse
-
.all ⇒ Object
Public: Returns all environment variables as hash.
-
.create_method(name, val) ⇒ Object
Public: Dynamically defines a singleton method.
-
.create_methods ⇒ Object
Public: Dynamically defines singleton methods from ENV.
- .method_missing(method, *args, &block) ⇒ Object
-
.no_conflict ⇒ Object
Public: Returns boolean indicating whether no conflict prefixing is on or off.
-
.no_conflict=(value) ⇒ Object
Public: Sets boolean indicating whether no conflict prefixing is on or off If set to false, env vars can be accessed without the ‘env_’ prefix Examples.
-
.reset! ⇒ Object
Public: Resets all environment variables back to their original values.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object (private)
57 58 59 60 61 62 63 64 |
# File 'lib/environ.rb', line 57 def method_missing(method, *args, &block) if (method.to_s =~ /env_/) create_method(method.to_s, nil) nil else super.method_missing(method, *args, &block) end end |
Class Method Details
.all ⇒ Object
Public: Returns all environment variables as hash
Examples
Environ.all => {"PATH"=>"/Users/someone/.rvm/gems/ruby-2.2.0/bin", "RUBY_VERSION"=>"ruby-2.2.0", "_"=>"/Users/someone/.rvm/rubies/ruby-2.2.0/bin/irb"}
returns hash of all variables
41 42 43 |
# File 'lib/environ.rb', line 41 def all ENV end |
.create_method(name, val) ⇒ Object
Public: Dynamically defines a singleton method
Examples
Environ.create_method('path', 'something')
returns nothing
86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/environ.rb', line 86 def create_method(name, val) var_prefix = @_no_conflict ? 'env_' : '' var_name = "#{var_prefix}#{name.strip.downcase}" @_data[var_name] = val define_singleton_method(var_name) do @_data[var_name] end define_singleton_method(:"#{var_name}=") do |value| @_data[var_name] = value end end |
.create_methods ⇒ Object
Public: Dynamically defines singleton methods from ENV
Examples
Environ.create_methods
returns nothing
73 74 75 76 77 |
# File 'lib/environ.rb', line 73 def create_methods ENV.each do |key, val| create_method(key, val) end end |
.method_missing(method, *args, &block) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/environ.rb', line 57 def method_missing(method, *args, &block) if (method.to_s =~ /env_/) create_method(method.to_s, nil) nil else super.method_missing(method, *args, &block) end end |
.no_conflict ⇒ Object
Public: Returns boolean indicating whether no conflict prefixing is on or off
Examples
Environ.no_conflict => true
returns boolean
17 18 19 |
# File 'lib/environ.rb', line 17 def no_conflict @_no_conflict end |
.no_conflict=(value) ⇒ Object
Public: Sets boolean indicating whether no conflict prefixing is on or off If set to false, env vars can be accessed without the ‘env_’ prefix Examples
Environ.no_conflict = false
Environ.path => {"PATH"=>"/Users/someone/.rvm/gems/ruby-2.2.0/bin", "RUBY_VERSION"=>"ruby-2.2.0", "_"=>"/Users/someone/.rvm/rubies/ruby-2.2.0/bin/irb"}
returns nothing
29 30 31 32 |
# File 'lib/environ.rb', line 29 def no_conflict=(value) @_no_conflict = value create_methods unless @_no_conflict end |
.reset! ⇒ Object
Public: Resets all environment variables back to their original values
Examples
Environ.reset!
returns nothing
52 53 54 55 |
# File 'lib/environ.rb', line 52 def reset! @_data.clear create_methods end |