class CSVToSeed

Attributes

file[RW]
headers[RW]
name_of_array[RW]

Public Class Methods

new(args) click to toggle source
# File lib/csv_to_seed.rb, line 6
def initialize(args)
  @csv_path_to_file = args[:csv_path_to_file]
  @name_of_array = args[:name_of_array]
  validate_name_of_array(@name_of_array)
end

Public Instance Methods

csv_object() click to toggle source
# File lib/csv_to_seed.rb, line 21
def csv_object
  CSV.new(get_csv_body.to_s, {:headers => true, :header_converters => :symbol, :converters => :all})
end
csv_to_hash() click to toggle source
# File lib/csv_to_seed.rb, line 25
def csv_to_hash

  @headers = ''
  fix_csv = csv_object.to_a.map {|row|
    @headers = row.headers
    row.to_hash
  }

  replace_and_fix_csv_string fix_csv.to_s
end
get_csv_body() click to toggle source
# File lib/csv_to_seed.rb, line 12
def get_csv_body
  begin
    @file = File.open(@csv_path_to_file, "rb")
    @file.read
  rescue
    raise 'Invalid file path'
  end
end
replace_and_fix_csv_string(fix_me) click to toggle source
# File lib/csv_to_seed.rb, line 36
def replace_and_fix_csv_string(fix_me)
  fix_me
    .gsub(/\}\,/, "},\n\r")
    .gsub(/\[\{/, "[\n\r{")
    .gsub(/\}\]/, "}\n\r]")
    .gsub(/\{\:/, "{\s:")
    .gsub(/\}\,/, "\s},")
end
set_string_to_create_loop() click to toggle source
# File lib/csv_to_seed.rb, line 49
  def set_string_to_create_loop
    if @headers.empty?
      csv_to_hash
    end
    inside_vars = ''
    @headers.each do |header|
      inside_vars = inside_vars + 
    <<-TEXT
        support_practice.#{header.to_s} = attributes[:#{header.to_s}]
    TEXT
    end
    <<-TEXT
    #{@name_of_array}.each do |attributes|
      Static::SupportPracticeLu.find_or_initialize_by_name(attributes[:name]).tap do |support_practice|
        #{inside_vars}
        support_practice.save!
      end
    end
    TEXT
  end
validate_name_of_array(value) click to toggle source
# File lib/csv_to_seed.rb, line 45
def validate_name_of_array(value)
  raise 'Must be a valid variable name' if (value =~ /^[a-z_][a-zA-Z_0-9]*$/).nil?
end
write_seedrb_file() click to toggle source
# File lib/csv_to_seed.rb, line 70
def write_seedrb_file
  File.write(Dir.pwd + '/db/seeds.rb', "\n\r#{@name_of_array} = #{csv_to_hash} \n\r #{set_string_to_create_loop}", File.size(Dir.pwd + '/db/seeds.rb'), mode: 'a')
  @file.close
end