Class: Hako::EnvProviders::File

Inherits:
Hako::EnvProvider show all
Defined in:
lib/hako/env_providers/file.rb

Instance Method Summary collapse

Methods inherited from Hako::EnvProvider

#validation_error!

Constructor Details

#initialize(root_path, options) ⇒ File

Returns a new instance of File.

Parameters:

  • root_path (Pathname)
  • options (Hash<String, Object>)


10
11
12
13
14
15
# File 'lib/hako/env_providers/file.rb', line 10

def initialize(root_path, options)
  unless options['path']
    validation_error!('path must be set')
  end
  @path = root_path.join(options['path'])
end

Instance Method Details

#ask(variables) ⇒ Hash<String, String>

Parameters:

  • variables (Array<String>)

Returns:

  • (Hash<String, String>)


19
20
21
22
23
24
25
26
27
# File 'lib/hako/env_providers/file.rb', line 19

def ask(variables)
  env = {}
  read_from_file do |key, val|
    if variables.include?(key)
      env[key] = val
    end
  end
  env
end

#ask_keys(variables) ⇒ Array<String>

Parameters:

  • variables (Array<String>)

Returns:

  • (Array<String>)


36
37
38
39
40
41
42
43
44
# File 'lib/hako/env_providers/file.rb', line 36

def ask_keys(variables)
  keys = []
  read_from_file do |key, _|
    if variables.include?(key)
      keys << key
    end
  end
  keys
end

#can_ask_keys?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/hako/env_providers/file.rb', line 30

def can_ask_keys?
  true
end

#read_from_file {|key, val| ... } ⇒ nil (private)

Yield Parameters:

  • key (String)
  • val (String)

Returns:

  • (nil)


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/hako/env_providers/file.rb', line 51

def read_from_file(&block)
  ::File.open(@path) do |f|
    f.each_line do |line|
      line.chomp!
      line.lstrip!
      if line[0] == '#'
        # line comment
        next
      end

      key, val = line.split('=', 2)
      if val
        block.call(key, val)
      end
    end
  end
end