-
-
Save gmodarelli/b095eaeb54dec548c8dc to your computer and use it in GitHub Desktop.
ESC #1: Dotenv
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| MY_SECRET_ID=jkdsa89jkldas8y9p21bjl | |
| MY_SECRET_TOKEN=jdskalhiop12jldaskd |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'dotenv' | |
| Dotenv.load |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def apply | |
| each { |k,v| ENV[k] = v } | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def apply | |
| each { |k,v| ENV[k] ||= v } | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| instrument('dotenv.load', :env => env) { env.apply } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | |
| ... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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