datory 1.0.0 → 2.0.0

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: 737690b79da648cd735096c508112d29290fa3545b36edf7a1c50e55f7ef8b37
4
- data.tar.gz: fb05d32494c22838c71e26470317733a66aa9317ef200231beb407d6b70ef082
3
+ metadata.gz: 9f3a9f44ab2e8f4a239e74584df3257a3d5311cec3b4af1ad0bd0797d117b2a9
4
+ data.tar.gz: 5309732bfa829f0f6b9249d24f62483a8e04e6a6ec8c6e14b964cacbd0a80abd
5
5
  SHA512:
6
- metadata.gz: baa25bfc17f75ec42119a1b28e5f0419c1b43c3a25095d9c123a51330ff1da2091849abaecb597a3bd30601ea60229dbb8eae9ca4300d9412246a7515bedeeda
7
- data.tar.gz: 570d760bf48ea0bdd2e7e27e3143702eadeaffd82398f51e49aad4fcd7a87ead19e056b25e929af48ce6c7b8f8f360cd09779cde1fe16e06ede5705bc02f1c7e
6
+ metadata.gz: 6572ce657ce219f037a9d6da576749a2983489e3a1eab12d697e456209972df7f5ecf76373f281bab6e93f4cbe2e39b9225dfb0dc269804c1a889b745acf6dde
7
+ data.tar.gz: 966d9d98944a0b541900e580d253e2c28150525710db322010cfa70f44a12a474e0f1fa3621a8674af0d5f7d8ef90d0f25f055445a178558adcbcca5e6025e49
data/README.md CHANGED
@@ -52,38 +52,42 @@ form.serialize # => { ... }
52
52
 
53
53
  ```ruby
54
54
  class SerialDto < Datory::Base
55
- uuid :id
55
+ uuid! :id
56
56
 
57
- string :status
58
- string :title
57
+ string! :status
58
+ string! :title
59
59
 
60
- one :poster, include: ImageDto
60
+ one! :poster, include: ImageDto
61
61
 
62
- one :ratings, include: RatingsDto
62
+ one! :ratings, include: RatingsDto
63
63
 
64
- many :countries, include: CountryDto
65
- many :genres, include: GenreDto
66
- many :seasons, include: SeasonDto
64
+ many! :countries, include: CountryDto
65
+ many! :genres, include: GenreDto
66
+ many! :seasons, include: SeasonDto
67
67
 
68
- date :premieredOn, to: :premiered_on
68
+ date! :premieredOn, to: :premiered_on
69
69
  end
70
70
  ```
71
71
 
72
72
  ```ruby
73
73
  class SeasonDto < Datory::Base
74
- uuid :id
75
- uuid :serial_id
74
+ uuid! :id
75
+ uuid! :serialId, to: :serial_id
76
76
 
77
- integer :number
77
+ integer! :number
78
78
 
79
- many :episodes, include: EpisodeDto
79
+ many! :episodes, include: EpisodeDto
80
80
 
81
- date :premiered_on
81
+ date! :premieredOn, to: :premiered_on
82
+ date? :endedOn, to: :ended_on
82
83
  end
83
84
  ```
84
85
 
85
86
  ## Attribute declaration
86
87
 
88
+ Using the `!` sign means that the attribute is required.
89
+ The optionality of an attribute is indicated using the `?` sign.
90
+
87
91
  ### Basic
88
92
 
89
93
  #### attribute
@@ -92,84 +96,84 @@ end
92
96
  attribute :uuid, from: String, to: :id, as: String, format: :uuid
93
97
  ```
94
98
 
95
- #### string
99
+ #### string!
96
100
 
97
101
  ```ruby
98
- string :uuid, to: :id
102
+ string! :uuid, to: :id
99
103
  ```
100
104
 
101
- #### integer
105
+ #### integer!
102
106
 
103
107
  ```ruby
104
- integer :rating, min: 1, max: 10
108
+ integer! :rating, min: 1, max: 10
105
109
  ```
106
110
 
107
- #### float
111
+ #### float!
108
112
 
109
113
  ```ruby
