Module: Ymlenv

Defined in:
lib/ymlenv.rb,
lib/ymlenv/version.rb

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.convert_bodyObject



11
12
13
14
15
16
# File 'lib/ymlenv.rb', line 11

def convert_body
  @options[:body] = File.read(@options[:file]) if define_file?
  ENV.each_pair do |ek, ev|
    @options[:body] = @options[:body].gsub(/\$[{]?#{ek}[}]?/, ev)
  end
end

.debugObject



69
70
71
# File 'lib/ymlenv.rb', line 69

def debug
  @options[:debug]
end

.define_file?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/ymlenv.rb', line 57

def define_file?
  !@options[:file].nil?
end

.enable_symborizeObject



65
66
67
# File 'lib/ymlenv.rb', line 65

def enable_symborize
  @options[:symborize]
end

.exec_with_formatObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ymlenv.rb', line 41

def exec_with_format
  convert_body

  if @options[:json]
    require 'json'
    load_yaml.to_json
  else
    if write_to_file?
      File.write @options[:file], @options[:body]
      "You yaml file #{@options[:file]} has been overwritten."
    else
      @options[:body]
    end
  end
end

.load_yamlObject



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

def load_yaml
  require "yaml"
  symbolize_all_keys YAML.load(@options[:body])
end

.optionsObject



7
8
9
# File 'lib/ymlenv.rb', line 7

def options
  @options
end

.symbolize_all_keys(obj) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ymlenv.rb', line 23

def symbolize_all_keys(obj)
  return obj unless enable_symborize
  case obj.class.to_s
  when 'Hash'
    obj.keys.each do |key|
      obj[(key.to_sym rescue key) || key] = symbolize_all_keys(obj.delete(key))
    end
    obj
  when 'Array'
    obj.map! do |elem|
      symbolize_all_keys elem
    end
    obj
  else
    obj
  end
end

.write_to_file?Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/ymlenv.rb', line 61

def write_to_file?
  define_file? && @options[:overwrite]
end