class MakeData::CLI
Public Class Methods
new(format: nil, ids: true, count: nil, shape: nil, dry: false)
click to toggle source
# File lib/make_data.rb, line 92 def initialize(format: nil, ids: true, count: nil, shape: nil, dry: false) @dry = dry @ids = ids @format = @dry ? :json : format @count = count @shape = shape end
Public Instance Methods
add_key(shape)
click to toggle source
# File lib/make_data.rb, line 164 def add_key(shape) key = get_key cat = get_category method = get_method(cat) shape[key] = [cat, method] shape end
choose_among(prompt, strings)
click to toggle source
# File lib/make_data.rb, line 131 def choose_among(prompt, strings) `echo "#{strings.join("\n")}" | peco --prompt="#{prompt}>"`.chomp end
get_category()
click to toggle source
# File lib/make_data.rb, line 135 def get_category prompt = "What faker category?" choose_among(prompt, FakerFinder.available_categories.map(&:to_s)) end
get_count()
click to toggle source
# File lib/make_data.rb, line 172 def get_count puts "How many records?" gets.chomp.to_i end
get_format()
click to toggle source
# File lib/make_data.rb, line 112 def get_format prompt = "What kind of data do you want to generate?" format = choose_among(prompt, ResultsFormatter.valid_formats) unless ResultsFormatter.valid_formats.include?(format) raise InvalidFormatError.new("File needs to be one of #{ResultsFormatter.valid_formats.join(', ')}") end format end
get_key()
click to toggle source
# File lib/make_data.rb, line 159 def get_key puts "What key do you want to add?" gets.chomp end
get_method(category)
click to toggle source
# File lib/make_data.rb, line 140 def get_method(category) prompt = "What method?" choose_among(prompt, FakerFinder.new(category).available_methods.map(&:to_s)) end
get_shape(shape = {})
click to toggle source
# File lib/make_data.rb, line 145 def get_shape(shape = {}) puts "---" print_results(shape, 'json') puts "\n---" action = choose_among("What do you want to do?", ["Add a key", "Done"]) case action when "Done" return shape when "Add a key" updated = add_key(shape) get_shape(updated) end end
print_results(results, format = @format)
click to toggle source
# File lib/make_data.rb, line 108 def print_results(results, format = @format) print ResultsFormatter.new(results, format).format_results end
puts_in_columns(strings)
click to toggle source
# File lib/make_data.rb, line 121 def puts_in_columns(strings) col_count = `tput cols`.chomp.to_i col_width = strings.max_by { |s| s.length }.length + 2 cols = col_count / col_width strings.each_slice(cols) do |row| row.each { |s| print s.to_s.ljust(col_width) } puts end end
run()
click to toggle source
# File lib/make_data.rb, line 100 def run @shape ||= get_shape @format ||= get_format @count ||= get_count unless @dry @results = @dry ? @shape : SampleGenerator.new(@shape, @ids).generate(@count) print_results(@results) end