datory 1.0.0.rc14 → 1.0.0.rc16
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +11 -6
- data/lib/datory/attributes/form.rb +51 -1
- data/lib/datory/exceptions.rb +2 -0
- data/lib/datory/version.rb +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 201a53f5b4ca79442fa45bbd224c13028cc9d0d23fe1c9648d0e5c847af9bc12
|
4
|
+
data.tar.gz: 5e89ad33bbbe3a5de586c4c1387782309f104fd01a1ee9a6bba97f55492e786b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8271c50fce2d564962ae13dc97c4cea02a089c77317767065cfef7b912aa56732e967206f8e56d103d21b2395ffc6477a99587a8a23aee09f23be07c797c31fc
|
7
|
+
data.tar.gz: 986822851f48f25085bfe2c5828b85bea56490e0874b699fbeb1808c15b7bedcb04b819c15ae355e1b2de10a1e3aaeeb0fedd3ff4e0538fa6628df929a4b2817
|
data/README.md
CHANGED
@@ -25,24 +25,29 @@ user = User.find(...)
|
|
25
25
|
UserDto.serialize(user) # => { ... }
|
26
26
|
```
|
27
27
|
|
28
|
+
#### Deserialize
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
UserDto.deserialize(json) # => Datory::Result
|
32
|
+
```
|
33
|
+
|
34
|
+
#### Form
|
35
|
+
|
28
36
|
For serialization, the `form` method is also available.
|
29
37
|
This prepares a `Form` object, which has a set of additional methods such as `valid?` and `invalid?`.
|
30
38
|
|
31
39
|
```ruby
|
32
40
|
form = UserDto.form(user)
|
33
41
|
|
42
|
+
form.target # => UserDto
|
43
|
+
form.model # => { ... }
|
44
|
+
|
34
45
|
form.valid? # => true
|
35
46
|
form.invalid? # => false
|
36
47
|
|
37
48
|
form.serialize # => { ... }
|
38
49
|
```
|
39
50
|
|
40
|
-
#### Deserialize
|
41
|
-
|
42
|
-
```ruby
|
43
|
-
UserDto.deserialize(json) # => Datory::Result
|
44
|
-
```
|
45
|
-
|
46
51
|
#### Examples
|
47
52
|
|
48
53
|
```ruby
|
@@ -3,13 +3,49 @@
|
|
3
3
|
module Datory
|
4
4
|
module Attributes
|
5
5
|
class Form
|
6
|
+
attr_reader :model
|
7
|
+
|
6
8
|
def initialize(context, model)
|
7
9
|
@context = context
|
8
10
|
@model = model
|
9
11
|
end
|
10
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
|
+
|
11
47
|
def serialize
|
12
|
-
@serialize ||= @context.serialize(
|
48
|
+
@serialize ||= @context.serialize(model)
|
13
49
|
end
|
14
50
|
|
15
51
|
def valid?
|
@@ -23,6 +59,20 @@ module Datory
|
|
23
59
|
def invalid?
|
24
60
|
!valid?
|
25
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
|
26
76
|
end
|
27
77
|
end
|
28
78
|
end
|
data/lib/datory/exceptions.rb
CHANGED
data/lib/datory/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.rc16
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Anton Sokolov
|
@@ -48,16 +48,16 @@ dependencies:
|
|
48
48
|
name: servactory
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|
50
50
|
requirements:
|
51
|
-
- -
|
51
|
+
- - ">="
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version: 2.5.0
|
53
|
+
version: 2.5.0
|
54
54
|
type: :runtime
|
55
55
|
prerelease: false
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
57
57
|
requirements:
|
58
|
-
- -
|
58
|
+
- - ">="
|
59
59
|
- !ruby/object:Gem::Version
|
60
|
-
version: 2.5.0
|
60
|
+
version: 2.5.0
|
61
61
|
- !ruby/object:Gem::Dependency
|
62
62
|
name: terminal-table
|
63
63
|
requirement: !ruby/object:Gem::Requirement
|