morpher 0.4.0 → 0.4.2

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: cc9380b32ac09b2e3765666a2c56baeb3aef272d22be1f7043f1977c9da1d815
4
- data.tar.gz: 0736e2646b93e59dc8fb2bf992bdf7729431260a08f3b158dd1eaf1324a657f1
3
+ metadata.gz: '0697df06da10720a25802c383e50cea61c7f7c86d24c46ad5e3e435fb00d8569'
4
+ data.tar.gz: 2f864673a64301e6827a35c1f6e570b253f40f475bc707c6bebd4004ed8f82b8
5
5
  SHA512:
6
- metadata.gz: 496748be0cfeae869808cb58aead93df98b270020b93310bba695ce03bea0cbc14b7451a881ce3b3e9280acbbf2155caaba7bfba827896bd79dd1119d9fa7fc0
7
- data.tar.gz: 0fc7f421fb376af18c2f1ea41eebf2b44350236e453a38878b1f015cb7cb15794d9e897de66c27c00143e1ce44b9271cc3f69be1ae79cb76f61f68a887e1c305
6
+ metadata.gz: 429cb71b2dffd2f59a584df47175986e7b9ab87ea1b69ef54e323506fa392d6ef8dea04325804e9ddd15e0ecd451ff81575e102c4bc6638229b173f3d0288b90
7
+ data.tar.gz: a0b532f79d366969453c9e9b62eb38bef741b8689f64b65b1e74562b70660f516a5afe4ddb8b674aa035166d4c10f4800af7ee1b2d198b414ebf8fd8a2bcf70f
@@ -3,8 +3,17 @@
3
3
  module Morpher
4
4
  # Generator for struct a-like wrappers
5
5
  class Record < Module
6
+ DEFAULTS = {
7
+ required: EMPTY_HASH,
8
+ optional: EMPTY_HASH
9
+ }.freeze
10
+
6
11
  include Anima.new(:required, :optional)
7
12
 
13
+ def self.new(**attributes)
14
+ super(DEFAULTS.merge(attributes))
15
+ end
16
+
8
17
  # rubocop:disable Metrics/AbcSize
9
18
  # rubocop:disable Metrics/MethodLength
10
19
  def included(host)
@@ -20,6 +20,36 @@ module Morpher
20
20
  # @return [Either<Error, Object>]
21
21
  abstract_method :call
22
22
 
23
+ # Build sequence
24
+ #
25
+ # @param [Transform] transform
26
+ #
27
+ # @return [Transform]
28
+ def seq(transform)
29
+ Sequence.new([self, transform])
30
+ end
31
+
32
+ # Build array transform
33
+ #
34
+ # @return [Transform]
35
+ def array
36
+ Array.new(self)
37
+ end
38
+
39
+ # Build maybe transform
40
+ #
41
+ # @return [Transform]
42
+ def maybe
43
+ Maybe.new(self)
44
+ end
45
+
46
+ # Build Proc to transform input
47
+ #
48
+ # @return [Proc]
49
+ def to_proc
50
+ public_method(:call).to_proc
51
+ end
52
+
23
53
  # Deep error data structure
24
54
  class Error
