datory 1.0.0.rc1 → 1.0.0.rc2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +49 -1
- data/lib/datory/attributes/collection.rb +1 -13
- data/lib/datory/context/callable.rb +35 -25
- data/lib/datory/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dad592cd7b60ed42e5e729fc785dfcb148abff2e4966194563d943f9e3f66f60
|
4
|
+
data.tar.gz: 9bffee76ef163ac26529c4a163afa2e1a9cea42da6619381b85d7cd2b5187290
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe6407e48e97f7e2f462e52cc7aba7a1726171f02e9fcd1d774bb36c9b04047abdf7218a3eede1b99023fe38f16d80b7bbec224313ffc229e3695b8bdc6126e3
|
7
|
+
data.tar.gz: ae3835273961a84951d232afccf50851cade4f9efb5f89c44d8d7cdf55b3804ff6ba983580d4025aa4f6fd28fd4f695d1941fb74833015d3a88852712d4332fa
|
data/README.md
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
<a href="https://github.com/servactory/datory/releases"><img src="https://img.shields.io/github/release-date/servactory/datory" alt="Release Date"></a>
|
4
4
|
</p>
|
5
5
|
|
6
|
-
## Documentation
|
6
|
+
## Documentation (soon)
|
7
7
|
|
8
8
|
See [datory.servactory.com](https://datory.servactory.com) for documentation.
|
9
9
|
|
@@ -15,6 +15,54 @@ See [datory.servactory.com](https://datory.servactory.com) for documentation.
|
|
15
15
|
gem "datory"
|
16
16
|
```
|
17
17
|
|
18
|
+
### Usage
|
19
|
+
|
20
|
+
#### Serialize
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
user = User.find(...)
|
24
|
+
|
25
|
+
UserDto.serialize(user)
|
26
|
+
```
|
27
|
+
|
28
|
+
#### Deserialize
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
UserDto.deserialize(json)
|
32
|
+
```
|
33
|
+
|
34
|
+
#### Examples
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
class UserDto < Datory::Base
|
38
|
+
string :id
|
39
|
+
string :firstname, as: :first_name
|
40
|
+
string :lastname, as: :last_name
|
41
|
+
string :email
|
42
|
+
string :birthDate, as: :birth_date, output: ->(value:) { value.to_s }
|
43
|
+
|
44
|
+
one :login, include: UserLoginDto
|
45
|
+
|
46
|
+
many :addresses, include: UserAddressDto
|
47
|
+
|
48
|
+
string :phone
|
49
|
+
string :website
|
50
|
+
|
51
|
+
one :company, include: UserCompanyDto
|
52
|
+
end
|
53
|
+
```
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
class UserLoginDto < Datory::Base
|
57
|
+
string :uuid
|
58
|
+
string :username
|
59
|
+
string :password
|
60
|
+
string :md5
|
61
|
+
string :sha1
|
62
|
+
string :registered
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
18
66
|
## Contributing
|
19
67
|
|
20
68
|
This project is intended to be a safe, welcoming space for collaboration.
|
@@ -4,27 +4,15 @@ module Datory
|
|
4
4
|
module Attributes
|
5
5
|
class Collection
|
6
6
|
extend Forwardable
|
7
|
-
def_delegators :@collection, :<<, :each, :map, :
|
7
|
+
def_delegators :@collection, :<<, :each, :map, :to_h, :merge
|
8
8
|
|
9
9
|
def initialize(collection = Set.new)
|
10
10
|
@collection = collection
|
11
11
|
end
|
12
12
|
|
13
|
-
# def only(*names)
|
14
|
-
# Collection.new(filter { |input| names.include?(input.internal_name) })
|
15
|
-
# end
|
16
|
-
|
17
|
-
# def except(*names)
|
18
|
-
# Collection.new(filter { |input| !names.include?(input.internal_name) })
|
19
|
-
# end
|
20
|
-
|
21
13
|
def names
|
22
14
|
map(&:name)
|
23
15
|
end
|
24
|
-
|
25
|
-
# def find_by(name:)
|
26
|
-
# find { |input| input.internal_name == name }
|
27
|
-
# end
|
28
16
|
end
|
29
17
|
end
|
30
18
|
end
|
@@ -3,42 +3,52 @@
|
|
3
3
|
module Datory
|
4
4
|
module Context
|
5
5
|
module Callable
|
6
|
+
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
6
7
|
def serialize(model)
|
7
|
-
|
8
|
+
if [Set, Array].include?(model.class)
|
9
|
+
model.map do |model_item|
|
10
|
+
serialize(model_item)
|
11
|
+
end
|
12
|
+
else
|
13
|
+
collection_of_attributes.to_h do |attribute|
|
14
|
+
internal_name = attribute.options.fetch(:as, attribute.name)
|
15
|
+
include_class = attribute.options.fetch(:include, nil)
|
16
|
+
output_formatter = attribute.options.fetch(:output, nil)
|
8
17
|
|
9
|
-
|
10
|
-
internal_name = attribute.options.fetch(:as, attribute.name)
|
11
|
-
include_class = attribute.options.fetch(:include, nil)
|
12
|
-
output_formatter = attribute.options.fetch(:output, nil)
|
18
|
+
value = model.public_send(internal_name)
|
13
19
|
|
14
|
-
|
20
|
+
value =
|
21
|
+
if include_class.present?
|
22
|
+
type = attribute.options.fetch(:type, nil)
|
15
23
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
24
|
+
if [Set, Array].include?(type)
|
25
|
+
value.map { |item| include_class.serialize(item) }
|
26
|
+
else
|
27
|
+
include_class.serialize(value)
|
28
|
+
end
|
29
|
+
elsif output_formatter.is_a?(Proc)
|
30
|
+
output_formatter.call(value: value)
|
31
|
+
elsif [Date, Time, DateTime].include?(value.class)
|
32
|
+
value.to_s
|
22
33
|
else
|
23
|
-
|
34
|
+
value
|
24
35
|
end
|
25
|
-
elsif output_formatter.is_a?(Proc)
|
26
|
-
output_formatter.call(value: value)
|
27
|
-
elsif [Date, Time, DateTime].include?(value.class)
|
28
|
-
value.to_s
|
29
|
-
else
|
30
|
-
value
|
31
|
-
end
|
32
36
|
|
33
|
-
|
37
|
+
[attribute.name, value]
|
38
|
+
end
|
34
39
|
end
|
35
|
-
|
36
|
-
hash
|
37
40
|
end
|
41
|
+
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
38
42
|
|
39
43
|
def deserialize(json)
|
40
|
-
|
41
|
-
|
44
|
+
if [Set, Array].include?(json.class)
|
45
|
+
json.map do |json_item|
|
46
|
+
deserialize(json_item)
|
47
|
+
end
|
48
|
+
else
|
49
|
+
hash = JSON.parse(json.to_json)
|
50
|
+
build(**hash)
|
51
|
+
end
|
42
52
|
end
|
43
53
|
|
44
54
|
# def build!(attributes = {})
|
data/lib/datory/version.rb
CHANGED