Class: CSVToSeed
- Inherits:
-
Object
- Object
- CSVToSeed
- Defined in:
- lib/csv_to_seed.rb
Instance Attribute Summary collapse
-
#file ⇒ Object
Returns the value of attribute file.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#name_of_array ⇒ Object
Returns the value of attribute name_of_array.
Instance Method Summary collapse
- #csv_object ⇒ Object
- #csv_to_hash ⇒ Object
- #get_csv_body ⇒ Object
-
#initialize(args) ⇒ CSVToSeed
constructor
A new instance of CSVToSeed.
- #replace_and_fix_csv_string(fix_me) ⇒ Object
- #set_string_to_create_loop ⇒ Object
- #validate_name_of_array(value) ⇒ Object
- #write_seedrb_file ⇒ Object
Constructor Details
#initialize(args) ⇒ CSVToSeed
Returns a new instance of CSVToSeed.
6 7 8 9 10 |
# 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 |
Instance Attribute Details
#file ⇒ Object
Returns the value of attribute file.
4 5 6 |
# File 'lib/csv_to_seed.rb', line 4 def file @file end |
#headers ⇒ Object
Returns the value of attribute headers.
4 5 6 |
# File 'lib/csv_to_seed.rb', line 4 def headers @headers end |
#name_of_array ⇒ Object
Returns the value of attribute name_of_array.
4 5 6 |
# File 'lib/csv_to_seed.rb', line 4 def name_of_array @name_of_array end |
Instance Method Details
#csv_object ⇒ Object
21 22 23 |
# 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 ⇒ Object
25 26 27 28 29 30 31 32 33 34 |
# 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 ⇒ Object
12 13 14 15 16 17 18 19 |
# 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) ⇒ Object
36 37 38 39 40 41 42 43 |
# 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 ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# 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) ⇒ Object
45 46 47 |
# 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 ⇒ Object
70 71 72 73 |
# 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 |