datory 1.0.0.rc4 → 1.0.0.rc6

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: 5ca68aea305e426d629f97a3d8940e016a933e4298e56b7ab7d3f9e495fb515d
4
- data.tar.gz: 3589f41e040965dacdbaa9f22d197c837afd96f6a5d34e6b32b93e2043ce2391
3
+ metadata.gz: 36c9c3d73db41d95c3d5bafd98df4bb5002ea1f36cacc7fafa808ccb9f8af8d7
4
+ data.tar.gz: 56304b66db42d82a4b143711f1192df86f3139979711a0a9c4d6bf6be7f95d85
5
5
  SHA512:
6
- metadata.gz: 4b08dcadef0150c3183c5ae1458fdd4de9ddf9a1dd6303c5b7dfa1e104f7c4581826c4876660628879a432263ac4cc87de728d2948a3167dc1dd20c83b6bf176
7
- data.tar.gz: a19f50f5e2bfe11d3092292ea2e641f546ba78bd2b2eb8efb50769f42d47753c9a6edd46460a6391b4e389c6dcd662cbaf5510f80aa7fd66ab9cf0f3233bb5f0
6
+ metadata.gz: 7a741dd5da9f0ad4782c101ce76c1063668fe5c77074ff650edb23e16216dffe3bceb7b049b13301a06a6ac4de37156da84b3ebc3754297bf2509ef9cb09195e
7
+ data.tar.gz: ec355c7bb2073c0236d4556e6005c874987b3c513389f2efb5c64adb25de3b35f16077579c5468661e53b60a6933217be561d246031d261d8c1e5ab11be0758a
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)
@@ -49,6 +60,11 @@ module Datory
49
60
  attribute(name, **options)
50
61
  end
51
62
 
63
+ def duration(name, **options)
64
+ options = options.merge(from: ActiveSupport::Duration)
65
+ attribute(name, **options)
66
+ end
67
+
52
68
  def date(name, **options)
53
69
  options = options.merge(from: Date)
54
70
  attribute(name, **options)
@@ -64,13 +80,7 @@ module Datory
64
80
  attribute(name, **options)
65
81
  end
66
82
 
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
83
+ ########################################################################
74
84
 
75
85
  def collection_of_attributes
76
86
  @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
@@ -37,6 +39,8 @@ module Datory
37
39
  output_formatter.call(value: value)
38
40
  elsif [Date, Time, DateTime].include?(value.class)
39
41
  value.to_s
42
+ elsif ActiveSupport::Duration == value.class
43
+ value.iso8601
40
44
  else
41
45
  value
42
46
  end
@@ -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,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datory
4
+ module Utils
5
+ module_function
6
+
7
+ TRANSFORMATIONS = {
8
+ Symbol => ->(value) { value.to_sym },
9
+ String => ->(value) { value.to_s },
10
+ Integer => ->(value) { value.to_i },
11
+ Float => ->(value) { value.to_f },
12
+ Date => ->(value) { Date.parse(value) },
13
+ Time => ->(value) { Time.parse(value) },
14
+ DateTime => ->(value) { DateTime.parse(value) },
15
+ ActiveSupport::Duration => ->(value) { ActiveSupport::Duration.parse(value) }
16
+ }.freeze
17
+
18
+ private_constant :TRANSFORMATIONS
19
+
20
+ def transform_value_with(value, type)
21
+ TRANSFORMATIONS.fetch(type, ->(v) { v }).call(value)
22
+ end
23
+ end
24
+ end
@@ -5,7 +5,7 @@ module Datory
5
5
  MAJOR = 1
6
6
  MINOR = 0
7
7
  PATCH = 0
8
- PRE = "rc4"
8
+ PRE = "rc6"
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.rc6
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: