datory 1.0.0.rc4 → 1.0.0.rc5

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: 5ca68aea305e426d629f97a3d8940e016a933e4298e56b7ab7d3f9e495fb515d
4
- data.tar.gz: 3589f41e040965dacdbaa9f22d197c837afd96f6a5d34e6b32b93e2043ce2391
3
+ metadata.gz: 6f0d3dcd343bb011dc9a4f364a9d62b14deaa0920c48495a48c36ed23d08143e
4
+ data.tar.gz: c299b7365488894f8467c094f3e11445fde073eab82b14d45ff63c284a98f67f
5
5
  SHA512:
6
- metadata.gz: 4b08dcadef0150c3183c5ae1458fdd4de9ddf9a1dd6303c5b7dfa1e104f7c4581826c4876660628879a432263ac4cc87de728d2948a3167dc1dd20c83b6bf176
7
- data.tar.gz: a19f50f5e2bfe11d3092292ea2e641f546ba78bd2b2eb8efb50769f42d47753c9a6edd46460a6391b4e389c6dcd662cbaf5510f80aa7fd66ab9cf0f3233bb5f0
6
+ metadata.gz: b7a458af89e5be1b6ca35f1064da7deb50a8da75751cce60d4c7bf5b93cc559c20ab6ea13156667e2be8e0d984ebebef82c2bc1a08f5c2f6fc7bccca9d9d6dfd
7
+ data.tar.gz: abaf617dcbcaf4d8e6661cca45fb39ec17d1f69f656f2afc85ee42ccf3beb197c56c60acc07a82d0818d4a5af425b8b2e080bbdecf76815fadb966d08ed70706
data/README.md CHANGED
@@ -35,11 +35,11 @@ UserDto.deserialize(json)
35
35
 
36
36
  ```ruby
37
37
  class UserDto < Datory::Base
38
- string :id
38
+ uuid :id
39
39
  string :firstname, to: :first_name
40
40
  string :lastname, to: :last_name
41
41
  string :email
42
- string :birthDate, to: :birth_date, output: ->(value:) { value.to_s }
42
+ string :birthDate, to: :birth_date, as: Date
43
43
 
44
44
  one :login, include: UserLoginDto
45
45
 
@@ -59,7 +59,7 @@ class UserLoginDto < Datory::Base
59
59
  string :password
60
60
  string :md5
61
61
  string :sha1
62
- string :registered
62
+ string :registered, to: :registered_at, as: DateTime
63
63
  end
64
64
  ```
65
65
 
@@ -9,6 +9,56 @@ module Datory
9
9
  @name = name
10
10
  @options = options
11
11
  end
12
+
13
+ def input_options # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
14
+ hash = {
15
+ as: options.fetch(:to, name),
16
+ type: options.fetch(:from),
17
+ required: options.fetch(:required, true),
18
+ consists_of: options.fetch(:consists_of, false),
19
+ prepare: (lambda do |value:|
20
+ include_class = options.fetch(:include, nil)
21
+ return value unless include_class.present?
22
+
23
+ from_type = options.fetch(:from, nil)
24
+
25
+ if [Set, Array].include?(from_type)
26
+ value.map { |item| include_class.build(**item) }
27
+ else
28
+ include_class.build(**value)
29
+ end
30
+ end)
31
+ }
32
+
33
+ if (format = options.fetch(:format, nil)).present?
34
+ hash[:format] = format
35
+ end
36
+
37
+ hash
38
+ end
39
+
40
+ def output_options # rubocop:disable Metrics/MethodLength
41
+ hash = {
42
+ consists_of: if (consists_of_type = options.fetch(:consists_of, false)) == Hash
43
+ Datory::Result
44
+ else
45
+ consists_of_type
46
+ end,
47
+ type: if (from_type = options.fetch(:from)) == Hash
48
+ Datory::Result
49
+ elsif (option_as = options.fetch(:as, nil)).present?
50
+ option_as
51
+ else
52
+ from_type
53
+ end
54
+ }
55
+
56
+ if (format = options.fetch(:format, nil)).present?
57
+ hash[:format] = format
58
+ end
59
+
60
+ hash
61
+ end
12
62
  end
13
63
  end
14
64
  end
@@ -17,18 +17,29 @@ module Datory
17
17
 
18
18
  private
19
19
 
20
- def singular(value)
21
- @singular = value
20
+ def attribute(name, **options)
21
+ collection_of_attributes << Attribute.new(name, **options)
22
22
  end
23
23
 
24
- def plural(value)
25
- @plural = value
24
+ ########################################################################
25
+
26
+ def one(name, include:, to: nil)
27
+ attribute(name, to: to.presence || name, from: Hash, include: include)
26
28
  end
27
29
 
28
- def attribute(name, **options)
29
- collection_of_attributes << Attribute.new(name, **options)
30
+ def many(name, include:, to: nil)
31
+ attribute(name, to: to.presence || name, from: Array, consists_of: Hash, include: include)
32
+ end
33
+
34
+ ########################################################################
35
+
36
+ def uuid(name, **options)
37
+ options = options.merge(format: :uuid)
38
+ string(name, **options)
30
39
  end
31
40
 
41
+ ########################################################################
42
+
32
43
  def symbol(name, **options)
33
44
  options = options.merge(from: Symbol)
34
45
  attribute(name, **options)
@@ -64,13 +75,7 @@ module Datory
64
75
  attribute(name, **options)
65
76
  end
66
77
 
67
- def one(name, include:, to: nil)
68
- attribute(name, to: to.presence || name, from: Hash, include: include)
69
- end
70
-
71
- def many(name, include:, to: nil)
72
- attribute(name, to: to.presence || name, from: Array, consists_of: Hash, include: include)
73
- end
78
+ ########################################################################
74
79
 
75
80
  def collection_of_attributes
76
81
  @collection_of_attributes ||= Collection.new
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Datory
2
4
  module Attributes
3
5
  module Serialization
@@ -13,47 +13,19 @@ module Datory
13
13
  @collection_of_attributes = collection_of_attributes
14
14
  end
15
15
 
16
- # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
16
+ # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
17
17
  def create
18
18
  return if @model_class.const_defined?(ServiceBuilder::SERVICE_CLASS_NAME)
19
19
 
20
20
  collection_of_attributes = @collection_of_attributes
21
21
 
22
22
  class_sample = Class.new(Datory::Service::Builder) do
23
- collection_of_attributes.each do |attribute| # rubocop:disable Metrics/BlockLength
23
+ collection_of_attributes.each do |attribute|
24
24
  input_internal_name = attribute.options.fetch(:to, attribute.name)
25
25
 
26
- input attribute.name,
27
- as: input_internal_name,
28
- type: attribute.options.fetch(:from),
29
- required: attribute.options.fetch(:required, true),
30
- consists_of: attribute.options.fetch(:consists_of, false),
31
- prepare: (lambda do |value:|
32
- include_class = attribute.options.fetch(:include, nil)
33
- return value unless include_class.present?
34
-
35
- from_type = attribute.options.fetch(:from, nil)
36
-
37
- if [Set, Array].include?(from_type)
38
- value.map { |item| include_class.build(**item) }
39
- else
40
- include_class.build(**value)
41
- end
42
- end)
43
-
44
- output input_internal_name,
45
- consists_of: if (consists_of_type = attribute.options.fetch(:consists_of, false)) == Hash
46
- Datory::Result
47
- else
48
- consists_of_type
49
- end,
50
- type: if (from_type = attribute.options.fetch(:from)) == Hash
51
- Datory::Result
52
- elsif (option_as = attribute.options.fetch(:as, nil)).present?
53
- option_as
54
- else
55
- from_type
56
- end
26
+ input attribute.name, **attribute.input_options
27
+
28
+ output input_internal_name, **attribute.output_options
57
29
 
58
30
  make :"assign_#{input_internal_name}_output"
59
31
 
@@ -62,15 +34,7 @@ module Datory
62
34
 
63
35
  option_as = attribute.options.fetch(:as, nil)
64
36
 
65
- if [Date, Time, DateTime].include?(option_as)
66
- value = option_as.parse(value)
67
- elsif option_as == String
68
- value = value.to_s
69
- elsif option_as == Integer
70
- value = value.to_i
71
- elsif option_as == Float
72
- value = value.to_f
73
- end
37
+ value = Datory::Utils.transform_value_with(value, option_as)
74
38
 
75
39
  outputs.public_send(:"#{input_internal_name}=", value)
76
40
  end
@@ -92,7 +56,7 @@ module Datory
92
56
 
93
57
  @model_class.const_set(ServiceBuilder::SERVICE_CLASS_NAME, class_sample)
94
58
  end
95
- # rubocop:enable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
59
+ # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
96
60
  end
97
61
  end
98
62
  end
@@ -13,6 +13,14 @@ module Datory
13
13
  failure_class Datory::Service::Exceptions::Failure
14
14
 
15
15
  result_class Datory::Result
16
+
17
+ input_option_helpers [
18
+ Servactory::ToolKit::DynamicOptions::Format.use
19
+ ]
20
+
21
+ output_option_helpers [
22
+ Servactory::ToolKit::DynamicOptions::Format.use
23
+ ]
16
24
  end
17
25
  end
18
26
  end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datory
4
+ module Utils
5
+ module_function
6
+
7
+ TRANSFORMATIONS = {
8
+ Date => ->(value) { Date.parse(value) },
9
+ Time => ->(value) { Time.parse(value) },
10
+ DateTime => ->(value) { DateTime.parse(value) },
11
+ Symbol => ->(value) { value.to_sym },
12
+ String => ->(value) { value.to_s },
13
+ Integer => ->(value) { value.to_i },
14
+ Float => ->(value) { value.to_f }
15
+ }.freeze
16
+
17
+ private_constant :TRANSFORMATIONS
18
+
19
+ def transform_value_with(value, type)
20
+ TRANSFORMATIONS.fetch(type, ->(v) { v }).call(value)
21
+ end
22
+ end
23
+ end
@@ -5,7 +5,7 @@ module Datory
5
5
  MAJOR = 1
6
6
  MINOR = 0
7
7
  PATCH = 0
8
- PRE = "rc4"
8
+ PRE = "rc5"
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.rc4
4
+ version: 1.0.0.rc5
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-23 00:00:00.000000000 Z
11
+ date: 2024-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -50,14 +50,14 @@ dependencies:
50
50
  requirements:
51
51
  - - '='
52
52
  - !ruby/object:Gem::Version
53
- version: 2.5.0.rc2
53
+ version: 2.5.0.rc5
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.rc2
60
+ version: 2.5.0.rc5
61
61
  - !ruby/object:Gem::Dependency
62
62
  name: zeitwerk
63
63
  requirement: !ruby/object:Gem::Requirement
@@ -211,6 +211,7 @@ files:
211
211
  - lib/datory/service/base.rb
212
212
  - lib/datory/service/builder.rb
213
213
  - lib/datory/service/exceptions.rb
214
+ - lib/datory/utils.rb
214
215
  - lib/datory/version.rb
215
216
  homepage: https://github.com/servactory/datory
216
217
  licenses: