csv_to_yaml 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +87 -10
  3. data/lib/csv_to_yaml/version.rb +1 -1
  4. metadata +6 -4
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: efb0d27827ab1b8b65d932e2ab99592df354b85b1c6d2a7dc4a616735524b010
4
- data.tar.gz: 92594a121766813967749c4ed1948db796ac13dbafeb843b1abcab2a027e8482
3
+ metadata.gz: 203833bb7ee6445da088d9d8a2be114623aef646f819c0905e2a160b9763ded0
4
+ data.tar.gz: 60ade610796549a7c6f6990b1da2f4d623f0024c0602cfe3596fd0fe22c032d7
5
5
  SHA512:
6
- metadata.gz: 267e1d0c52d49b2e3b3dfae5fd82ecfd075f0b6ecd7d5d8af419ad00d6dfa637f0af913f39607eb4211a9be05f421b361d4355bb26e2887c0cebfc9d24662dbc
7
- data.tar.gz: 8ac49a1370650e33ebbaec656e38a9bc30a69ba31fc9164fa0984aa3e0acb347f22420d25bf98bb5dff8c76a1dbbe4b627a38708ccfd2f62336eb3bbd76944c2
6
+ metadata.gz: 84fc475c3506061206f71c1b98919afe590691cfd5105619371d0f293f6c30a18b14f655f26a2c5d6e1164dff4fb1eb651d38833fb2c08fe7b96c2d69c85d16b
7
+ data.tar.gz: 69b564e70a63fbe57162383111add40bc150ee235c83aec62aee49bf4934d57412d3a8e08fda224aba93492b9a118faaf00bf375702e6eb25d7dd15a3978d925
data/README.md CHANGED
@@ -1,13 +1,14 @@
1
1
  # CsvToYaml
2
2
 
