philiprehberger-data_mapper 0.5.0 → 0.7.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 +11 -0
- data/README.md +21 -0
- data/lib/philiprehberger/data_mapper/mapping.rb +29 -0
- data/lib/philiprehberger/data_mapper/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: '08f4e17d177ee25cbc161e5873c41623a8f774b0a2c36faa1e43776cbea3c5a2'
|
|
4
|
+
data.tar.gz: 133251c11f08fd0204a5672fa78c60a1bec5b3225648e49d1c67ffc01b4102cd
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 294795cf41fadc25753f7f951fa584b6c09a934e5ba10cdd4f4a95439e3e5161487d69d1916fd58ac890e796569d0c69c2ba418fbf3165b1d10462658f8820ec
|
|
7
|
+
data.tar.gz: 50a8a7eccca2e2822014b57b1d670588ec5595db505e53e37506718475b3e1a2621d79e1f6e07d4e1288165bd58fd111066f5743a5290643a752fe898c29bf98
|
data/CHANGELOG.md
CHANGED
|
@@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
+
## [0.7.0] - 2026-05-01
|
|
11
|
+
|
|
12
|
+
### Added
|
|
13
|
+
- `Mapping#has_field?(name)` — boolean predicate for whether the mapping declares an output field with that name (covers both regular and computed fields)
|
|
14
|
+
|
|
15
|
+
## [0.6.0] - 2026-04-21
|
|
16
|
+
|
|
17
|
+
### Added
|
|
18
|
+
- `Mapping#call(hash)` — alias for `#map`, enabling `Mapping` instances to be used as callables
|
|
19
|
+
- `Mapping#to_proc` — returns a `Proc` that maps a single record, allowing idiomatic usage like `rows.map(&mapping)`
|
|
20
|
+
|
|
10
21
|
## [0.5.0] - 2026-04-18
|
|
11
22
|
|
|
12
23
|
### Added
|
data/README.md
CHANGED
|
@@ -69,6 +69,20 @@ mapping.map_lazy(infinite).first(3)
|
|
|
69
69
|
# => [{ id: 1 }, { id: 2 }, { id: 3 }]
|
|
70
70
|
```
|
|
71
71
|
|
|
72
|
+
### Use as a callable
|
|
73
|
+
|
|
74
|
+
`Mapping#call` is an alias for `#map`, and `Mapping#to_proc` returns a proc, so a mapping can be passed wherever a callable is expected:
|
|
75
|
+
|
|
76
|
+
```ruby
|
|
77
|
+
mapping = Philiprehberger::DataMapper.define do
|
|
78
|
+
field :name, from: :Name
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
mapping.call({ Name: "Ada" }) # => { name: "Ada" }
|
|
82
|
+
[{ Name: "Ada" }, { Name: "Lin" }].map(&mapping)
|
|
83
|
+
# => [{ name: "Ada" }, { name: "Lin" }]
|
|
84
|
+
```
|
|
85
|
+
|
|
72
86
|
### Parse CSV
|
|
73
87
|
|
|
74
88
|
```ruby
|
|
@@ -191,6 +205,10 @@ mapping = Philiprehberger::DataMapper.define do
|
|
|
191
205
|
computed(:upper) { |r| r[:Name].upcase }
|
|
192
206
|
end
|
|
193
207
|
mapping.field_names # => [:name, :upper]
|
|
208
|
+
|
|
209
|
+
mapping.has_field?(:name) # => true
|
|
210
|
+
mapping.has_field?(:upper) # => true
|
|
211
|
+
mapping.has_field?(:missing) # => false
|
|
194
212
|
```
|
|
195
213
|
|
|
196
214
|
### Validation
|
|
@@ -223,10 +241,13 @@ result.errors # => [{ field: :age, value: -1 }, { field: :name, value: "" }]
|
|
|
223
241
|
| `Mapping#computed(target, &block)` | Define a computed field derived from the full record |
|
|
224
242
|
| `Mapping#array_field(target, from:, split:, &transform)` | Define a field that splits a string into an array |
|
|
225
243
|
| `Mapping#map(hash)` | Apply mapping to a single hash |
|
|
244
|
+
| `Mapping#call(hash)` | Alias for `#map` — lets a `Mapping` be used as a callable |
|
|
245
|
+
| `Mapping#to_proc` | Return a `Proc` that calls `#map`, usable with `array.map(&mapping)` |
|
|
226
246
|
| `Mapping#map_with_validation(hash)` | Apply mapping and return a `MappingResult` with errors |
|
|
227
247
|
| `Mapping#map_all(array)` | Apply mapping to an array of hashes |
|
|
228
248
|
| `Mapping#map_lazy(enumerable)` | Return a `Lazy` Enumerator applying `#map` per-element for streaming |
|
|
229
249
|
| `Mapping#field_names` | Array of symbol targets for declared fields and computed fields |
|
|
250
|
+
| `Mapping#has_field?(name)` | Whether the mapping declares an output field with that name |
|
|
230
251
|
| `Mapping#reverse(hash)` | Transform output hash back to input schema |
|
|
231
252
|
| `Mapping#from_csv(string, headers: true)` | Parse CSV and map each row |
|
|
232
253
|
| `Mapping#from_json(json_string)` | Parse JSON string and map the result |
|
|
@@ -48,6 +48,22 @@ module Philiprehberger
|
|
|
48
48
|
enumerable.lazy.map { |hash| map(hash) }
|
|
49
49
|
end
|
|
50
50
|
|
|
51
|
+
# Proc conversion so a `Mapping` can be passed wherever a callable is expected
|
|
52
|
+
# (e.g. `rows.map(&mapping)`).
|
|
53
|
+
#
|
|
54
|
+
# @return [Proc] a proc that calls `#map` on its argument
|
|
55
|
+
def to_proc
|
|
56
|
+
method(:map).to_proc
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# Alias of `#map` so a Mapping instance can be invoked like a function.
|
|
60
|
+
#
|
|
61
|
+
# @param hash [Hash] the record to map
|
|
62
|
+
# @return [Hash]
|
|
63
|
+
def call(hash)
|
|
64
|
+
map(hash)
|
|
65
|
+
end
|
|
66
|
+
|
|
51
67
|
# Names (targets) of every declared field, including computed fields.
|
|
52
68
|
#
|
|
53
69
|
# @return [Array<Symbol>]
|
|
@@ -55,6 +71,19 @@ module Philiprehberger
|
|
|
55
71
|
@fields.map(&:target) + @computed_fields.map(&:target)
|
|
56
72
|
end
|
|
57
73
|
|
|
74
|
+
# Whether the mapping declares an output field with the given name.
|
|
75
|
+
#
|
|
76
|
+
# Matches both regular and computed fields. Comparison is exact
|
|
77
|
+
# (no symbol/string coercion beyond `to_sym` on the input).
|
|
78
|
+
#
|
|
79
|
+
# @param name [Symbol, String] the target field name to check
|
|
80
|
+
# @return [Boolean]
|
|
81
|
+
def has_field?(name)
|
|
82
|
+
target = name.to_sym
|
|
83
|
+
@fields.any? { |f| f.target == target } ||
|
|
84
|
+
@computed_fields.any? { |c| c.target == target }
|
|
85
|
+
end
|
|
86
|
+
|
|
58
87
|
private
|
|
59
88
|
|
|
60
89
|
def map_fields(hash)
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: philiprehberger-data_mapper
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.7.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Philip Rehberger
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-05-02 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: A zero-dependency Ruby gem for transforming data between formats with
|
|
14
14
|
a mapping DSL, field renaming, type conversion, validation, and CSV support.
|