Class: DotEnvDotExample

Inherits:
Object
  • Object
show all
Defined in:
lib/dot_example/dot_env_dot_example.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ DotEnvDotExample

Returns a new instance of DotEnvDotExample.



2
3
4
5
# File 'lib/dot_example/dot_env_dot_example.rb', line 2

def initialize( options = {} )
  @filename = options[:filename] || ".env.example"
  @dot_env = options[:dot_env] || DotEnv.new
end

Instance Attribute Details

#dot_envObject (readonly)

Returns the value of attribute dot_env.



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

def dot_env
  @dot_env
end

#filenameObject (readonly)

Returns the value of attribute filename.



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

def filename
  @filename
end

Instance Method Details

#keys_new_to_exampleObject



18
19
20
21
22
# File 'lib/dot_example/dot_env_dot_example.rb', line 18

def keys_new_to_example
  dot_env.keys.reject do |key|
    keys.map(&:chomp).include? key
  end
end

#missing_keysObject



32
33
34
# File 'lib/dot_example/dot_env_dot_example.rb', line 32

def missing_keys
  keys.reject { |key| dot_env.keys.include?(key) } 
end

#pre_commit_hookObject



46
47
48
49
50
51
52
53
# File 'lib/dot_example/dot_env_dot_example.rb', line 46

def pre_commit_hook
  # TODO: Make it possible to put .env and .env.example files somewhere else?
  # Maybe later if people actually want this feature
  [
    "dot_example sync",
    "git add .env.example"
  ].join("\n")
end


36
37
38
39
40
41
42
43
44
# File 'lib/dot_example/dot_env_dot_example.rb', line 36

def print_missing_keys_message
  if missing_keys.any?
    puts Paint[
      "missing ENV variables from #{dot_env.filename}", 
      :green
    ]
    puts missing_keys
  end
end


24
25
26
27
28
29
30
# File 'lib/dot_example/dot_env_dot_example.rb', line 24

def print_new_keys_message
  puts Paint[
    "New variables added to #{filename}",
    :green
  ]
  puts keys_new_to_example
end

#write!Object



9
10
11
12
13
14
15
16
# File 'lib/dot_example/dot_env_dot_example.rb', line 9

def write!
  if keys_new_to_example.any?
    print_new_keys_message
    File.open(filename, "a") do |file|
      file.puts keys_new_to_example.map { |key| "#{key}=" }
    end
  end
end