Class: RailsAppGenerator::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/rails_app_generator/util.rb

Overview

Utility methods

Class Method Summary collapse

Class Method Details

.bundler_environment(environment_style: :unbundled_env, &block) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/rails_app_generator/util.rb', line 51

def bundler_environment(environment_style: :unbundled_env, &block)
  case environment_style
  when :unbundled_env
    Bundler.with_unbundled_env(&block)
  when :original_env
    Bundler.with_original_env(&block)
  else
    block.call
  end
end

.kv(label, value, len = 35) ⇒ Object

Log KeyValue pair

Examples:

Util.kv(:key, :value)


11
12
13
14
15
16
17
18
# File 'lib/rails_app_generator/util.rb', line 11

def kv(label, value, len = 35)
  return ' ' * len if label.nil?

  label = label.to_s if label.is_a?(Symbol)
  label = label.length > len ? label.slice(0..len) : label.ljust(len, ' ')

  puts "#{label}: #{value}"
end

.line(heading, len = 80) ⇒ Object



20
21
22
23
# File 'lib/rails_app_generator/util.rb', line 20

def line(heading, len = 80)
  heading = "- [ #{heading} ]"
  puts heading.length > len ? heading : heading.ljust(len, '-')
end

.thor_options_to_hash(thor_options) ⇒ Object

Take array of Thor::Option and extract the data as a hash

You can find the array on the class_options reader



40
41
42
43
44
45
46
47
48
49
# File 'lib/rails_app_generator/util.rb', line 40

def thor_options_to_hash(thor_options)
  common_keys = thor_options.flat_map(&:instance_variables).uniq
  common_keys = common_keys.map { |k| k.to_s.delete_prefix('@').to_sym }

  thor_options.map do |option|
    common_keys.each_with_object({}) do |key, result|
      result[key] = option.send(key) if option.respond_to?(key)
    end
  end
end

.write_json(file, data) ⇒ Object



30
31
32
33
34
35
# File 'lib/rails_app_generator/util.rb', line 30

def write_json(file, data)
  file = File.expand_path(file)
  puts "Write to #{file}"
  FileUtils.mkdir_p(File.dirname(file))
  File.write(file, JSON.pretty_generate(data))
end

.write_last_run(file, data) ⇒ Object



25
26
27
28
# File 'lib/rails_app_generator/util.rb', line 25

def write_last_run(file, data)
  file = File.join('~/dev/kgems/rails_app_generator/docs/last_run/', file)
  write_json(file, data)
end