Class: ENVied::EnvVarExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/envied/env_var_extractor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ EnvVarExtractor

Returns a new instance of EnvVarExtractor.



18
19
20
21
# File 'lib/envied/env_var_extractor.rb', line 18

def initialize(**options)
  @globs = options.fetch(:globs, self.defaults[:globs])
  @extensions = options.fetch(:extensions, self.defaults[:extensions])
end

Instance Attribute Details

#extensionsObject (readonly)

Returns the value of attribute extensions.



16
17
18
# File 'lib/envied/env_var_extractor.rb', line 16

def extensions
  @extensions
end

#globsObject (readonly)

Returns the value of attribute globs.



16
17
18
# File 'lib/envied/env_var_extractor.rb', line 16

def globs
  @globs
end

Class Method Details

.defaultsObject



3
4
5
6
7
8
9
10
# File 'lib/envied/env_var_extractor.rb', line 3

def self.defaults
  @defaults ||= begin
    {
      extensions: %w(ru thor rake rb yml ruby yaml erb builder markerb haml),
      globs: %w(*.* Thorfile Rakefile {app,config,db,lib,script}/*)
    }
  end
end

.extract_from(globs, **options) ⇒ Object



23
24
25
# File 'lib/envied/env_var_extractor.rb', line 23

def self.extract_from(globs, **options)
  new(options.merge(globs: Array(globs))).extract
end

Instance Method Details

#capture_variables(line) ⇒ Array<String>

Greps all ENV-variables from a line of text. Captures ‘A’ in lines like ‘ENV`, but also `ENV.fetch(’A’)‘.

Examples:

extractor.new.capture_variables("config.force_ssl = ENV['FORCE_SSL']")
# => ["FORCE_SSL"]

Parameters:

  • line (String)

    the line to grep

Returns:

  • (Array<String>)

    the names o



37
38
39
# File 'lib/envied/env_var_extractor.rb', line 37

def capture_variables(line)
  line.scan(/ENV(?:\[|\.fetch\()['"]([^'"]+)['"]/).flatten
end

#defaultsObject



12
13
14
# File 'lib/envied/env_var_extractor.rb', line 12

def defaults
  self.class.defaults
end

#extract(globs = self.globs) ⇒ <Hash{String => Array<String => Array>}>

Extract all keys recursively from files found via ‘globs`. Any occurrence of `ENV` or `ENV.fetch(’A’)‘, will result in ’A’ being extracted.

Examples:

EnvVarExtractor.new.extract(*%w(app lib))
# => {'A' => [{:path => 'app/models/user.rb', :line => 2}, {:path => ..., :line => ...}],
      'B' => [{:path => 'config/application.rb', :line => 12}]}

Parameters:

  • globs (Array<String>) (defaults to: self.globs)

    the collection of globs

Returns:

  • (<Hash{String => Array<String => Array>}>)

    the list of items.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/envied/env_var_extractor.rb', line 53

def extract(globs = self.globs)
  results = Hash.new { |hash, key| hash[key] = [] }

  Array(globs).each do |glob|
    Dir.glob(glob).each do |item|
      next if File.basename(item)[0] == ?.

      if File.directory?(item)
        results.merge!(extract("#{item}/*"))
      else
        next unless extensions.detect {|ext| File.extname(item)[ext] }
        File.readlines(item).each_with_index do |line, ix|
          capture_variables(line).each do |variable|
            results[variable] << { :path => item, :line => ix.succ }
          end
        end
      end
    end
  end

  results
end