Module: Jets::CLI::Group::Actions

Extended by:
Memoist
Included in:
Base
Defined in:
lib/jets/cli/group/actions.rb

Instance Method Summary collapse

Instance Method Details

#comment_out_line(string, options = {}) ⇒ Object



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

def comment_out_line(string, options = {})
  file = if options[:env]
    "config/environments/#{options[:env]}.rb"
  else
    "config/application.rb"
  end
  lines = IO.readlines(file).map do |l|
    if l.include?(string) && !l.strip.start_with?("#")
      "  # #{l.strip}"
    else
      l
    end
  end
  IO.write(file, lines.join)
end

#config_environment(data, options = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/jets/cli/group/actions.rb', line 5

def config_environment(data, options = {})
  config_file = if options[:env]
    "config/environments/#{options[:env]}.rb"
  else
    "config/application.rb"
  end
  lines = IO.readlines(config_file)
  # remove comment lines
  lines.reject! { |line| line =~ /^\s*#/ }
  found = lines.any? { |line| line.include?(data) }
  environment(data, options) unless found
end

#environment(data = nil, options = {}) ⇒ Object Also known as: application



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/jets/cli/group/actions.rb', line 18

def environment(data = nil, options = {})
  sentinel = "class Application < Rails::Application\n"
  env_file_sentinel = "Rails.application.configure do\n"
  data ||= yield if block_given?

  in_root do
    if options[:env].nil?
      inject_into_file "config/application.rb", optimize_indentation(data, 4), after: sentinel, verbose: true
    else
      Array(options[:env]).each do |env|
        inject_into_file "config/environments/#{env}.rb", optimize_indentation(data, 2), after: env_file_sentinel, verbose: true
      end
    end
  end
end

#optimize_indentation(value, amount = 0) ⇒ Object Also known as: rebase_indentation

:doc:



35
36
37
38
# File 'lib/jets/cli/group/actions.rb', line 35

def optimize_indentation(value, amount = 0) # :doc:
  return "#{value}\n" unless value.is_a?(String)
  "#{value.strip_heredoc.indent(amount).chomp}\n"
end