datory 1.0.0.rc12 → 1.0.0.rc13

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: 198bd72a333d4fa2fe2ee18935d586ab10f6185c7b51c6cb94f2627f5698fb52
4
- data.tar.gz: 6aa7c4bf85587d02a38e79de2135c83bed01be5153a644c0892433887dd1b982
3
+ metadata.gz: ad1cc00d4916767ce4e99d5cd52cde2f9640c4ea536f602e0ade8e5008b61820
4
+ data.tar.gz: 012e57a2c905b6de960fb1cd32e1719f1f876c630cc77b37ea7e8ee31427dae8
5
5
  SHA512:
6
- metadata.gz: ed216465c59651df53b3170a9381a058a26b5dcaa48058b20300eae6111f9ef582b198391f3813f49e886759c18844a621c4b1a77d9d4a131080fda9ed2e5608
7
- data.tar.gz: df02fd6e1eabc40683e506052d448aa9f563f857a52fc48972482b30e962a6571801f9c816ca805bbe1fed8b1acf4c8310d6784374281ecfeb075c1d182480ab
6
+ metadata.gz: 271166881cb4e4e5b673d05ed180b2a09e0523aab2739d9c8127894981b34d5d37486a74aaa029524f13dbb03206dbf924c96f7983f28a985738e41ae96b64f2
7
+ data.tar.gz: 0b5a6b6cbbf3db6f505ebf629ff33e2bd57f72d7840295cc35660fcba5f8ee497db538fb699b75a8db2676f6064bc67aa0533c78049dd958cb1a1e0573b6e559
data/README.md CHANGED
@@ -44,7 +44,7 @@ class UserDto < Datory::Base
44
44
  string :phone
45
45
  string :website
46
46
 
47
- string :birthDate, to: :birth_date, as: Date
47
+ date :birthDate, to: :birth_date
48
48
 
49
49
  one :login, include: UserLoginDto
50
50
  one :company, include: UserCompanyDto
@@ -62,8 +62,10 @@ class UserLoginDto < Datory::Base
62
62
 
63
63
  string :md5
64
64
  string :sha1
65
+
66
+ duration :lifetime
65
67
 
66
- string :registered_at, as: DateTime
68
+ datetime :registered_at
67
69
  end
68
70
  ```
69
71
 
@@ -160,7 +162,7 @@ UserDto.info
160
162
  ```
161
163
 
