Skip to content

Instantly share code, notes, and snippets.

@gmodarelli
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gmodarelli/b095eaeb54dec548c8dc to your computer and use it in GitHub Desktop.
Save gmodarelli/b095eaeb54dec548c8dc to your computer and use it in GitHub Desktop.
ESC #1: Dotenv
MY_SECRET_ID=jkdsa89jkldas8y9p21bjl
MY_SECRET_TOKEN=jdskalhiop12jldaskd
require 'dotenv'
Dotenv.load
def apply
each { |k,v| ENV[k] = v }
end
def apply
each { |k,v| ENV[k] ||= v }
end
module Dotenv
extend self
attr_accessor :instrumenter
def load(*filenames)
with(*filenames) do |f|
if File.exist?(f)
env = Environment.new(f)
instrument('dotenv.load', :env => env) { env.apply }
end
end
end
...
end
module Dotenv
extend self
attr_accessor :instrumenter
def load(*filenames)
with(*filenames) do |f|
if File.exist?(f)
env = Environment.new(f)
instrument('dotenv.load', :env => env) { env.apply }
end
end
end
...
def with(*filenames, &block)
filenames << '.env' if filenames.empty?
{}.tap do |hash|
filenames.each do |filename|
hash.merge! block.call(File.expand_path(filename)) || {}
end
end
end
...
end
module Dotenv
extend self
attr_accessor :instrumenter
def load(*filenames)
with(*filenames) do |f|
if File.exist?(f)
env = Environment.new(f)
instrument('dotenv.load', :env => env) { env.apply }
end
end
end
...
def instrument(name, payload = {}, &block)
if instrumenter
instrumenter.instrument(name, payload, &block)
else
block.call
end
end
end
module Dotenv
class Environment < Hash
attr_reader :filename
def initialize(filename)
@filename = filename
load
end
def load
update Parser.call(read)
end
def read
File.read(@filename)
end
def apply
each { |k,v| ENV[k] ||= v }
end
def apply!
each { |k,v| ENV[k] = v }
end
end
end
module Utils
def something_useful string
string.upcase
end
end
class StringUtils
extend Utils
end
StringUtils.respond_to? :something_useful #=> true
StringUtils.something_useful 'mystring' #=> 'MYSTRING'
instrument('dotenv.load', :env => env) { env.apply }
require 'dotenv'
Dotenv.instrumenter = ActiveSupport::Notifications
# Watch all loaded env files with Spring
begin
require 'spring/watcher'
ActiveSupport::Notifications.subscribe(/^dotenv/) do |*args|
event = ActiveSupport::Notifications::Event.new(*args)
Spring.watch event.payload[:env].filename if Rails.application
end
rescue LoadError
# Spring is not available
end
...
module Dotenv
class Railtie < Rails::Railtie
config.before_configuration { load }
# Public: Load dotenv
#
# This will get called during the `before_configuration` callback, but you
# can manually call `Dotenv::Railtie.load` if you needed it sooner.
def load
Dotenv.load(
root.join(".env.local"),
root.join(".env.#{Rails.env}"),
root.join('.env')
)
end
# Internal: `Rails.root` is nil in Rails 4.1 before the application is
# initialized, so this falls back to the `RAILS_ROOT` environment variable,
# or the current working directory.
def root
Rails.root || Pathname.new(ENV["RAILS_ROOT"] || Dir.pwd)
end
# Rails uses `#method_missing` to delegate all class methods to the
# instance, which means `Kernel#load` gets called here. We don't want that.
def self.load
instance.load
end
end
end
def invite_people *people
people.each do |person|
send_invitation_to person
end
people.class #=> Array
people.inspect #=> [ "Andrea", "Luca", "Pawel" ]
end
invite_people "Andrea", "Luca", "Pawel"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment