datory 1.0.0.rc13 → 1.0.0.rc15
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/README.md +14 -2
- data/lib/datory/attributes/form.rb +78 -0
- data/lib/datory/context/callable.rb +4 -0
- data/lib/datory/exceptions.rb +2 -0
- data/lib/datory/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7cf1723be06fbf81937c9e8e9b20cf1e37f3d70765dbfde943fc0e3640ab3c13
|
4
|
+
data.tar.gz: 90f48cba076d54721d8c03858016326a87c4b6c3dea2bcbabdff17ac7c69677e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '0794e8bf0e3bbf898c595199207ecb6f833ab84b43450a493339c7a27f87577c3a4fdb43d48e2ab21f8a93c7cf49fdd55ca0e4182d18c89ad0a66bb613a2d0a5'
|
7
|
+
data.tar.gz: 8571cd2fbff8d5afb5b81fc68a8ae37c5d8ecb3f387037afdaddd61b0bad8974d0198d3c85750f028b9fd7a57e7b66d9a92bc3e0fd19849fc7bd6ab547c45237
|
data/README.md
CHANGED
@@ -22,13 +22,25 @@ gem "datory"
|
|
22
22
|
```ruby
|
23
23
|
user = User.find(...)
|
24
24
|
|
25
|
-
UserDto.serialize(user)
|
25
|
+
UserDto.serialize(user) # => { ... }
|
26
|
+
```
|
27
|
+
|
28
|
+
For serialization, the `form` method is also available.
|
29
|
+
This prepares a `Form` object, which has a set of additional methods such as `valid?` and `invalid?`.
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
form = UserDto.form(user)
|
33
|
+
|
34
|
+
form.valid? # => true
|
35
|
+
form.invalid? # => false
|
36
|
+
|
37
|
+
form.serialize # => { ... }
|
26
38
|
```
|
27
39
|
|
28
40
|
#### Deserialize
|
29
41
|
|
30
42
|
```ruby
|
31
|
-
UserDto.deserialize(json)
|
43
|
+
UserDto.deserialize(json) # => Datory::Result
|
32
44
|
```
|
33
45
|
|
34
46
|
#### Examples
|
@@ -0,0 +1,78 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Datory
|
4
|
+
module Attributes
|
5
|
+
class Form
|
6
|
+
attr_reader :model
|
7
|
+
|
8
|
+
def initialize(context, model)
|
9
|
+
@context = context
|
10
|
+
@model = model
|
11
|
+
end
|
12
|
+
|
13
|
+
def target
|
14
|
+
@context
|
15
|
+
end
|
16
|
+
|
17
|
+
def update(**attributes)
|
18
|
+
if model_collection?
|
19
|
+
raise_misuse "The `update` method cannot be used with a collection. Instead, use the `update_by` method."
|
20
|
+
end
|
21
|
+
|
22
|
+
found_keys = model.keys & attributes.keys
|
23
|
+
|
24
|
+
reset!
|
25
|
+
|
26
|
+
model.merge!(attributes.slice(*found_keys))
|
27
|
+
end
|
28
|
+
|
29
|
+
def update_by(index, **attributes) # rubocop:disable Metrics/MethodLength
|
30
|
+
unless model_collection?
|
31
|
+
raise_misuse "The `update_by` method cannot be used without a collection. Instead, use the `update` method."
|
32
|
+
end
|
33
|
+
|
34
|
+
reset!
|
35
|
+
|
36
|
+
model.map!.with_index do |model_item, model_index|
|
37
|
+
if model_index == index
|
38
|
+
found_keys = model_item.keys & attributes.keys
|
39
|
+
|
40
|
+
model_item.merge(attributes.slice(*found_keys))
|
41
|
+
else
|
42
|
+
model_item
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def serialize
|
48
|
+
@serialize ||= @context.serialize(model)
|
49
|
+
end
|
50
|
+
|
51
|
+
def valid?
|
52
|
+
serialize
|
53
|
+
|
54
|
+
true
|
55
|
+
rescue Datory::Exceptions::SerializationError
|
56
|
+
false
|
57
|
+
end
|
58
|
+
|
59
|
+
def invalid?
|
60
|
+
!valid?
|
61
|
+
end
|
62
|
+
|
63
|
+
private
|
64
|
+
|
65
|
+
def raise_misuse(message)
|
66
|
+
raise Datory::Exceptions::MisuseError, message
|
67
|
+
end
|
68
|
+
|
69
|
+
def model_collection?
|
70
|
+
[Set, Array].include?(model.class)
|
71
|
+
end
|
72
|
+
|
73
|
+
def reset!
|
74
|
+
@serialize = nil
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
data/lib/datory/exceptions.rb
CHANGED
data/lib/datory/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: datory
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.0.
|
4
|
+
version: 1.0.0.rc15
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anton Sokolov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-04-
|
11
|
+
date: 2024-04-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -216,6 +216,7 @@ files:
|
|
216
216
|
- lib/datory/attributes/deserialization/service_builder.rb
|
217
217
|
- lib/datory/attributes/deserialization/service_factory.rb
|
218
218
|
- lib/datory/attributes/dsl.rb
|
219
|
+
- lib/datory/attributes/form.rb
|
219
220
|
- lib/datory/attributes/options/base.rb
|
220
221
|
- lib/datory/attributes/options/from.rb
|
221
222
|
- lib/datory/attributes/options/to.rb
|