25
55
  include Anima.new(
@@ -59,13 +89,13 @@ module Morpher
59
89
 
60
90
  # Wrapper adding a name to a transformation
61
91
  class Named < self
62
- include Concord.new(:name, :transformer)
92
+ include Concord.new(:name, :transform)
63
93
 
64
94
  # Apply transformation to input
65
95
  #
66
96
  # @return [Either<Error, Object>]
67
97
  def call(input)
68
- transformer.call(input).lmap(&method(:wrap_error))
98
+ transform.call(input).lmap(&method(:wrap_error))
69
99
  end
70
100
 
71
101
  # Named slug
@@ -366,7 +396,7 @@ module Morpher
366
396
  def transform_keys(keys, input)
367
397
  success(
368
398
  keys
369
- .map do |key|
399
+ .to_h do |key|
370
400
  [
371
401
  key.value,
372
402
  coerce_key(key, input).from_right do |error|
@@ -374,7 +404,6 @@ module Morpher
374
404
  end
375
405
  ]
376
406
  end
377
- .to_h
378
407
  )
379
408
  end
380
409
  # rubocop:enable Metrics/MethodLength
@@ -419,6 +448,15 @@ module Morpher
419
448
  class Sequence < self
420
449
  include Concord.new(:steps)
421
450
 
451
+ # Build sequence
452
+ #
453
+ # @param [Transform] transform
454
+ #
455
+ # @return [Transform]
456
+ def seq(transform)
457
+ self.class.new(steps + [transform])
458
+ end
459
+
422
460
  # Apply transformation to input
423
461
  #
424
462
  # @param [Object]
@@ -437,7 +475,7 @@ module Morpher
437
475
  end
438
476
  end # Sequence
439
477
 
440
- # Generic exception transformer
478
+ # Generic exception transform
441
479
  class Exception < self
442
480
  include Concord.new(:error_class, :block)
443
481
 
@@ -467,6 +505,19 @@ module Morpher
467
505
  end
468
506
  end # Success
469
507
 
508
+ # Transform accepting nil values
509
+ class Maybe < Transform
510
+ include Concord.new(:transform)
511
+
512
+ def call(input)
513
+ if input.nil?
514
+ success(nil)
515
+ else
516
+ transform.call(input).lmap(&method(:wrap_error))
517
+ end
518
+ end
519
+ end # Maybe
520
+
470
521
  BOOLEAN = Transform::Boolean.new
471
522
  FLOAT = Transform::Primitive.new(Float)
472
523
  INTEGER = Transform::Primitive.new(Integer)
data/lib/morpher.rb CHANGED
@@ -8,6 +8,8 @@ require 'mprelude'
8
8
 
9
9
  module Morpher
10
10
  Either = MPrelude::Either
11
+
12
+ EMPTY_HASH = {}.freeze
11
13
  end
12
14
 
13
15
  require 'morpher/newtype'
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.4.0
4
+ version: 0.4.2
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-26 00:00:00.000000000 Z
11
+ date: 2022-11-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: abstract_type
@@ -52,20 +52,6 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: 0.3.0
55
- - !ruby/object:Gem::Dependency
56
- name: ast
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - "~>"
60
- - !ruby/object:Gem::Version
61
- version: '2.2'
62
- type: :runtime
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - "~>"
67
- - !ruby/object:Gem::Version
68
- version: '2.2'
69
55
  - !ruby/object:Gem::Dependency
70
56
  name: concord
71
57
  requirement: !ruby/object:Gem::Requirement
@@ -94,20 +80,6 @@ dependencies:
94
80
  - - "~>"
95
81
  - !ruby/object:Gem::Version
96
82
  version: 0.0.9
97
- - !ruby/object:Gem::Dependency
98
- name: ice_nine
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - "~>"
102
- - !ruby/object:Gem::Version
103
- version: 0.11.0
104
- type: :runtime
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - "~>"
109
- - !ruby/object:Gem::Version
110
- version: 0.11.0
111
83
  - !ruby/object:Gem::Dependency
112
84
  name: mprelude
113
85
  requirement: !ruby/object:Gem::Requirement
@@ -235,7 +207,8 @@ files:
235
207
  homepage: https://github.com/mbj/morpher
236
208
  licenses:
237
209
  - MIT
238
- metadata: {}
210
+ metadata:
211
+ rubygems_mfa_required: 'true'
239
212
  post_install_message:
240
213
  rdoc_options: []
241
214
  require_paths:
@@ -244,14 +217,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
244
217
  requirements:
245
218
  - - ">="
246
219
  - !ruby/object:Gem::Version
247
- version: '2.5'
220
+ version: '2.6'
248
221
  required_rubygems_version: !ruby/object:Gem::Requirement
249
222
  requirements:
250
223
  - - ">="
251
224
  - !ruby/object:Gem::Version
252
225
  version: '0'
253
226
  requirements: []
254
- rubygems_version: 3.1.4
227
+ rubygems_version: 3.3.7
255
228
  signing_key:
256
229
  specification_version: 4
257
230
  summary: Domain Transformation Algebra