162
164
  ```
163
- #<Datory::Info::Result:0x00000001069c45d0 @attributes={:id=>{:from=>String, :to=>:id, :as=>String, :include=>nil}, :firstname=>{:from=>String, :to=>:first_name, :as=>String, :include=>nil}, :lastname=>{:from=>String, :to=>:last_name, :as=>String, :include=>nil}, :email=>{:from=>String, :to=>:email, :as=>String, :include=>nil}, :phone=>{:from=>String, :to=>:phone, :as=>String, :include=>nil}, :website=>{:from=>String, :to=>:website, :as=>String, :include=>nil}, :birthDate=>{:from=>String, :to=>:birth_date, :as=>Date, :include=>nil}, :login=>{:from=>Hash, :to=>:login, :as=>[Datory::Result, Hash], :include=>Usual::Example1::UserLogin}, :company=>{:from=>Hash, :to=>:company, :as=>[Datory::Result, Hash], :include=>Usual::Example1::UserCompany}, :addresses=>{:from=>Array, :to=>:addresses, :as=>Array, :include=>Usual::Example1::UserAddress}}>
165
+ #<Datory::Info::Result:0x000000011eecd7d0 @attributes={:id=>{:from=>{:name=>:id, :type=>String, :min=>nil, :max=>nil, :consists_of=>false, :format=>:uuid}, :to=>{:name=>:id, :type=>String, :min=>nil, :max=>nil, :consists_of=>false, :format=>:uuid, :required=>true, :include=>nil}}, :firstname=>{:from=>{:name=>:firstname, :type=>String, :min=>nil, :max=>nil, :consists_of=>false, :format=>nil}, :to=>{:name=>:first_name, :type=>String, :min=>nil, :max=>nil, :consists_of=>false, :format=>nil, :required=>true, :include=>nil}}, :lastname=>{:from=>{:name=>:lastname, :type=>String, :min=>nil, :max=>nil, :consists_of=>false, :format=>nil}, :to=>{:name=>:last_name, :type=>String, :min=>nil, :max=>nil, :consists_of=>false, :format=>nil, :required=>true, :include=>nil}}, :email=>{:from=>{:name=>:email, :type=>String, :min=>nil, :max=>nil, :consists_of=>false, :format=>nil}, :to=>{:name=>:email, :type=>String, :min=>nil, :max=>nil, :consists_of=>false, :format=>nil, :required=>true, :include=>nil}}, :phone=>{:from=>{:name=>:phone, :type=>String, :min=>nil, :max=>nil, :consists_of=>false, :format=>nil}, :to=>{:name=>:phone, :type=>String, :min=>nil, :max=>nil, :consists_of=>false, :format=>nil, :required=>true, :include=>nil}}, :website=>{:from=>{:name=>:website, :type=>String, :min=>nil, :max=>nil, :consists_of=>false, :format=>nil}, :to=>{:name=>:website, :type=>String, :min=>nil, :max=>nil, :consists_of=>false, :format=>nil, :required=>true, :include=>nil}}, :birthDate=>{:from=>{:name=>:birthDate, :type=>String, :min=>nil, :max=>nil, :consists_of=>false, :format=>:date}, :to=>{:name=>:birth_date, :type=>Date, :min=>nil, :max=>nil, :consists_of=>false, :format=>nil, :required=>true, :include=>nil}}, :login=>{:from=>{:name=>:login, :type=>Hash, :min=>nil, :max=>nil, :consists_of=>false, :format=>nil}, :to=>{:name=>:login, :type=>[Datory::Result, Hash], :min=>nil, :max=>nil, :consists_of=>false, :format=>nil, :required=>true, :include=>Usual::Example1::UserLogin}}, :company=>{:from=>{:name=>:company, :type=>Hash, :min=>nil, :max=>nil, :consists_of=>false, :format=>nil}, :to=>{:name=>:company, :type=>[Datory::Result, Hash], :min=>nil, :max=>nil, :consists_of=>false, :format=>nil, :required=>true, :include=>Usual::Example1::UserCompany}}, :addresses=>{:from=>{:name=>:addresses, :type=>Array, :min=>nil, :max=>nil, :consists_of=>[Datory::Result, Hash], :format=>nil}, :to=>{:name=>:addresses, :type=>Array, :min=>nil, :max=>nil, :consists_of=>[Datory::Result, Hash], :format=>nil, :required=>true, :include=>Usual::Example1::UserAddress}}}>
164
166
  ```
165
167
 
166
168
  ### Describe
@@ -2,130 +2,105 @@
2
2
 
3
3
  module Datory
4
4
  module Attributes
5
- class Attribute # rubocop:disable Metrics/ClassLength
6
- attr_reader :name, :options
5
+ class Attribute
6
+ attr_reader :from, :to
7
7
 
8
- def initialize(name, **options)
9
- @name = name
10
- @options = options
8
+ def initialize(name, **options) # rubocop:disable Metrics/MethodLength
9
+ @from = Options::From.new(
10
+ name: name,
11
+ type: options.fetch(:from),
12
+ consists_of: options.fetch(:consists_of, false),
13
+ min: options.fetch(:min, nil),
14
+ max: options.fetch(:max, nil),
15
+ format: options.fetch(:format, nil)
16
+ )
17
+
18
+ @to = Options::To.new(
19
+ name: options.fetch(:to, name),
20
+ type: options.fetch(:as, @from.type),
21
+ # TODO: It is necessary to implement NilClass support for optional
22
+ required: options.fetch(:required, true),
23
+ consists_of: @from.consists_of,
24
+ min: @from.min,
25
+ max: @from.max,
26
+ format: options.fetch(:format, nil),
27
+ include_class: options.fetch(:include, nil)
28
+ )
11
29
  end
