Class: ENVied::EnvVarExtractor
- Inherits:
-
Object
- Object
- ENVied::EnvVarExtractor
- Defined in:
- lib/envied/env_var_extractor.rb
Instance Attribute Summary collapse
-
#extensions ⇒ Object
readonly
Returns the value of attribute extensions.
-
#globs ⇒ Object
readonly
Returns the value of attribute globs.
Class Method Summary collapse
Instance Method Summary collapse
-
#capture_variables(line) ⇒ Array<String>
Greps all ENV-variables from a line of text.
- #defaults ⇒ Object
-
#extract(globs = self.globs) ⇒ <Hash{String => Array<String => Array>}>
Extract all keys recursively from files found via ‘globs`.
-
#initialize(**options) ⇒ EnvVarExtractor
constructor
A new instance of EnvVarExtractor.
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(**) @globs = .fetch(:globs, self.defaults[:globs]) @extensions = .fetch(:extensions, self.defaults[:extensions]) end |
Instance Attribute Details
#extensions ⇒ Object (readonly)
Returns the value of attribute extensions.
16 17 18 |
# File 'lib/envied/env_var_extractor.rb', line 16 def extensions @extensions end |
#globs ⇒ Object (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
.defaults ⇒ Object
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, **) new(.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’)‘.
37 38 39 |
# File 'lib/envied/env_var_extractor.rb', line 37 def capture_variables(line) line.scan(/ENV(?:\[|\.fetch\()['"]([^'"]+)['"]/).flatten end |
#defaults ⇒ Object
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.
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 |