datory 1.0.0.rc1 → 1.0.0.rc2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9a373fdef28cc23b1fede6c3c3405e92ca888b832792122bde0c9a2758aab365
4
- data.tar.gz: 962d0fc90e6a130d249800d982176ece5b7e85226979e4e6a94c2a306b1b7a4d
3
+ metadata.gz: dad592cd7b60ed42e5e729fc785dfcb148abff2e4966194563d943f9e3f66f60
4
+ data.tar.gz: 9bffee76ef163ac26529c4a163afa2e1a9cea42da6619381b85d7cd2b5187290
5
5
  SHA512:
6
- metadata.gz: 947b785c440d6fef3672dc82cb25eb8c59e256f881974f32dc50673fe626274d0723b0b8e09c1c24ba4c6714b77eabfbf0a83a3399794dd41b8ef2039426ec3c
7
- data.tar.gz: 9984cdcd0c95f0b3e5de163fe84b46e2275ce7e61c6764fcfcb38e0696832d9922f8313108ec2ae6505a18543ca36ac8c8a87d67cfa4d2f205cd41953aabc59b
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, :merge # :filter, :each, :map, :flat_map, :to_h, :merge, :find
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
- hash = {}
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
- collection_of_attributes.each do |attribute|
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
- value = model.public_send(internal_name)
20
+ value =
21
+ if include_class.present?
22
+ type = attribute.options.fetch(:type, nil)
15
23
 
16
- value =
17
- if include_class.present?
18
- type = attribute.options.fetch(:type, nil)
19
-
20
- if [Set, Array].include?(type)
21
- value.map { |item| include_class.serialize(item) }
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
- include_class.serialize(value)
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
- hash[attribute.name] = value
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
- hash = JSON.parse(json.to_json)
41
- build(**hash)
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 = {})
@@ -5,7 +5,7 @@ module Datory
5
5
  MAJOR = 1
6
6
  MINOR = 0
7
7
  PATCH = 0
8
- PRE = "rc1"
8
+ PRE = "rc2"
9
9
 
10
10
  STRING = [MAJOR, MINOR, PATCH, PRE].compact.join(".")
11
11
  end
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.rc1
4
+ version: 1.0.0.rc2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton Sokolov