12
30
 
13
- def input_serialization_options # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
31
+ ##########################################################################
32
+
33
+ def input_serialization_options # rubocop:disable Metrics/AbcSize
14
34
  hash = {
15
- as: options.fetch(:to, name),
16
- type: options.fetch(:as, options.fetch(:from)),
17
- required: options.fetch(:required, true),
18
- consists_of: options.fetch(:consists_of, false)
35
+ as: to.name,
36
+ type: to.type,
37
+ required: to.required,
38
+ consists_of: to.consists_of
19
39
  }
20
40
 
21
- if (min = options.fetch(:min, nil)).present?
22
- hash[:min] = min
23
- end
41
+ hash[:min] = to.min if to.min.present?
24
42
 
25
- if (max = options.fetch(:max, nil)).present?
26
- hash[:max] = max
27
- end
43
+ hash[:max] = to.max if to.max.present?
28
44
 
29
- if (format = options.fetch(:format, nil)).present?
30
- hash[:format] = format
31
- end
45
+ hash[:format] = to.format if to.format.present?
32
46
 
33
47
  hash
34
48
  end
35
49
 
36
- def input_deserialization_options # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
50
+ def output_serialization_options # rubocop:disable Metrics/AbcSize
37
51
  hash = {
38
- as: options.fetch(:to, name),
39
- type: options.fetch(:from),
40
- required: options.fetch(:required, true),
41
- consists_of: options.fetch(:consists_of, false),
42
- prepare: (lambda do |value:|
43
- include_class = options.fetch(:include, nil)
44
- return value unless include_class.present?
45
-
46
- from_type = options.fetch(:from, nil)
47
-
48
- if [Set, Array].include?(from_type)
49
- value.map { |item| include_class.deserialize(**item) }
50
- else
51
- include_class.deserialize(**value)
52
- end
53
- end)
52
+ consists_of: to.consists_of == Hash ? Datory::Result : from.consists_of,
53
+ type: to.type == Datory::Result ? Hash : from.type
54
54
  }
55
55
 
56
- if (min = options.fetch(:min, nil)).present?
57
- hash[:min] = min
58
- end
56
+ hash[:min] = from.min if from.min.present?
59
57
 
60
- if (max = options.fetch(:max, nil)).present?
61
- hash[:max] = max
62
- end
58
+ hash[:max] = from.max if from.max.present?
63
59
 
64
- if (format = options.fetch(:format, nil)).present?
65
- hash[:format] = format
66
- end
60
+ hash[:format] = from.format if from.format.present?
67
61
 
68
62
  hash
69
63
  end
70
64
 
71
- def output_serialization_options # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/PerceivedComplexity
65
+ ##########################################################################
66
+
67
+ def input_deserialization_options # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
72
68
  hash = {
73
- consists_of: if (consists_of_type = options.fetch(:consists_of, false)) == Hash
74
- Datory::Result
75
- else
76
- consists_of_type
77
- end,
78
- type: if (as_type = options.fetch(:as, options.fetch(:from))) == Datory::Result
79
- Hash
80
- elsif (from_type = options.fetch(:from)).present?
81
- from_type
82
- else
83
- as_type
84
- end
69
+ as: to.name,
70
+ type: from.type,
71
+ required: to.required,
72
+ consists_of: from.consists_of,
73
+ prepare: (lambda do |value:|
74
+ return value unless to.include_class.present?
75
+
76
+ if [Set, Array].include?(from.type)
77
+ value.map { |item| to.include_class.deserialize(**item) }
78
+ else
79
+ to.include_class.deserialize(**value)
80
+ end
81
+ end)
85
82
  }
