morpher 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e0eec4915e34accded2656d830113f0ae9af9ffb77d5aa09b76886886464eb02
4
- data.tar.gz: cd0255f306246e12d521b749c1015b0c3f569df1be1ecd633aa5862524dfbcbc
3
+ metadata.gz: cc9380b32ac09b2e3765666a2c56baeb3aef272d22be1f7043f1977c9da1d815
4
+ data.tar.gz: 0736e2646b93e59dc8fb2bf992bdf7729431260a08f3b158dd1eaf1324a657f1
5
5
  SHA512:
6
- metadata.gz: 79dcc3f54caaa111991c5e2423319ed7a3f80a46c36fdb56331b0d26169c5050bb288b9ce315c9a5dcc9fcc364f6fe65509ac55c46a6cda596aee35301c43d8a
7
- data.tar.gz: 2c2b9d6c716ff971baebc94825d13e20ed986d35413e40fa817e67f33a38fe86fca894e20d30d16ff5f238f81bac306bb1572ead8d3e325e7905e1590ffb6fe3
6
+ metadata.gz: 496748be0cfeae869808cb58aead93df98b270020b93310bba695ce03bea0cbc14b7451a881ce3b3e9280acbbf2155caaba7bfba827896bd79dd1119d9fa7fc0
7
+ data.tar.gz: 0fc7f421fb376af18c2f1ea41eebf2b44350236e453a38878b1f015cb7cb15794d9e897de66c27c00143e1ce44b9271cc3f69be1ae79cb76f61f68a887e1c305
@@ -3,15 +3,18 @@
3
3
  module Morpher
4
4
  # Generator for struct a-like wrappers
5
5
  class Record < Module
6
- include Concord.new(:attributes)
6
+ include Anima.new(:required, :optional)
7
7
 
8
+ # rubocop:disable Metrics/AbcSize
8
9
  # rubocop:disable Metrics/MethodLength
9
10
  def included(host)
10
- attributes = attributes()
11
- keys_transform = keys_transform()
11
+ optional = optional()
12
+ optional_transform = transform(optional)
13
+ required = required()
14
+ required_transform = transform(required)
12
15
 
13
16
  host.class_eval do
14
- include Adamantium::Flat, Anima.new(*attributes.keys)
17
+ include Adamantium::Flat, Anima.new(*(required.keys + optional.keys))
15
18
 
16
19
  const_set(
17
20
  :TRANSFORM,
@@ -19,18 +22,22 @@ module Morpher
19
22
  [
20
23
  Transform::Primitive.new(Hash),
21
24
  Transform::Hash::Symbolize.new,
22
- Transform::Hash.new(required: keys_transform, optional: []),
25
+ Transform::Hash.new(
26
+ required: required_transform,
27
+ optional: optional_transform
28
+ ),
23
29
  Transform::Success.new(public_method(:new))
24
30
  ]
25
31
  )
26
32
  )
27
33
  end
28
34
  end
35
+ # rubocop:enable Metrics/AbcSize
29
36
  # rubocop:enable Metrics/MethodLength
30
37
 
31
38
  private
32
39
 
33
- def keys_transform
40
+ def transform(attributes)
34
41
  attributes.map do |name, transform|
35
42
  Transform::Hash::Key.new(name, transform)
36
43
  end
@@ -350,11 +350,16 @@ module Morpher
350
350
  transform_keys(required, input)
351
351
  end
352
352
 
353
+ def defaults
354
+ optional.map(&:value).product([nil]).to_h
355
+ end
356
+ memoize :defaults
357
+
353
358
  def transform_optional(input)
354
359
  transform_keys(
355
360
  optional.select { |key| input.key?(key.value) },
356
361
  input
357
- )
362
+ ).fmap(&defaults.public_method(:merge))
358
363
  end
359
364
 
360
365
  # rubocop:disable Metrics/MethodLength
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: morpher
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Markus Schirp
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-07 00:00:00.000000000 Z
11
+ date: 2021-03-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: abstract_type