3
- CsvToYaml is a Ruby gem that provides an easy way to convert CSV(Comma Separated Values) files to YAML(YAML Ain't Markup Language) format. It handles various data types and attempts to infer and convert them appropriately.
3
+ CsvToYaml is a Ruby gem designed to simplify the process of converting CSV (Comma Separated Values) files into YAML (YAML Ain't Markup Language) format, particularly for use in Rails applications where YAML files are needed for seeding data into the database. This gem was born out of a real-world need when seed data was provided in CSV format, but the project required YAML for `rails db:seed`.
4
4
 
5
5
  ## Features
6
6
 
7
- - Convert CSV files to YAML format
8
- - Automatic data type inference and conversion
9
- - Handles empty files and invalid CSV formats
10
- - Simple and easy-to-use API
7
+ - **CSV to YAML Conversion for Rails Seeds:** Easily convert CSV files into YAML format, making it straightforward to create seed data for Rails applications.
8
+ - **Automatic Data Type Inference:** The gem intelligently infers and converts data types, including boolean values, ensuring your YAML files are accurate and ready to use directly without further adjustments.
9
+ - **Flexible File Handling:** Input CSV files can be read from any directory, and output YAML files can be saved to any directory, providing flexibility in your project setup.
10
+ - **Error Handling:** Robust handling of empty files, invalid CSV formats, and other potential issues, providing clear error messages to assist in troubleshooting.
11
+ - **Simple API:** Designed with ease of use in mind, the API allows you to convert files with minimal setup.
11
12
 
12
13
  ## Installation
13
14
 
@@ -26,19 +27,95 @@ Here's a basic example of how to use CsvToYaml:
26
27
  ```ruby
27
28
  require 'csv_to_yaml'
28
29
 
29
- # Convert a CSV file to YAML
30
- CsvToYaml.convert('input.csv', 'output.yaml')
30
+ # Convert a CSV file to a YAML file for Rails seed data
31
+ CsvToYaml.convert('path/to/input.csv', 'path/to/output.yaml')
31
32
  ```
32
33
 
33
- This will read the `input.csv` file, convert its contents to YAML format, and save the result to `output.yaml`.
34
+ This will read the `input.csv` file from any specified directory, convert its contents to YAML format, and save the result to the specified directory as `output.yaml`.
35
+
36
+ ## Example: Converting CSV to YAML and Seeding Data in Rails
37
+
38
+ Here's a step-by-step example of how to convert a CSV file to YAML and then use it to seed data into a Rails application.
39
+
40
+ ### Step 1: CSV File
41
+
42
+ Suppose you have a CSV file named `data/users.csv` with the following content:
43
+
44
+ ```csv:data/users.csv
45
+ id,name,age,verified
46
+ 1,Alice,30,true
47
+ 2,Bob,25,false
48
+ 3,Charlie,35,true
49
+ 4,David,40,false
50
+ 5,Eve,20,true
51
+ ```
52
+
53
+ ### Step 2: Convert CSV to YAML
54
+
55
+ Using CsvToYaml, you can convert this CSV file into a YAML file and save it to any directory of your choice:
56
+
57
+ ```ruby
58
+ require 'csv_to_yaml'
59
+
60
+ CsvToYaml.convert('data/users.csv', 'db/seeds/users.yaml')
61
+ ```
62
+
63
+ This will generate an `users.yaml` file at the specified location (`db/seeds/users.yaml`) with the following content:
64
+
65
+ ```yaml:db/seeds/users.yaml
66
+ - id: 1
67
+ name: Alice
68
+ age: 30
69
+ verified: true
70
+ - id: 2
71
+ name: Bob
72
+ age: 25
73
+ verified: false
74
+ - id: 3
75
+ name: Charlie
76
+ age: 35
77
+ verified: true
78
+ - id: 4
79
+ name: David
80
+ age: 40
81
+ verified: false
82
+ - id: 5
83
+ name: Eve
84
+ age: 20
85
+ verified: true
86
+ ```
87
+
88
+ In this YAML file, the data types (including integers and booleans) are correctly inferred and preserved, so you can use it directly in your Rails application without worrying about type mismatches, which can occur if the values were incorrectly treated as strings.
89
+
90
+ ### Step 3: Use YAML File to Seed Data
91
+
92
+ You can now use this YAML file to seed data into your Rails application. First, load the YAML file in your `db/seeds.rb`:
93
+
94
+ ```ruby
95
+ require 'yaml'
96
+
97
+ seed_data = YAML.load_file(Rails.root.join('db', 'seeds', 'users.yaml'))
98
+
99
+ seed_data.each do |record|
100
+ User.create!(record)
101
+ end
102
+ ```
103
+
104
+ Finally, run the Rails seed command to populate your database:
105
+
106
+ ```bash
107
+ $ rails db:seed
108
+ ```
109
+
110
+ Now, your database will be populated with the data from the original CSV file, converted to YAML with the correct data types.
34
111
 
35
112
  ## Error Handling
36
113
 
37
- CsvToYaml provides specific error classes to handle different scenarios:
114
+ CsvToYaml provides specific error classes to handle different scenarios, ensuring that you can troubleshoot issues effectively:
38
115
 
39
116
  ```ruby
40
117
  begin
41
- CsvToYaml.convert('input.csv', 'output.yaml')
118
+ CsvToYaml.convert('data/users.csv', 'db/seeds/users.yaml')
42
119
  rescue CsvToYaml::EmptyFileError
43
120
  puts "The input file is empty"
44
121
  rescue CsvToYaml::InvalidCsvFormatError
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CsvToYaml
4
- VERSION = "0.1.0"
4
+ VERSION = "0.1.1"
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: csv_to_yaml
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - kazuyainoue0124
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-08-12 00:00:00.000000000 Z
11
+ date: 2024-08-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -52,7 +52,9 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '1.21'
55
- description: This gem reads a CSV file and converts its content into a YAML file.
55
+ description: CsvToYaml is a Ruby gem that converts CSV files to YAML format, with
56
+ automatic data type inference, allowing seamless creation of YAML files for Rails
57
+ seed data.
56
58
  email:
57
59
  - kazuyainoue0124@users.noreply.github.com
58
60
  executables: []
@@ -92,6 +94,6 @@ requirements: []
92
94
  rubygems_version: 3.5.14
93
95
  signing_key:
94
96
  specification_version: 4
95
- summary: A gem to convert CSV data to YAML format
97
+ summary: A gem to convert CSV data to YAML format, particularly for Rails seed data.
96
98
  test_files:
97
99
  - spec/csv_to_yaml_spec.rb