86
83
 
87
- if (min = options.fetch(:min, nil)).present?
88
- hash[:min] = min
89
- end
84
+ hash[:min] = from.min if from.min.present?
90
85
 
91
- if (max = options.fetch(:max, nil)).present?
92
- hash[:max] = max
93
- end
86
+ hash[:max] = from.max if from.max.present?
94
87
 
95
- if (format = options.fetch(:format, nil)).present?
96
- hash[:format] = format
97
- end
88
+ hash[:format] = from.format if from.format.present?
98
89
 
99
90
  hash
100
91
  end
101
92
 
102
- def output_deserialization_options # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/PerceivedComplexity
93
+ def output_deserialization_options # rubocop:disable Metrics/AbcSize
103
94
  hash = {
104
- consists_of: if (consists_of_type = options.fetch(:consists_of, false)) == Hash
105
- Datory::Result
106
- else
107
- consists_of_type
108
- end,
109
- type: if (from_type = options.fetch(:from)) == Hash
110
- Datory::Result
111
- elsif (option_as = options.fetch(:as, nil)).present?
112
- option_as
113
- else
114
- from_type
115
- end
95
+ consists_of: from.consists_of == Hash ? Datory::Result : to.consists_of,
96
+ type: from.type == Hash ? Datory::Result : to.type
116
97
  }
117
98
 
118
- if (min = options.fetch(:min, nil)).present?
119
- hash[:min] = min
120
- end
99
+ hash[:min] = to.min if to.min.present?
121
100
 
122
- if (max = options.fetch(:max, nil)).present?
123
- hash[:max] = max
124
- end
101
+ hash[:max] = to.max if to.max.present?
125
102
 
126
- if (format = options.fetch(:format, nil)).present?
127
- hash[:format] = format
128
- end
103
+ hash[:format] = to.format if to.format.present?
129
104
 
130
105
  hash
131
106
  end
@@ -15,12 +15,12 @@ module Datory
15
15
  end
16
16
 
17
17
  def internal_names
18
- map { |attribute| attribute.options.fetch(:to, attribute.name) }
18
+ map { |attribute| attribute.to.name }
19
19
  end
20
20
 
21
- def include_exist?
22
- @include_exist ||= filter do |attribute| # rubocop:disable Performance/Count
23
- include_class = attribute.options.fetch(:include, nil)
21
+ def include_class_exist?
22
+ @include_class_exist ||= filter do |attribute| # rubocop:disable Performance/Count
23
+ include_class = attribute.to.include_class
24
24
 
25
25
  next false if include_class.nil?
26
26
 
@@ -15,21 +15,18 @@ module Datory
15
15
  headings << "From"
16
16
  headings << "To"
17
17
  headings << "As"
18
- headings << "Include" if collection_of_attributes.include_exist?
18
+ headings << "Include" if collection_of_attributes.include_class_exist?
19
19
 
20
20
  collection_of_attributes.each do |attribute|
21
21
  row = []
22
22
 
23
- row << attribute.name
23
+ include_class = attribute.to.include_class.presence || attribute.from.type
24
24
 
25
- from_type = attribute.options.fetch(:from)
26
- include = attribute.options.fetch(:include, from_type)
27
-
28
- row << from_type
29
- row << attribute.options.fetch(:to, attribute.name)
30
- row << attribute.options.fetch(:as, include)
31
-
32
- row << (include if include <= Datory::Base) if collection_of_attributes.include_exist?
25
+ row << attribute.from.name
26
+ row << attribute.from.type
27
+ row << attribute.to.name
28
+ row << attribute.to.type
29
+ row << (include_class if include_class <= Datory::Base) if collection_of_attributes.include_class_exist?
33
30
 
34
31
  rows << row
35
32
  end
@@ -60,22 +60,22 @@ module Datory
60
60
  end