110
- float :rating
114
+ float! :rating
111
115
  ```
112
116
 
113
- #### boolean
117
+ #### boolean!
114
118
 
115
119
  ```ruby
116
- boolean :default
120
+ boolean! :default
117
121
  ```
118
122
 
119
123
  ### Helpers
120
124
 
121
- #### uuid
125
+ #### uuid!
122
126
 
123
127
  It will also check that the value matches the UUID format.
124
128
 
125
129
  ```ruby
126
- uuid :id
130
+ uuid! :id
127
131
  ```
128
132
 
129
- #### money
133
+ #### money!
130
134
 
131
135
  It will prepare two attributes `*_cents` and `*_currency`.
132
136
 
133
137
  ```ruby
134
- money :box_office
138
+ money! :box_office
135
139
  ```
136
140
 
137
- #### duration
141
+ #### duration!
138
142
 
139
143
  ```ruby
140
- duration :episode_duration
144
+ duration! :episode_duration
141
145
  ```
142
146
 
143
- #### date
147
+ #### date!
144
148
 
145
149
  ```ruby
146
- date :premiered_on
150
+ date! :premiered_on
147
151
  ```
148
152
 
149
- #### time
153
+ #### time!
150
154
 
151
155
  ```ruby
152
- time :premiered_at
156
+ time! :premiered_at
153
157
  ```
154
158
 
155
- #### datetime
159
+ #### datetime!
156
160
 
157
161
  ```ruby
158
- time :premiered_at
162
+ time! :premiered_at
159
163
  ```
160
164
 
161
165
  ### Nesting
162
166
 
163
- #### one
167
+ #### one!
164
168
 
165
169
  ```ruby
166
- one :poster, include: ImageDto
170
+ one! :poster, include: ImageDto
167
171
  ```
168
172
 
169
- #### many
173
+ #### many!
170
174
 
171
175
  ```ruby
172
- many :seasons, include: SeasonDto
176
+ many! :seasons, include: SeasonDto
173
177
  ```
174
178
 
175
179
  ## Object information
@@ -181,7 +185,7 @@ SerialDto.info
181
185
  ```
182
186
 
