ycv 0.1.0
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.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +27 -0
- data/LICENSE.txt +21 -0
- data/README.md +61 -0
- data/Rakefile +12 -0
- data/bin/ycv +32 -0
- data/lib/ycv/to_csv.rb +20 -0
- data/lib/ycv/to_file.rb +11 -0
- data/lib/ycv/to_yaml.rb +49 -0
- data/lib/ycv/version.rb +5 -0
- data/lib/ycv.rb +21 -0
- data/sig/ycv.rbs +4 -0
- data/ycv.gemspec +40 -0
- metadata +60 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: dc8b8fd494999f14f547f1f53b24541bf84f263858230203d2e79919bc8bb743
|
4
|
+
data.tar.gz: cf3e226d25dae7eefcedcf1386683a79cf1a8cd4d95978c92664e6f3ee96109a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dfbcca5d84d53100138ea8862540ce4a5e344d0f5249864334f391c86895fe5610a0c9c3d154a4eb7cfe1c1c98d30bcbb566d5b334db897bcb43cc06aa440171
|
7
|
+
data.tar.gz: 8bf7f1533929631e5e7dc12e7872f5a1419e95a8611bae2ea51f7edb18908ac091d0dd53185ed1420f48282f2e2ce3c56a535d88a3dfe36302319ef9e1c87a26
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 3.0
|
3
|
+
|
4
|
+
Style/StringLiterals:
|
5
|
+
Enabled: true
|
6
|
+
EnforcedStyle: double_quotes
|
7
|
+
|
8
|
+
Style/StringLiteralsInInterpolation:
|
9
|
+
Enabled: true
|
10
|
+
EnforcedStyle: double_quotes
|
11
|
+
|
12
|
+
Layout/LineLength:
|
13
|
+
Max: 120
|
14
|
+
|
15
|
+
Metrics/BlockLength:
|
16
|
+
Max: 50
|
17
|
+
Exclude:
|
18
|
+
- spec/**/*
|
19
|
+
|
20
|
+
Style/Documentation:
|
21
|
+
Enabled: false
|
22
|
+
|
23
|
+
Metrics/MethodLength:
|
24
|
+
Max: 50
|
25
|
+
|
26
|
+
Metrics/AbcSize:
|
27
|
+
Max: 25
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Nozomi-Hijikata
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# Ycv
|
2
|
+
|
3
|
+
The `Ycv` Gem (YAML to CSV and vice versa) is a simple yet powerful command-line tool designed specifically for converting Rails fixtures between YAML and CSV formats. This makes `Ycv` an ideal tool for developers working with Rails applications, allowing for easy manipulation and conversion of data fixtures and configurations.
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
To install the `Ycv` gem, add the following line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'ycv'
|
12
|
+
```
|
13
|
+
Then execute:
|
14
|
+
````bash
|
15
|
+
bundle install
|
16
|
+
````
|
17
|
+
|
18
|
+
Alternatively, you can install it yourself from the command line:
|
19
|
+
|
20
|
+
```bash
|
21
|
+
gem install ycv
|
22
|
+
```
|
23
|
+
|
24
|
+
# Usage
|
25
|
+
## Command-Line Interface (CLI)
|
26
|
+
The ycv command-line tool is straightforward to use and supports output to both files and standard output (STDOUT), making it versatile for scripting and piping to other commands.
|
27
|
+
|
28
|
+
### Converting Files
|
29
|
+
|
30
|
+
To convert a YAML file to CSV:
|
31
|
+
```bash
|
32
|
+
ycv path/to/input.yml path/to/output.csv
|
33
|
+
```
|
34
|
+
|
35
|
+
To convert a CSV file to YAML:
|
36
|
+
```bash
|
37
|
+
ycv path/to/input.csv path/to/output.yml
|
38
|
+
```
|
39
|
+
|
40
|
+
Using Standard Output
|
41
|
+
If you prefer to output the conversion result to STDOUT (for example, for further processing with other command-line tools), simply omit the output file path:
|
42
|
+
|
43
|
+
Convert YAML to CSV and output to STDOUT:
|
44
|
+
```bash
|
45
|
+
ycv path/to/input.yml > output.csv
|
46
|
+
```
|
47
|
+
|
48
|
+
Convert CSV to YAML and output to STDOUT:
|
49
|
+
```bash
|
50
|
+
ycv path/to/input.csv > output.yml
|
51
|
+
```
|
52
|
+
This feature allows you to easily integrate ycv into your command-line workflows and use it in conjunction with other tools like grep, awk, or redirections.
|
53
|
+
|
54
|
+
## As a Ruby Library
|
55
|
+
In addition to the CLI, Ycv can be used as a library within Ruby applications.
|
56
|
+
|
57
|
+
|
58
|
+
# Contributing
|
59
|
+
Contributions to Ycv are always welcome, whether it's through bug reports, pull requests, or feature requests.
|
60
|
+
|
61
|
+
Feel free to contribute GitHub at https://github.com/Nozomi-Hijikata/ycv.
|
data/Rakefile
ADDED
data/bin/ycv
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require_relative "../lib/ycv"
|
5
|
+
|
6
|
+
if ARGV.empty? || ARGV.length > 2
|
7
|
+
puts "Usage: ycv INPUT_FILE [OUTPUT_FILE]"
|
8
|
+
exit 1
|
9
|
+
end
|
10
|
+
|
11
|
+
input_path = ARGV[0]
|
12
|
+
output_path = ARGV[1]
|
13
|
+
|
14
|
+
begin
|
15
|
+
conversion_method = case File.extname(input_path)
|
16
|
+
when ".csv" then :yaml
|
17
|
+
when ".yml", ".yaml" then :csv
|
18
|
+
else raise Ycv::Error, "Unsupported file type"
|
19
|
+
end
|
20
|
+
|
21
|
+
result = Ycv.convert(input_path, conversion_method)
|
22
|
+
|
23
|
+
if output_path
|
24
|
+
Ycv::ToFile.call(result, output_path)
|
25
|
+
puts "Conversion successful: #{input_path} -> #{output_path || "STDOUT"}"
|
26
|
+
else
|
27
|
+
puts result
|
28
|
+
end
|
29
|
+
rescue Ycv::Error => e
|
30
|
+
puts "Error: #{e.message}"
|
31
|
+
exit 1
|
32
|
+
end
|
data/lib/ycv/to_csv.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "csv"
|
4
|
+
require "yaml"
|
5
|
+
|
6
|
+
module Ycv
|
7
|
+
class ToCsv
|
8
|
+
def self.call(file_path)
|
9
|
+
file_path[/(\w+)\.(yml|yaml)$/, 1]
|
10
|
+
fixtures = YAML.load_file(file_path)
|
11
|
+
headers = fixtures.first[1].keys
|
12
|
+
CSV.generate do |c|
|
13
|
+
c << headers
|
14
|
+
fixtures.each_value do |v|
|
15
|
+
c << v.values
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/ycv/to_file.rb
ADDED
data/lib/ycv/to_yaml.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "csv"
|
4
|
+
require "yaml"
|
5
|
+
require "active_support/all"
|
6
|
+
|
7
|
+
module Ycv
|
8
|
+
class ToYaml
|
9
|
+
def self.call(file_path)
|
10
|
+
file_name = extract_file_name(file_path)
|
11
|
+
fixtures_section_name = file_name.singularize
|
12
|
+
fixtures = process_csv_rows(file_path, fixtures_section_name)
|
13
|
+
convert_to_yaml(fixtures)
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.extract_file_name(file_path)
|
17
|
+
file_path[/(\w+).csv$/, 1]
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.process_csv_rows(file_path, section_name)
|
21
|
+
fixtures = {}
|
22
|
+
CSV.foreach(file_path, headers: true) do |row|
|
23
|
+
key = "#{section_name}_#{row[0]}"
|
24
|
+
fixtures[key] = process_row(row)
|
25
|
+
end
|
26
|
+
fixtures
|
27
|
+
end
|
28
|
+
|
29
|
+
def self.process_row(row)
|
30
|
+
row.to_hash.transform_values do |value|
|
31
|
+
parse_value(value)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.parse_value(value)
|
36
|
+
case value
|
37
|
+
when /\A\d*\.\d*\Z/ then value.to_f
|
38
|
+
when /\A\d+\Z/ then value.to_i
|
39
|
+
else value
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def self.convert_to_yaml(data)
|
44
|
+
yaml_content = data.to_yaml
|
45
|
+
yaml_content.sub!(/---\n/, "")
|
46
|
+
yaml_content
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/lib/ycv/version.rb
ADDED
data/lib/ycv.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "ycv/version"
|
4
|
+
require_relative "ycv/to_yaml"
|
5
|
+
require_relative "ycv/to_csv"
|
6
|
+
require_relative "ycv/to_file"
|
7
|
+
|
8
|
+
module Ycv
|
9
|
+
class Error < StandardError; end
|
10
|
+
|
11
|
+
def self.convert(input_path, format)
|
12
|
+
case format
|
13
|
+
when :csv
|
14
|
+
Ycv::ToCsv.call(input_path)
|
15
|
+
when :yaml
|
16
|
+
Ycv::ToYaml.call(input_path)
|
17
|
+
else
|
18
|
+
raise Error, "Unsupported format: #{format}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/sig/ycv.rbs
ADDED
data/ycv.gemspec
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/ycv/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "ycv"
|
7
|
+
spec.version = Ycv::VERSION
|
8
|
+
spec.authors = ["Nozomi-Hijikata"]
|
9
|
+
spec.email = ["nozomi.hijikata@techouse.jp"]
|
10
|
+
|
11
|
+
spec.summary = "A tool for converting between YAML and CSV formats."
|
12
|
+
spec.description = "This gem allows for the conversion of CSV files to YAML format suitable for Rails fixtures and vice versa."
|
13
|
+
spec.homepage = "https://github.com/Nozomi-Hijikata/ycv"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = ">= 3.0.0"
|
16
|
+
|
17
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to your gem server 'https://example.com'"
|
18
|
+
|
19
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
20
|
+
spec.metadata["source_code_uri"] = "https://github.com/Nozomi-Hijikata/ycv"
|
21
|
+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
22
|
+
|
23
|
+
# Specify which files should be added to the gem when it is released.
|
24
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
25
|
+
spec.files = Dir.chdir(__dir__) do
|
26
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
27
|
+
(File.expand_path(f) == __FILE__) ||
|
28
|
+
f.start_with?(*%w[bin/ test/ spec/ features/ .git .github appveyor Gemfile])
|
29
|
+
end
|
30
|
+
end
|
31
|
+
spec.bindir = "bin"
|
32
|
+
spec.executables = "ycv"
|
33
|
+
spec.require_paths = ["lib"]
|
34
|
+
|
35
|
+
# Uncomment to register a new dependency of your gem
|
36
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
37
|
+
|
38
|
+
# For more information and examples about making a new gem, check out our
|
39
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,60 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ycv
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Nozomi-Hijikata
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-03-01 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: This gem allows for the conversion of CSV files to YAML format suitable
|
14
|
+
for Rails fixtures and vice versa.
|
15
|
+
email:
|
16
|
+
- nozomi.hijikata@techouse.jp
|
17
|
+
executables:
|
18
|
+
- ycv
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- ".rspec"
|
23
|
+
- ".rubocop.yml"
|
24
|
+
- LICENSE.txt
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- bin/ycv
|
28
|
+
- lib/ycv.rb
|
29
|
+
- lib/ycv/to_csv.rb
|
30
|
+
- lib/ycv/to_file.rb
|
31
|
+
- lib/ycv/to_yaml.rb
|
32
|
+
- lib/ycv/version.rb
|
33
|
+
- sig/ycv.rbs
|
34
|
+
- ycv.gemspec
|
35
|
+
homepage: https://github.com/Nozomi-Hijikata/ycv
|
36
|
+
licenses:
|
37
|
+
- MIT
|
38
|
+
metadata:
|
39
|
+
homepage_uri: https://github.com/Nozomi-Hijikata/ycv
|
40
|
+
source_code_uri: https://github.com/Nozomi-Hijikata/ycv
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: 3.0.0
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
requirements: []
|
56
|
+
rubygems_version: 3.4.10
|
57
|
+
signing_key:
|
58
|
+
specification_version: 4
|
59
|
+
summary: A tool for converting between YAML and CSV formats.
|
60
|
+
test_files: []
|