philiprehberger-csv_builder 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/CHANGELOG.md +19 -0
- data/LICENSE +21 -0
- data/README.md +105 -0
- data/lib/philiprehberger/csv_builder/builder.rb +61 -0
- data/lib/philiprehberger/csv_builder/column.rb +44 -0
- data/lib/philiprehberger/csv_builder/version.rb +7 -0
- data/lib/philiprehberger/csv_builder.rb +26 -0
- metadata +56 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: de54b4bae8142b3836493b3297627a4f6da7ecc17529cf650521bfa989068324
|
|
4
|
+
data.tar.gz: 3ab9cc18882a0a7ecf9dd9a8ac6ec81507488c37ff57de1f0ce37a057680a8a7
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: e059bd4aac2698b14995b49fbf5465d9bddb89f7e89404b75d03df6dfd80bce00ff4631fc96c0ac9ee01615971d565f45f4115b1820cc6826e3868008c6f7499
|
|
7
|
+
data.tar.gz: 96b72fa4660690b33e60f20b7a41f88de010e861cf60c83aca89063a156d3cff3659a22b4ced074b4f6f423741c33194ffa891daa7590eea9579025c7d891eb0
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this gem will be documented in this file.
|
|
4
|
+
|
|
5
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
|
+
|
|
8
|
+
## [Unreleased]
|
|
9
|
+
|
|
10
|
+
## [0.1.0] - 2026-03-22
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
- Initial release
|
|
14
|
+
- Declarative column definition DSL
|
|
15
|
+
- Custom transform blocks for computed columns
|
|
16
|
+
- CSV string generation via `to_csv`
|
|
17
|
+
- File output via `to_file`
|
|
18
|
+
- Support for hash records with symbol and string keys
|
|
19
|
+
- Proper CSV escaping for values with commas and quotes
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 philiprehberger
|
|
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 all
|
|
13
|
+
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 THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
# philiprehberger-csv_builder
|
|
2
|
+
|
|
3
|
+
[](https://github.com/philiprehberger/rb-csv-builder/actions/workflows/ci.yml)
|
|
4
|
+
[](https://rubygems.org/gems/philiprehberger-csv_builder)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
|
|
7
|
+
Declarative CSV builder with column mapping and streaming output
|
|
8
|
+
|
|
9
|
+
## Requirements
|
|
10
|
+
|
|
11
|
+
- Ruby >= 3.1
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
Add to your Gemfile:
|
|
16
|
+
|
|
17
|
+
```ruby
|
|
18
|
+
gem "philiprehberger-csv_builder"
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Or install directly:
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
gem install philiprehberger-csv_builder
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Usage
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
require "philiprehberger/csv_builder"
|
|
31
|
+
|
|
32
|
+
records = [
|
|
33
|
+
{ name: 'Alice', email: 'alice@example.com', active: true },
|
|
34
|
+
{ name: 'Bob', email: 'bob@example.com', active: false }
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
builder = Philiprehberger::CsvBuilder.build(records) do
|
|
38
|
+
column :name
|
|
39
|
+
column :email
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
puts builder.to_csv
|
|
43
|
+
# name,email
|
|
44
|
+
# Alice,alice@example.com
|
|
45
|
+
# Bob,bob@example.com
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
### Custom Transforms
|
|
49
|
+
|
|
50
|
+
```ruby
|
|
51
|
+
builder = Philiprehberger::CsvBuilder.build(records) do
|
|
52
|
+
column :name
|
|
53
|
+
column :email
|
|
54
|
+
column(:status) { |r| r[:active] ? 'Active' : 'Inactive' }
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
puts builder.to_csv
|
|
58
|
+
# name,email,status
|
|
59
|
+
# Alice,alice@example.com,Active
|
|
60
|
+
# Bob,bob@example.com,Inactive
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### File Output
|
|
64
|
+
|
|
65
|
+
```ruby
|
|
66
|
+
builder = Philiprehberger::CsvBuilder.build(records) do
|
|
67
|
+
column :name
|
|
68
|
+
column :email
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
builder.to_file('output.csv')
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Headers
|
|
75
|
+
|
|
76
|
+
```ruby
|
|
77
|
+
builder = Philiprehberger::CsvBuilder.build(records) do
|
|
78
|
+
column :name
|
|
79
|
+
column :email
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
builder.headers # => ["name", "email"]
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## API
|
|
86
|
+
|
|
87
|
+
| Method | Description |
|
|
88
|
+
|--------|-------------|
|
|
89
|
+
| `CsvBuilder.build(records, &block)` | Build a CSV using the column DSL |
|
|
90
|
+
| `Builder#column(name, &block)` | Define a column with optional transform |
|
|
91
|
+
| `Builder#to_csv` | Generate CSV as a string |
|
|
92
|
+
| `Builder#to_file(path)` | Write CSV to a file |
|
|
93
|
+
| `Builder#headers` | Return column header names |
|
|
94
|
+
|
|
95
|
+
## Development
|
|
96
|
+
|
|
97
|
+
```bash
|
|
98
|
+
bundle install
|
|
99
|
+
bundle exec rspec # Run tests
|
|
100
|
+
bundle exec rubocop # Check code style
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## License
|
|
104
|
+
|
|
105
|
+
MIT
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'csv'
|
|
4
|
+
require_relative 'column'
|
|
5
|
+
|
|
6
|
+
module Philiprehberger
|
|
7
|
+
module CsvBuilder
|
|
8
|
+
# DSL builder for constructing CSV output from records
|
|
9
|
+
class Builder
|
|
10
|
+
# @return [Array<Column>] the defined columns
|
|
11
|
+
attr_reader :columns
|
|
12
|
+
|
|
13
|
+
# @return [Array] the source records
|
|
14
|
+
attr_reader :records
|
|
15
|
+
|
|
16
|
+
# @param records [Array] the source records
|
|
17
|
+
def initialize(records)
|
|
18
|
+
@records = records
|
|
19
|
+
@columns = []
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Define a column
|
|
23
|
+
#
|
|
24
|
+
# @param name [Symbol, String] the column name
|
|
25
|
+
# @yield [record] optional block to transform the value
|
|
26
|
+
# @yieldparam record [Object] the source record
|
|
27
|
+
# @return [self]
|
|
28
|
+
def column(name, &block)
|
|
29
|
+
@columns << Column.new(name, &block)
|
|
30
|
+
self
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Return the header row
|
|
34
|
+
#
|
|
35
|
+
# @return [Array<String>]
|
|
36
|
+
def headers
|
|
37
|
+
@columns.map(&:header)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Generate the CSV as a string
|
|
41
|
+
#
|
|
42
|
+
# @return [String]
|
|
43
|
+
def to_csv
|
|
44
|
+
CSV.generate do |csv|
|
|
45
|
+
csv << headers
|
|
46
|
+
@records.each do |record|
|
|
47
|
+
csv << @columns.map { |col| col.extract(record) }
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Write the CSV to a file
|
|
53
|
+
#
|
|
54
|
+
# @param path [String] the output file path
|
|
55
|
+
# @return [void]
|
|
56
|
+
def to_file(path)
|
|
57
|
+
File.write(path, to_csv)
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Philiprehberger
|
|
4
|
+
module CsvBuilder
|
|
5
|
+
# Represents a single column definition in a CSV builder
|
|
6
|
+
class Column
|
|
7
|
+
# @return [Symbol] the column name
|
|
8
|
+
attr_reader :name
|
|
9
|
+
|
|
10
|
+
# @return [Proc, nil] optional transform block
|
|
11
|
+
attr_reader :transform
|
|
12
|
+
|
|
13
|
+
# @param name [Symbol, String] the column name (also used as the record key)
|
|
14
|
+
# @param transform [Proc, nil] optional block to transform the value
|
|
15
|
+
def initialize(name, &transform)
|
|
16
|
+
@name = name.to_sym
|
|
17
|
+
@transform = block_given? ? transform : nil
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Extract the value for this column from a record
|
|
21
|
+
#
|
|
22
|
+
# @param record [Hash, Object] the source record
|
|
23
|
+
# @return [String] the extracted and converted value
|
|
24
|
+
def extract(record)
|
|
25
|
+
value = if @transform
|
|
26
|
+
@transform.call(record)
|
|
27
|
+
elsif record.is_a?(Hash)
|
|
28
|
+
record[@name] || record[@name.to_s]
|
|
29
|
+
elsif record.respond_to?(@name)
|
|
30
|
+
record.send(@name)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
value.to_s
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Return the header label for this column
|
|
37
|
+
#
|
|
38
|
+
# @return [String]
|
|
39
|
+
def header
|
|
40
|
+
@name.to_s
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'csv_builder/version'
|
|
4
|
+
require_relative 'csv_builder/column'
|
|
5
|
+
require_relative 'csv_builder/builder'
|
|
6
|
+
|
|
7
|
+
module Philiprehberger
|
|
8
|
+
module CsvBuilder
|
|
9
|
+
class Error < StandardError; end
|
|
10
|
+
|
|
11
|
+
# Build a CSV from records using a declarative DSL
|
|
12
|
+
#
|
|
13
|
+
# @param records [Array] the source records
|
|
14
|
+
# @yield [builder] the builder instance for defining columns
|
|
15
|
+
# @yieldparam builder [Builder]
|
|
16
|
+
# @return [Builder] the configured builder
|
|
17
|
+
# @raise [Error] if no block is given
|
|
18
|
+
def self.build(records, &block)
|
|
19
|
+
raise Error, 'A block is required' unless block
|
|
20
|
+
|
|
21
|
+
builder = Builder.new(records)
|
|
22
|
+
builder.instance_eval(&block)
|
|
23
|
+
builder
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: philiprehberger-csv_builder
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Philip Rehberger
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-03-22 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: Build CSV files from record collections using a declarative DSL with
|
|
14
|
+
column definitions, custom transforms, and file output support.
|
|
15
|
+
email:
|
|
16
|
+
- me@philiprehberger.com
|
|
17
|
+
executables: []
|
|
18
|
+
extensions: []
|
|
19
|
+
extra_rdoc_files: []
|
|
20
|
+
files:
|
|
21
|
+
- CHANGELOG.md
|
|
22
|
+
- LICENSE
|
|
23
|
+
- README.md
|
|
24
|
+
- lib/philiprehberger/csv_builder.rb
|
|
25
|
+
- lib/philiprehberger/csv_builder/builder.rb
|
|
26
|
+
- lib/philiprehberger/csv_builder/column.rb
|
|
27
|
+
- lib/philiprehberger/csv_builder/version.rb
|
|
28
|
+
homepage: https://github.com/philiprehberger/rb-csv-builder
|
|
29
|
+
licenses:
|
|
30
|
+
- MIT
|
|
31
|
+
metadata:
|
|
32
|
+
homepage_uri: https://github.com/philiprehberger/rb-csv-builder
|
|
33
|
+
source_code_uri: https://github.com/philiprehberger/rb-csv-builder
|
|
34
|
+
changelog_uri: https://github.com/philiprehberger/rb-csv-builder/blob/main/CHANGELOG.md
|
|
35
|
+
bug_tracker_uri: https://github.com/philiprehberger/rb-csv-builder/issues
|
|
36
|
+
rubygems_mfa_required: 'true'
|
|
37
|
+
post_install_message:
|
|
38
|
+
rdoc_options: []
|
|
39
|
+
require_paths:
|
|
40
|
+
- lib
|
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
42
|
+
requirements:
|
|
43
|
+
- - ">="
|
|
44
|
+
- !ruby/object:Gem::Version
|
|
45
|
+
version: 3.1.0
|
|
46
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
47
|
+
requirements:
|
|
48
|
+
- - ">="
|
|
49
|
+
- !ruby/object:Gem::Version
|
|
50
|
+
version: '0'
|
|
51
|
+
requirements: []
|
|
52
|
+
rubygems_version: 3.5.22
|
|
53
|
+
signing_key:
|
|
54
|
+
specification_version: 4
|
|
55
|
+
summary: Declarative CSV builder with column mapping and streaming output
|
|
56
|
+
test_files: []
|