183
187
  ```
184
- #<Datory::Info::Result:0x0000000124aa7bc8 @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}}, :status=>{:from=>{:name=>:status, :type=>String, :min=>nil, :max=>nil, :consists_of=>false, :format=>nil}, :to=>{:name=>:status, :type=>String, :min=>nil, :max=>nil, :consists_of=>false, :format=>nil, :required=>true, :include=>nil}}, :title=>{:from=>{:name=>:title, :type=>String, :min=>nil, :max=>nil, :consists_of=>false, :format=>nil}, :to=>{:name=>:title, :type=>String, :min=>nil, :max=>nil, :consists_of=>false, :format=>nil, :required=>true, :include=>nil}}, :poster=>{:from=>{:name=>:poster, :type=>Hash, :min=>nil, :max=>nil, :consists_of=>false, :format=>nil}, :to=>{:name=>:poster, :type=>[Datory::Result, Hash], :min=>nil, :max=>nil, :consists_of=>false, :format=>nil, :required=>true, :include=>Usual::Example1::Image}}, :ratings=>{:from=>{:name=>:ratings, :type=>Hash, :min=>nil, :max=>nil, :consists_of=>false, :format=>nil}, :to=>{:name=>:ratings, :type=>[Datory::Result, Hash], :min=>nil, :max=>nil, :consists_of=>false, :format=>nil, :required=>true, :include=>Usual::Example1::Ratings}}, :countries=>{:from=>{:name=>:countries, :type=>Array, :min=>nil, :max=>nil, :consists_of=>[Datory::Result, Hash], :format=>nil}, :to=>{:name=>:countries, :type=>Array, :min=>nil, :max=>nil, :consists_of=>[Datory::Result, Hash], :format=>nil, :required=>true, :include=>Usual::Example1::Country}}, :genres=>{:from=>{:name=>:genres, :type=>Array, :min=>nil, :max=>nil, :consists_of=>[Datory::Result, Hash], :format=>nil}, :to=>{:name=>:genres, :type=>Array, :min=>nil, :max=>nil, :consists_of=>[Datory::Result, Hash], :format=>nil, :required=>true, :include=>Usual::Example1::Genre}}, :seasons=>{:from=>{:name=>:seasons, :type=>Array, :min=>nil, :max=>nil, :consists_of=>[Datory::Result, Hash], :format=>nil}, :to=>{:name=>:seasons, :type=>Array, :min=>nil, :max=>nil, :consists_of=>[Datory::Result, Hash], :format=>nil, :required=>true, :include=>Usual::Example1::Season}}, :premieredOn=>{:from=>{:name=>:premieredOn, :type=>String, :min=>nil, :max=>nil, :consists_of=>false, :format=>:date}, :to=>{:name=>:premiered_on, :type=>Date, :min=>nil, :max=>nil, :consists_of=>false, :format=>nil, :required=>true, :include=>nil}}}>
188
+ #<Datory::Info::Result:0x0000000128a33c38 @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, :default=>nil, :include=>nil}}, :status=>{:from=>{:name=>:status, :type=>String, :min=>nil, :max=>nil, :consists_of=>false, :format=>nil}, :to=>{:name=>:status, :type=>String, :min=>nil, :max=>nil, :consists_of=>false, :format=>nil, :required=>true, :default=>nil, :include=>nil}}, :title=>{:from=>{:name=>:title, :type=>String, :min=>nil, :max=>nil, :consists_of=>false, :format=>nil}, :to=>{:name=>:title, :type=>String, :min=>nil, :max=>nil, :consists_of=>false, :format=>nil, :required=>true, :default=>nil, :include=>nil}}, :poster=>{:from=>{:name=>:poster, :type=>[Usual::Example1::Image, Hash], :min=>nil, :max=>nil, :consists_of=>false, :format=>nil}, :to=>{:name=>:poster, :type=>[Usual::Example1::Image, Hash], :min=>nil, :max=>nil, :consists_of=>false, :format=>nil, :required=>true, :default=>nil, :include=>Usual::Example1::Image}}, :ratings=>{:from=>{:name=>:ratings, :type=>[Usual::Example1::Ratings, Hash], :min=>nil, :max=>nil, :consists_of=>false, :format=>nil}, :to=>{:name=>:ratings, :type=>[Usual::Example1::Ratings, Hash], :min=>nil, :max=>nil, :consists_of=>false, :format=>nil, :required=>true, :default=>nil, :include=>Usual::Example1::Ratings}}, :countries=>{:from=>{:name=>:countries, :type=>Array, :min=>nil, :max=>nil, :consists_of=>[Usual::Example1::Country, Hash], :format=>nil}, :to=>{:name=>:countries, :type=>Array, :min=>nil, :max=>nil, :consists_of=>[Usual::Example1::Country, Hash], :format=>nil, :required=>true, :default=>nil, :include=>Usual::Example1::Country}}, :genres=>{:from=>{:name=>:genres, :type=>Array, :min=>nil, :max=>nil, :consists_of=>[Usual::Example1::Genre, Hash], :format=>nil}, :to=>{:name=>:genres, :type=>Array, :min=>nil, :max=>nil, :consists_of=>[Usual::Example1::Genre, Hash], :format=>nil, :required=>true, :default=>nil, :include=>Usual::Example1::Genre}}, :seasons=>{:from=>{:name=>:seasons, :type=>Array, :min=>nil, :max=>nil, :consists_of=>[Usual::Example1::Season, Hash], :format=>nil}, :to=>{:name=>:seasons, :type=>Array, :min=>nil, :max=>nil, :consists_of=>[Usual::Example1::Season, Hash], :format=>nil, :required=>true, :default=>nil, :include=>Usual::Example1::Season}}, :premieredOn=>{:from=>{:name=>:premieredOn, :type=>String, :min=>nil, :max=>nil, :consists_of=>false, :format=>:date}, :to=>{:name=>:premiered_on, :type=>Date, :min=>nil, :max=>nil, :consists_of=>false, :format=>nil, :required=>true, :default=>nil, :include=>nil}}}>
185
189
  ```
