Class: SpecFill::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/spec_fill/cli.rb

Instance Method Summary collapse

Instance Method Details

#aboutObject



63
64
65
66
67
# File 'lib/spec_fill/cli.rb', line 63

def about
  puts "\nSpecFill!"
  puts "version: " + SpecFill::VERSION
  puts "author: Publicover\n"
end

#fill(column, factory_name = '') ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/spec_fill/cli.rb', line 7

def fill(column, factory_name='')
  class_name = column.split('_').map(&:capitalize).join
  line_array = []
  next_line = false
  faker_terms = 'vendor/bundle/ruby/2.6.0/gems/spec_fill-0.1.0/lib/faker_terms.txt'
  skip_spaces = indent_length(faker_terms)

  File.foreach('db/schema.rb') do |line|
    break if line.chomp.empty? && next_line == true
    line_array << line if next_line
    if line.include?(column)
      line_array << line unless line.include?('add_index')
      next_line = true
    end
  end

  not_a_colum(line_array)

  File.open("spec/factories/#{column}.rb", 'w') do |file|
    unless factory_name.empty?
      file.print("FactoryGirl.define do\n\tfactory :#{factory_name}, class: #{class_name} do\n")
    else
      file.print("FactoryGirl.define do\n\tfactory :#{column} do\n")
    end

    reminder if line_array[0].include?('primary_key')

    line_array.shift
    get_attributes(line_array).each do |attr|
      finished = false
      File.foreach(faker_terms) do |line|
        if line.to_s.include?(attr)
          meth = line.split(',')
          file.print "\t\t#{attr}\t\t\tFaker::#{meth[1].chomp}.#{meth[0]}\n"
          finished = true
        end
      end
      if finished == false
        file.print "\t\t#{attr}\n"
      end
    end
    file.print("\tend\n")
    file.print('end')
  end
end

#get_attributes(ary) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/spec_fill/cli.rb', line 54

def get_attributes(ary)
  stripped_ary = []
  ary.each do |char|
    stripped_ary << char.scan(/"([^"]*)"/)
  end
  stripped_ary.flatten
end

#indent_length(schema) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/spec_fill/cli.rb', line 77

def indent_length(schema)
  num = 0
  File.foreach(schema) do |line|
    if line.to_i > num
      num = line.to_i
    end
  end
  num
end

#not_a_colum(ary) ⇒ Object



88
89
90
91
92
93
# File 'lib/spec_fill/cli.rb', line 88

def not_a_colum(ary)
  if ary.empty?
    puts 'That is not a column in your db'
    return false
  end
end

#reminderObject



70
71
72
73
74
# File 'lib/spec_fill/cli.rb', line 70

def reminder
  file.print("\t\t# It looks like your primary key isn't the record ID.\n")
  file.print("\t\t# Don't forget to pass in your primary key when you're creating\n")
  file.print("\t\t# the test object (e.g., create(:object, primary_key: column_name)).\n")
end