honey_format 0.23.0 → 0.24.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 +4 -4
- data/CHANGELOG.md +12 -1
- data/README.md +52 -0
- data/lib/honey_format/converters/convert_string.rb +9 -1
- data/lib/honey_format/converters/converters.rb +2 -0
- data/lib/honey_format/matrix/row_builder.rb +5 -1
- data/lib/honey_format/version.rb +1 -1
- metadata +6 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4e7b05a5cbde9ef4df67f043ff78e75c545563faa02333c57c97f70f1adec0f9
|
4
|
+
data.tar.gz: 91275865eabfd283c22ef0507a76b0c433d64a36c2ab68d92501548b982a00c8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c5904b276f295163855c1d74f7afa27568572e50583ec54532e571f70eb59ce06648e60a0e0d29806289d7126b8d0a9351acdd746967c247db03004cec3c4356
|
7
|
+
data.tar.gz: e498be3c0e31e594cc42d2186404d367564111d70d2fffbe0011d0ea208e5f217ff8416061f12cbdff5137b3075e1f09d857b2dd189d785b865381d02c429d07
|
data/CHANGELOG.md
CHANGED
@@ -1,8 +1,19 @@
|
|
1
1
|
# HEAD
|
2
2
|
|
3
|
+
## v0.24.0
|
4
|
+
|
5
|
+
* Add support for multiple/chained converters. [PR#69](https://github.com/buren/honey_format/pull/69)
|
6
|
+
```ruby
|
7
|
+
csv_string = "Id,Username\n1, BuRen "
|
8
|
+
type_map = { username: [:strip, :downcase] }
|
9
|
+
csv = HoneyFormat::CSV.new(csv_string, type_map: type_map)
|
10
|
+
csv.rows.first.username # => "buren"
|
11
|
+
```
|
12
|
+
* Add `strip` and `strip!` converter
|
13
|
+
|
3
14
|
## v0.23.0
|
4
15
|
|
5
|
-
* Add `Rows#
|
16
|
+
* Add `Rows#columns` method that returns header columns
|
6
17
|
|
7
18
|
## v0.22.0
|
8
19
|
|
data/README.md
CHANGED
@@ -107,6 +107,14 @@ csv = HoneyFormat::CSV.new(csv_string, type_map: type_map)
|
|
107
107
|
csv.rows.first.username # => "BUREN"
|
108
108
|
```
|
109
109
|
|
110
|
+
Combine multiple converters
|
111
|
+
```ruby
|
112
|
+
csv_string = "Id,Username\n1, BuRen "
|
113
|
+
type_map = { username: [:strip, :downcase] }
|
114
|
+
csv = HoneyFormat::CSV.new(csv_string, type_map: type_map)
|
115
|
+
csv.rows.first.username # => "buren"
|
116
|
+
```
|
117
|
+
|
110
118
|
Register your own converter
|
111
119
|
```ruby
|
112
120
|
HoneyFormat.configure do |config|
|
@@ -376,6 +384,50 @@ matrix.to_csv # => "name,id\nJACOB,1\n"
|
|
376
384
|
|
377
385
|
If you want to see more usage examples check out the [`examples/`](https://github.com/buren/honey_format/tree/master/examples) and [`spec/`](https://github.com/buren/honey_format/tree/master/spec) directories and of course [on RubyDoc](https://www.rubydoc.info/gems/honey_format/).
|
378
386
|
|
387
|
+
|
388
|
+
__SQL example__
|
389
|
+
|
390
|
+
When you want the result as an object, with certain columns converted to objects.
|
391
|
+
|
392
|
+
```ruby
|
393
|
+
require 'mysql2'
|
394
|
+
|
395
|
+
class DBClient
|
396
|
+
def initialize(host:, username:, password:, port: 3306)
|
397
|
+
@client = Mysql2::Client.new(
|
398
|
+
host: host,
|
399
|
+
username: username,
|
400
|
+
password: password,
|
401
|
+
port: port
|
402
|
+
)
|
403
|
+
end
|
404
|
+
|
405
|
+
def query(sql, type_map: {})
|
406
|
+
result = @client.query(sql)
|
407
|
+
return if result.first.nil?
|
408
|
+
|
409
|
+
matrix = HoneyFormat::Matrix.new(
|
410
|
+
result.map(&:values),
|
411
|
+
header: result.first.keys,
|
412
|
+
type_map: type_map
|
413
|
+
)
|
414
|
+
matrix.rows
|
415
|
+
end
|
416
|
+
end
|
417
|
+
```
|
418
|
+
|
419
|
+
Usage example with a fictional "users" database table (schema: `name`, `created_at`)
|
420
|
+
```ruby
|
421
|
+
client = DbClient.new(host: '127.0.0.1', username: 'root', password: nil)
|
422
|
+
users = client.query(
|
423
|
+
'SELECT * FROM users',
|
424
|
+
type_map: { created_at: :datetime! }
|
425
|
+
)
|
426
|
+
user = users.first
|
427
|
+
user.name # => buren
|
428
|
+
user.created_at.class # => Time
|
429
|
+
```
|
430
|
+
|
379
431
|
## CLI
|
380
432
|
|
381
433
|
> Perfect when you want to get something simple done quickly.
|
@@ -10,6 +10,9 @@ module HoneyFormat
|
|
10
10
|
# Convert to upcase or nil
|
11
11
|
ConvertUpcase = proc { |v| v&.upcase }
|
12
12
|
|
13
|
+
# Convert to stripped string
|
14
|
+
ConvertStrip = proc { |v| v&.strip }
|
15
|
+
|
13
16
|
# Convert to symbol or nil
|
14
17
|
ConvertSymbol = proc { |v| v&.to_sym }
|
15
18
|
|
@@ -35,8 +38,13 @@ module HoneyFormat
|
|
35
38
|
ConvertDowncase.call(v) || raise(ArgumentError, "can't convert nil to downcased string")
|
36
39
|
end
|
37
40
|
|
41
|
+
# Convert to downcase or raise error
|
42
|
+
StrictConvertStrip = proc do |v|
|
43
|
+
ConvertStrip.call(v) || raise(ArgumentError, "can't convert nil to downcased string")
|
44
|
+
end
|
45
|
+
|
38
46
|
# Convert to symbol or raise error
|
39
47
|
StrictConvertSymbol = proc do |v|
|
40
|
-
ConvertSymbol.call(v) || raise(ArgumentError, "can't convert nil to
|
48
|
+
ConvertSymbol.call(v) || raise(ArgumentError, "can't convert nil to stripped string")
|
41
49
|
end
|
42
50
|
end
|
@@ -20,6 +20,7 @@ module HoneyFormat
|
|
20
20
|
symbol!: StrictConvertSymbol,
|
21
21
|
downcase!: StrictConvertDowncase,
|
22
22
|
upcase!: StrictConvertUpcase,
|
23
|
+
strip!: StrictConvertStrip,
|
23
24
|
boolean!: StrictConvertBoolean,
|
24
25
|
# safe variants
|
25
26
|
decimal: ConvertDecimal,
|
@@ -31,6 +32,7 @@ module HoneyFormat
|
|
31
32
|
symbol: ConvertSymbol,
|
32
33
|
downcase: ConvertDowncase,
|
33
34
|
upcase: ConvertUpcase,
|
35
|
+
strip: ConvertStrip,
|
34
36
|
boolean: ConvertBoolean,
|
35
37
|
md5: ConvertMD5,
|
36
38
|
hex: ConvertHex,
|
@@ -57,7 +57,11 @@ module HoneyFormat
|
|
57
57
|
|
58
58
|
# Convert values
|
59
59
|
@type_map.each do |column, type|
|
60
|
-
|
60
|
+
types = type.respond_to?(:each) ? type : [type]
|
61
|
+
value = row[column]
|
62
|
+
types.each { |t| value = @converter.call(value, t) }
|
63
|
+
|
64
|
+
row[column] = value
|
61
65
|
end
|
62
66
|
|
63
67
|
return row unless @builder
|
data/lib/honey_format/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: honey_format
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.24.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jacob Burenstam
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-07-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: benchmark-ips
|
@@ -153,7 +153,7 @@ homepage: https://github.com/buren/honey_format
|
|
153
153
|
licenses:
|
154
154
|
- MIT
|
155
155
|
metadata: {}
|
156
|
-
post_install_message:
|
156
|
+
post_install_message:
|
157
157
|
rdoc_options: []
|
158
158
|
require_paths:
|
159
159
|
- lib
|
@@ -168,8 +168,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
168
168
|
- !ruby/object:Gem::Version
|
169
169
|
version: '0'
|
170
170
|
requirements: []
|
171
|
-
rubygems_version: 3.
|
172
|
-
signing_key:
|
171
|
+
rubygems_version: 3.1.4
|
172
|
+
signing_key:
|
173
173
|
specification_version: 4
|
174
174
|
summary: Makes working with CSVs as smooth as honey.
|
175
175
|
test_files: []
|