Class: MakeData::CLI

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

Defined Under Namespace

Classes: InvalidFormatError

Instance Method Summary collapse

Constructor Details

#initialize(format: nil, ids: true, count: nil, shape: nil, dry: false) ⇒ CLI

Returns a new instance of CLI.



92
93
94
95
96
97
98
# 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

Instance Method Details

#add_key(shape) ⇒ Object



164
165
166
167
168
169
170
# 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) ⇒ Object



131
132
133
# File 'lib/make_data.rb', line 131

def choose_among(prompt, strings)
  `echo "#{strings.join("\n")}" | peco --prompt="#{prompt}>"`.chomp
end

#get_categoryObject



135
136
137
138
# 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_countObject



172
173
174
175
# File 'lib/make_data.rb', line 172

def get_count
  puts "How many records?"
  gets.chomp.to_i
end

#get_formatObject



112
113
114
115
116
117
118
119
# 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_keyObject



159
160
161
162
# File 'lib/make_data.rb', line 159

def get_key
  puts "What key do you want to add?"
  gets.chomp
end

#get_method(category) ⇒ Object



140
141
142
143
# 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 = {}) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
# 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


108
109
110
# 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) ⇒ Object



121
122
123
124
125
126
127
128
129
# 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

#runObject



100
101
102
103
104
105
106
# 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