61
61
 
62
62
  def duration(name, **options)
63
- options = options.merge(from: String, as: ActiveSupport::Duration) # TODO: Add `format: :duration`
63
+ options = options.merge(from: String, as: ActiveSupport::Duration, format: { from: :duration })
64
64
  string(name, **options)
65
65
  end
66
66
 
67
67
  def date(name, **options)
68
- options = options.merge(from: String, as: Date) # TODO: Add `format: :date`
68
+ options = options.merge(from: String, as: Date, format: { from: :date })
69
69
  string(name, **options)
70
70
  end
71
71
 
72
72
  def time(name, **options)
73
- options = options.merge(from: String, as: Time) # TODO: Add `format: :time`
73
+ options = options.merge(from: String, as: Time, format: { from: :time })
74
74
  string(name, **options)
75
75
  end
76
76
 
77
77
  def datetime(name, **options)
78
- options = options.merge(from: String, as: DateTime) # TODO: Add `format: :datetime`
78
+ options = options.merge(from: String, as: DateTime, format: { from: :datetime })
79
79
  string(name, **options)
80
80
  end
81
81
 
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datory
4
+ module Attributes
5
+ module Options
6
+ class Base
7
+ attr_reader :name, :type, :consists_of, :min, :max, :format
8
+
9
+ def initialize(name:, type:, consists_of:, min:, max:, format:)
10
+ @name = name
11
+ @type = type
12
+ @consists_of = consists_of
13
+ @min = min
14
+ @max = max
15
+ @format = format
16
+ end
17
+
18
+ def info
19
+ {
20
+ name: name,
21
+ type: type,
22
+ min: min,
23
+ max: max,
24
+ consists_of: consists_of,
25
+ format: format
26
+ }
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datory
4
+ module Attributes
5
+ module Options
6
+ class From < Base
7
+ def initialize(name:, type:, consists_of:, min:, max:, format:)
8
+ format = format.fetch(:from, nil) if format.is_a?(Hash)
9
+
10
+ super(name: name, type: type, consists_of: consists_of, min: min, max: max, format: format)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datory
4
+ module Attributes
5
+ module Options
6
+ class To < Base
7
+ attr_reader :required, :include_class
8
+
9
+ def initialize(name:, type:, required:, consists_of:, min:, max:, format:, include_class:)
10
+ @required = required
11
+ @include_class = include_class
12
+
13
+ format = format.fetch(:to, nil) if format.is_a?(Hash)
14
+
15
+ super(name: name, type: type, consists_of: consists_of, min: min, max: max, format: format)
16
+ end
17
+
18
+ def info
19
+ super.merge(
20
+ required: required,
21
+ include: include_class
22
+ )
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -20,23 +20,20 @@ module Datory
20
20
  end
21
21
  else
22
22
  @collection_of_attributes.to_h do |attribute|
23
- attribute.options.fetch(:to, attribute.name)
24
- include_class = attribute.options.fetch(:include, nil)
25
- output_formatter = attribute.options.fetch(:output, nil)
23
+ include_class = attribute.to.include_class
24
+ # output_formatter = attribute.options.fetch(:output, nil) # TODO
26
25
 
27
- value = model.public_send(attribute.name)
26
+ value = model.public_send(attribute.from.name)
28
27
 
29
28
  value =
30
29
  if include_class.present?
31
- from_type = attribute.options.fetch(:from)
32
-
33
- if [Set, Array].include?(from_type)
30
+ if [Set, Array].include?(attribute.from.type)
34
31
  value.map { |item| include_class.serialize(item) }
35
32
  else
36
33
  include_class.serialize(value)
37
34
  end
38
- elsif output_formatter.is_a?(Proc)
39
- output_formatter.call(value: value)
35
+ # elsif output_formatter.is_a?(Proc) # TODO
36
+ # output_formatter.call(value: value)
40
37
  elsif [Date, Time, DateTime].include?(value.class)