186
190
 
187
191
  ### Describe
@@ -193,21 +197,21 @@ SerialDto.describe
193
197
  ```
194
198
 
195
199
  ```
196
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
197
- | SerialDto |
198
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
199
- | Attribute | From | To | As | Include |
200
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
201
- | id | String | id | String | |
202
- | status | String | status | String | |
203
- | title | String | title | String | |
204
- | poster | Hash | poster | [Datory::Result, Hash] | ImageDto |
205
- | ratings | Hash | ratings | [Datory::Result, Hash] | RatingsDto |
206
- | countries | Array | countries | Array | CountryDto |
207
- | genres | Array | genres | Array | GenreDto |
208
- | seasons | Array | seasons | Array | SeasonDto |
209
- | premieredOn | String | premiered_on | Date | |
210
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
200
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
201
+ | SerialDto |
202
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
203
+ | Attribute | From | To | As | Include |
204
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
205
+ | id | String | id | String | |
206
+ | status | String | status | String | |
207
+ | title | String | title | String | |
208
+ | poster | [ImageDto, Hash] | poster | [ImageDto, Hash] | ImageDto | |
209
+ | ratings | [RatingsDto, Hash] | ratings | [RatingsDto, Hash] | RatingsDto | |
210
+ | countries | Array | countries | Array | CountryDto |
211
+ | genres | Array | genres | Array | GenreDto |
212
+ | seasons | Array | seasons | Array | SeasonDto |
213
+ | premieredOn | String | premiered_on | Date | |
214
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
211
215
  ```
212
216
 
213
217
  ## Contributing
@@ -51,8 +51,8 @@ module Datory
51
51
 
52
52
  def output_serialization_options # rubocop:disable Metrics/AbcSize
53
53
  hash = {
54
- consists_of: to.consists_of == Hash ? Datory::Result : from.consists_of,
55
- type: to.type == Datory::Result ? Hash : from.type
54
+ consists_of: from.consists_of,
55
+ type: from.type
56
56
  }
57
57
 
58
58
  hash[:min] = from.min if from.min.present?
@@ -97,8 +97,8 @@ module Datory
97
97
 
98
98
  def output_deserialization_options # rubocop:disable Metrics/AbcSize
99
99
  hash = {
100
- consists_of: from.consists_of == Hash ? Datory::Result : to.consists_of,
101
- type: from.type == Hash ? Datory::Result : to.type
100
+ consists_of: to.consists_of,
101
+ type: to.type
102
102
  }
103
103
 
104
104
  hash[:min] = to.min if to.min.present?
@@ -4,14 +4,14 @@ module Datory
4
4
  module Attributes
5
5
  class Collection
6
6
  extend Forwardable
7
- def_delegators :@collection, :<<, :each, :map, :filter, :to_h, :merge
7
+ def_delegators :@collection, :<<, :each, :map, :filter, :to_h, :merge, :find
8
8
 
9
9
  def initialize(collection = Set.new)
10
10
  @collection = collection
11
11
  end
12
12
 
13
- def names
14
- map(&:name)
13
+ def find_by(name:)
14
+ find { |attribute| attribute.from.name == name }
15
15
  end
16
16
 
17
17
  def internal_names
@@ -19,7 +19,9 @@ module Datory
19
19
  def build!
20
20
  ServiceFactory.create(@context.class, @collection_of_attributes)
21
21
 
22
- builder_class.call!(**@incoming_attributes)
22
+ result = builder_class.call!(**@incoming_attributes)
23
+
24
+ @context.class.serialization.new(**result.to_h)
23
25
  end
24
26
 
25
27
  private
@@ -27,9 +27,9 @@ module Datory
27
27
  attribute(
28
28
  name,
29
29
  to: to.presence || name,
30
- from: Hash,
30
+ from: [include, Hash],
31
31
  include: include,
32
- as: [Datory::Result, Hash]
32
+ as: [include, Hash]
33
33
  )
34
34
  end
35
35
  # NOTE: This will most likely be marked as deprecated in the future in favor of `one!`
@@ -39,9 +39,9 @@ module Datory
39
39
  attribute(
40
40
  name,
41
41
  to: to.presence || name,
42
- from: [Hash, NilClass],
42
+ from: [include, Hash, NilClass],
43
43
  include: include,
44
- as: [Datory::Result, Hash, NilClass],
44
+ as: [include, Hash, NilClass],
45
45
  required: false
46
46
  )
47
47
  end
@@ -51,7 +51,7 @@ module Datory
51
51
  name,
52
52
  to: to.presence || name,
53
53
  from: Array,
54
- consists_of: [Datory::Result, Hash],
54
+ consists_of: [include, Hash],
55
55
  include: include,
56
56
  as: Array
57
57
  )
@@ -64,7 +64,7 @@ module Datory
64
64
  name,
65
65
  to: to.presence || name,
66
66
  from: Array,
67
- consists_of: [Datory::Result, Hash],
67
+ consists_of: [include, Hash],
68
68
  include: include,
69
69
  as: Array,
70
70
  required: false,
@@ -19,11 +19,11 @@ module Datory
19
19
  raise_misuse "The `update` method cannot be used with a collection. Instead, use the `update_by` method."
20
20
  end
21
21
 
22
- found_keys = model.keys & attributes.keys
22
+ found_keys = model.send(:keys) & attributes.keys
23
23
 
24
24
  reset!
25
25
 
26
- model.merge!(attributes.slice(*found_keys))
26
+ model.send(:merge!, attributes.slice(*found_keys))
27
27
  end
28
28
 
29
29
  def update_by(index, **attributes) # rubocop:disable Metrics/MethodLength
@@ -35,9 +35,9 @@ module Datory
35
35
 
36
36
  model.map!.with_index do |model_item, model_index|
37
37
  if model_index == index
38
- found_keys = model_item.keys & attributes.keys
38
+ found_keys = model_item.send(:keys) & attributes.keys
39
39
 
40
- model_item.merge(attributes.slice(*found_keys))
40
+ model_item.send(:merge, attributes.slice(*found_keys))
41
41
  else
42
42
  model_item
43
43
  end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datory
4
+ module Attributes
5
+ class Model
6
+ def self.build!(...)
7
+ new(...).build!
8
+ end
9
+
10
+ def initialize(context, direction, attributes, collection_of_attributes)
11
+ @context = context
12
+ @direction = direction
13
+ @attributes = attributes
14
+ @collection_of_attributes = collection_of_attributes
15
+ end
16
+
17
+ def build!
18
+ @collection_of_attributes.each do |attribute|
19
+ attribute_name = @direction.serialization? ? attribute.to.name : attribute.from.name
20
+ attribute_value = @attributes.fetch(attribute_name, nil)
21
+
22
+ assign_attribute_for(@context, name: attribute_name, value: attribute_value)
23
+ end
24
+
25
+ @context
26
+ end
27
+
28
+ private
29
+
30
+ def assign_attribute_for(context, name:, value:)
31
+ return if context.instance_variable_defined?(:"@#{name}")
32
+
33
+ context.instance_variable_set(:"@#{name}", value)
34
+ context.class.attr_accessor(name)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -21,7 +21,7 @@ module Datory
21
21
  end
22
22
 
23
23
  def to_hash(data)
24
- if data.is_a?(Datory::Attributes::Serialization::Model)
24
+ if data.is_a?(Datory::Attributes::Serialization::Model) || data.is_a?(Datory::Base)
25
25
  parse(data)
26
26
  else
27
27
  data
@@ -52,9 +52,9 @@ module Datory
52
52
  value = data.instance_variable_get(key)
53
53
 
54
54
  value =
55
- if value.is_a?(Array)
56
- value.map! { |item| Datory::Attributes::Serialization::Model.to_hash(item) }
57
- elsif value.is_a?(Datory::Attributes::Serialization::Model)
55
+ if value.is_a?(Set) || value.is_a?(Array)
56
+ value.map { |item| Datory::Attributes::Serialization::Model.to_hash(item) }
57
+ elsif value.is_a?(Datory::Attributes::Serialization::Model) || value.is_a?(Datory::Base)
58
58
  Datory::Attributes::Serialization::Model.to_hash(value)
59
59
  else
60
60
  value
@@ -24,12 +24,12 @@ module Datory
24
24
  Deserialization::ServiceBuilder.build!(self, incoming_attributes, collection_of_attributes)
25
25
  end
26
26
 
27
- def to_model(attributes:)
27
+ def to_model(direction:, attributes:, collection_of_attributes:)
28
28
  super
29
29
 
30
- attributes.each do |attribute_name, attribute_value|
31
- define_singleton_method(attribute_name) { attribute_value }
32
- end
30
+ Model.build!(self, direction, attributes, collection_of_attributes)
31
+
32
+ self
33
33
  end
34
34
  end
35
35
  end
@@ -13,7 +13,7 @@ module Datory
13
13
  serialize(model_item)
14
14
  end
15
15
  else
16
- context = send(:new)
16
+ context = send(:new, _datory_to_model: false)
17
17
  model = Datory::Attributes::Serialization::Model.prepare(model)
18
18
  _serialize(context, model)
19
19
  end
@@ -23,18 +23,24 @@ module Datory
23
23
  raise Datory::Exceptions::SerializationError.new(message: e.message)
24
24
  end
25
25
 
26
- def deserialize(json) # rubocop:disable Metrics/MethodLength
27
- # TODO: Need to improve this place by adding more checks and an error exception.
28
- parsed_data = json.is_a?(String) ? JSON.parse(json) : json
26
+ def deserialize(data) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
27
+ prepared_data =
28
+ if data.is_a?(Datory::Base)
29
+ Datory::Attributes::Serialization::Model.to_hash(data)
30
+ elsif data.is_a?(String)
31
+ JSON.parse(data)
32
+ else
33
+ data
34
+ end
29
35
 
30
- if [Set, Array].include?(parsed_data.class)
31
- parsed_data.map do |item|
36
+ if [Set, Array].include?(prepared_data.class)
37
+ prepared_data.map do |item|
32
38
  deserialize(item)
33
39
  end
34
40
  else
35
- context = send(:new)
41
+ context = send(:new, _datory_to_model: false)
36
42
 
37
- _deserialize(context, **parsed_data)
43
+ _deserialize(context, **prepared_data)
38
44
  end
39
45
  rescue Datory::Service::Exceptions::Input,
40
46
  Datory::Service::Exceptions::Internal,
@@ -47,10 +53,21 @@ module Datory
47
53
  raise Datory::Exceptions::DeserializationError.new(message: message, meta: { original_exception: e })
48
54
  end
49
55
 
50
- def to_model(**attributes)
51
- context = send(:new)
56
+ def new(_datory_to_model: true, **attributes) # rubocop:disable Lint/UnderscorePrefixedVariableName
57
+ context = super()
58
+
59
+ return context unless _datory_to_model
60
+
61
+ direction = :serialization
62
+
63
+ if defined?(@_datory_model_type) && @_datory_model_type[context.class.name].present?
64
+ direction = @_datory_model_type.fetch(context.class.name)
65
+ @_datory_model_type.delete(context.class.name)
66
+ end
67
+
68
+ direction = direction.to_s.inquiry
52
69
 
53
- _to_model(context, **attributes)
70
+ _to_model(context, direction: direction, **attributes)
54
71
  end
55
72
 
56
73
  def describe
@@ -61,6 +78,18 @@ module Datory
61
78
  end
62
79
  alias table describe
63
80
 
81
+ def serialization
82
+ assign_datory_model_type(:serialization)
83
+
84
+ self
85
+ end
86
+
87
+ def deserialization
88
+ assign_datory_model_type(:deserialization)
89
+
90
+ self
91
+ end
92
+
64
93
  private
65
94
 
66
95
  def _serialize(context, model)
@@ -79,12 +108,21 @@ module Datory
79
108
  )
80
109
  end
81
110
 
82
- def _to_model(context, **attributes)
111
+ def _to_model(context, direction:, **attributes)
83
112
  context.send(
84
113
  :_to_model,
85
- attributes: attributes
114
+ direction: direction,
115
+ attributes: attributes,
116
+ collection_of_attributes: collection_of_attributes
86
117
  )
87
118
  end
119
+
120
+ def assign_datory_model_type(type)
121
+ data = { name => type }
122
+
123
+ @_datory_model_type =
124
+ defined?(@_datory_model_type) ? @_datory_model_type.merge(data) : data
125
+ end
88
126
  end
89
127
  end
90
128
  end
@@ -3,6 +3,22 @@
3
3
  module Datory
4
4
  module Context
5
5
  module Workspace
6
+ private
7
+
8
+ def merge!(attributes)
9
+ attributes.each do |key, value|
10
+ instance_variable_set(:"@#{key}", value)
11
+ self.class.attr_reader(key)
12
+ end
13
+
14
+ self
15
+ end
16
+ alias merge merge!
17
+
18
+ def keys
19
+ instance_variables.map { |instance_variable| instance_variable.to_s.sub(/^@/, "").to_sym }
20
+ end
21
+
6
22
  def _serialize(model:, collection_of_attributes:)
7
23
  serialize(
8
24
  model: model,
@@ -17,25 +33,19 @@ module Datory
17
33
  )
18
34
  end
19
35
 
20
- def _to_model(attributes:)
36
+ def _to_model(direction:, attributes:, collection_of_attributes:)
21
37
  to_model(
22
- attributes: attributes
38
+ direction: direction,
39
+ attributes: attributes,
40
+ collection_of_attributes: collection_of_attributes
23
41
  )
24
42
  end
25
43
 
26
- def serialize(model:, collection_of_attributes:, **)
27
- @model = model
28
- @collection_of_attributes = collection_of_attributes
29
- end
44
+ def serialize(**); end
30
45
 
31
- def deserialize(incoming_attributes:, collection_of_attributes:, **)
32
- @incoming_attributes = incoming_attributes
33
- @collection_of_attributes = collection_of_attributes
34
- end
46
+ def deserialize(**); end
35
47
 
36
- def to_model(attributes:)
37
- @attributes = attributes
38
- end
48
+ def to_model(**); end
39
49
  end
40
50
  end
41
51
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Datory
4
4
  module VERSION
5
- MAJOR = 1
5
+ MAJOR = 2
6
6
  MINOR = 0
7
7
  PATCH = 0
8
8
  PRE = nil
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: 2.0.0
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-07-05 00:00:00.000000000 Z
11
+ date: 2024-08-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '5.1'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '7.2'
22
+ version: '8.0'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: '5.1'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '7.2'
32
+ version: '8.0'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: i18n
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -50,14 +50,14 @@ dependencies:
50
50
  requirements:
51
51
  - - ">="
52
52
  - !ruby/object:Gem::Version
53
- version: 2.6.0
53
+ version: 2.8.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.6.0
60
+ version: 2.8.0
61
61
  - !ruby/object:Gem::Dependency
62
62
  name: terminal-table
63
63
  requirement: !ruby/object:Gem::Requirement
@@ -217,6 +217,7 @@ files:
217
217
  - lib/datory/attributes/deserialization/service_factory.rb
218
218
  - lib/datory/attributes/dsl.rb
219
219
  - lib/datory/attributes/form.rb
220
+ - lib/datory/attributes/model.rb
220
221
  - lib/datory/attributes/options/base.rb
221
222
  - lib/datory/attributes/options/from.rb
222
223
  - lib/datory/attributes/options/to.rb
@@ -264,7 +265,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
264
265
  - !ruby/object:Gem::Version
265
266
  version: '0'
266
267
  requirements: []
267
- rubygems_version: 3.5.9
268
+ rubygems_version: 3.5.11
268
269
  signing_key:
269
270
  specification_version: 4
270
271
  summary: A set of tools for building reliable services of any complexity