Class: Divide::Extractor
- Inherits:
-
Object
- Object
- Divide::Extractor
- Defined in:
- lib/divide/extractor.rb
Constant Summary collapse
- DEFAULT_ENV =
{ 'PORT' => '5000' }.merge(ENV)
Instance Method Summary collapse
- #env_content ⇒ Object
- #env_variables ⇒ Object
- #escape_double_quotes ⇒ Object
- #extract_processes! ⇒ Object
-
#initialize(flags = [], options = {}) ⇒ Extractor
constructor
A new instance of Extractor.
- #overwrite_env_variables ⇒ Object
- #overwrite_flags ⇒ Object
- #procfile_content ⇒ Object
Constructor Details
#initialize(flags = [], options = {}) ⇒ Extractor
Returns a new instance of Extractor.
7 8 9 10 11 12 13 14 |
# File 'lib/divide/extractor.rb', line 7 def initialize(flags = [], = {}) @flags = flags @options = overwrite_env_variables overwrite_flags escape_double_quotes end |
Instance Method Details
#env_content ⇒ Object
20 21 22 |
# File 'lib/divide/extractor.rb', line 20 def env_content @env_content ||= File.read("#{@options[:from]}/.env") rescue '' end |
#env_variables ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/divide/extractor.rb', line 46 def env_variables @env_variables ||= begin env_content.split(/\n/).inject({}) do |memo, line| variable, value = line.split(/=/) next {} if variable == nil || variable.chars.first == '#' memo.merge variable => value end end end |
#escape_double_quotes ⇒ Object
37 38 39 |
# File 'lib/divide/extractor.rb', line 37 def escape_double_quotes procfile_content.gsub!('"', '\"') end |
#extract_processes! ⇒ Object
41 42 43 44 |
# File 'lib/divide/extractor.rb', line 41 def extract_processes! return nil if procfile_content.empty? YAML.load(procfile_content) end |
#overwrite_env_variables ⇒ Object
56 57 58 59 60 |
# File 'lib/divide/extractor.rb', line 56 def overwrite_env_variables DEFAULT_ENV.merge(env_variables).each do |variable, value| procfile_content.gsub!("$#{variable}", value) end end |
#overwrite_flags ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/divide/extractor.rb', line 24 def overwrite_flags @flags.each do |option| next if option.length < 2 key = option[0] value = option[1] procfile_content.scan(/\s?#{key}\s(\S+)/).each do |value_to_overwrite| procfile_content.gsub!(value_to_overwrite[0], value) end end end |
#procfile_content ⇒ Object
16 17 18 |
# File 'lib/divide/extractor.rb', line 16 def procfile_content @procfile_content ||= File.read("#{@options[:from]}/Procfile") rescue '' end |