41
38
  value.to_s
42
39
  elsif value.instance_of?(ActiveSupport::Duration)
@@ -45,7 +42,7 @@ module Datory
45
42
  value
46
43
  end
47
44
 
48
- [attribute.name, value]
45
+ [attribute.from.name, value]
49
46
  end
50
47
  end
51
48
  end
@@ -8,21 +8,16 @@ module Datory
8
8
  end
9
9
 
10
10
  module ClassMethods
11
- def info # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
11
+ def info # rubocop:disable Metrics/MethodLength
12
12
  Datory::Info::Result.new(
13
13
  attributes: collection_of_attributes.to_h do |attribute|
14
- type_from = attribute.options.fetch(:from)
15
-
16
- options = {
17
- from: type_from,
18
- to: attribute.options.fetch(:to, attribute.name),
19
- as: attribute.options.fetch(:as, type_from),
20
- min: attribute.options.fetch(:min, nil),
21
- max: attribute.options.fetch(:max, nil),
22
- include: attribute.options.fetch(:include, nil)
23
- }
24
-
25
- [attribute.name, options]
14
+ [
15
+ attribute.from.name,
16
+ {
17
+ from: attribute.from.info,
18
+ to: attribute.to.info
19
+ }
20
+ ]
26
21
  end
27
22
  )
28
23
  end
@@ -29,8 +29,8 @@ module Datory
29
29
  private_constant :TRANSFORMATIONS
30
30
 
31
31
  def self.prepare_serialization_data_for(attribute) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
32
- serialized_name = attribute.name
33
- deserialized_name = attribute.options.fetch(:to, serialized_name)
32
+ serialized_name = attribute.from.name
33
+ deserialized_name = attribute.to.name
34
34
  method_name = :"assign_#{serialized_name}_output"
35
35
 
36
36
  input deserialized_name, **attribute.input_serialization_options
@@ -49,8 +49,8 @@ module Datory
49
49
  end
50
50
 
51
51
  def self.prepare_deserialization_data_for(attribute) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
52
- serialized_name = attribute.name
53
- deserialized_name = attribute.options.fetch(:to, serialized_name)
52
+ serialized_name = attribute.from.name
53
+ deserialized_name = attribute.to.name
54
54
  method_name = :"assign_#{deserialized_name}_output"
55
55
 
56
56
  input serialized_name, **attribute.input_deserialization_options
@@ -62,9 +62,7 @@ module Datory
62
62
  define_method(method_name) do
63
63
  value = inputs.public_send(deserialized_name)
64
64
 
65
- type_as = attribute.options.fetch(:as, nil)
66
-
67
- value = TRANSFORMATIONS.fetch(:DESERIALIZATION).fetch(type_as, ->(v) { v }).call(value)
65
+ value = TRANSFORMATIONS.fetch(:DESERIALIZATION).fetch(attribute.to.type, ->(v) { v }).call(value)
68
66
 
69
67
  outputs.public_send(:"#{deserialized_name}=", value)
70
68
  end
@@ -5,7 +5,7 @@ module Datory
5
5
  MAJOR = 1
6
6
  MINOR = 0
7
7
  PATCH = 0
8
- PRE = "rc12"
8
+ PRE = "rc13"
9
9
 
10
10
  STRING = [MAJOR, MINOR, PATCH, PRE].compact.join(".")
11
11
  end
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.rc12
4
+ version: 1.0.0.rc13
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-28 00:00:00.000000000 Z
11
+ date: 2024-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -216,6 +216,9 @@ 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/options/base.rb
220
+ - lib/datory/attributes/options/from.rb
221
+ - lib/datory/attributes/options/to.rb
219
222
  - lib/datory/attributes/serialization/model.rb
220
223
  - lib/datory/attributes/serialization/serializator.rb
221
224
  - lib/datory/attributes/serialization/service_builder.rb