datory 1.0.0.rc3 → 1.0.0.rc5

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: 8562a68170d39d6d97a80eceae372d7f043cbb08db2890faa2d6d22a792748bf
4
- data.tar.gz: 89ef0b5c9a73289a3567e01202a9a83beeaefee6db2f084168c6e755da4e3044
3
+ metadata.gz: 6f0d3dcd343bb011dc9a4f364a9d62b14deaa0920c48495a48c36ed23d08143e
4
+ data.tar.gz: c299b7365488894f8467c094f3e11445fde073eab82b14d45ff63c284a98f67f
5
5
  SHA512:
6
- metadata.gz: 3c6ed842a16e265979fc1d5ebc4b517390884bac7118500d39cc48d6b180c7c842b08cadfdf853db111b2a56a8c016cbcbf9654d4b290272399f461d9eec48ba
7
- data.tar.gz: 6428856321dd65efb127aca09955e5e34eb1d3cff3cdc2f8da0fd669e4556582ecef976919b60f6808ce84a602a9dc60f332ffcc42b0421c613b0dc43d1ff429
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
39
- string :firstname, as: :first_name
40
- string :lastname, as: :last_name
38
+ uuid :id
39
+ string :firstname, to: :first_name
40
+ string :lastname, to: :last_name
41
41
  string :email
42
- string :birthDate, as: :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,60 +17,65 @@ 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
- options = options.merge(type: Symbol)
44
+ options = options.merge(from: Symbol)
34
45
  attribute(name, **options)
35
46
  end
36
47
 
37
48
  def string(name, **options)
38
- options = options.merge(type: String)
49
+ options = options.merge(from: String)
39
50
  attribute(name, **options)
40
51
  end
41
52
 
42
53
  def integer(name, **options)
43
- options = options.merge(type: Integer)
54
+ options = options.merge(from: Integer)
44
55
  attribute(name, **options)
45
56
  end
46
57
 
47
58
  def float(name, **options)
48
- options = options.merge(type: Float)
59
+ options = options.merge(from: Float)
49
60
  attribute(name, **options)
50
61
  end
51
62
 
52
63
  def date(name, **options)
53
- options = options.merge(type: Date)
64
+ options = options.merge(from: Date)
54
65
  attribute(name, **options)
55
66
  end
56
67
 
57
68
  def time(name, **options)
58
- options = options.merge(type: Time)
69
+ options = options.merge(from: Time)
59
70
  attribute(name, **options)
60
71
  end
61
72
 
62
73
  def datetime(name, **options)
63
- options = options.merge(type: DateTime)
74
+ options = options.merge(from: DateTime)
64
75
  attribute(name, **options)
65
76
  end
66
77
 
67
- def one(name, include:, as: nil)
68
- attribute(name, as: as.presence || name, type: Hash, include: include)
69
- end
70
-
71
- def many(name, include:, as: nil)
72
- attribute(name, as: as.presence || name, type: 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
@@ -18,7 +20,7 @@ module Datory
18
20
  end
19
21
  else
20
22
  @collection_of_attributes.to_h do |attribute|
21
- internal_name = attribute.options.fetch(:as, attribute.name)
23
+ internal_name = attribute.options.fetch(:to, attribute.name)
22
24
  include_class = attribute.options.fetch(:include, nil)
23
25
  output_formatter = attribute.options.fetch(:output, nil)
24
26
 
@@ -13,52 +13,30 @@ 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
24
- input_internal_name = attribute.options.fetch(:as, attribute.name)
25
-
26
- input attribute.name,
27
- as: input_internal_name,
28
- type: attribute.options.fetch(:type),
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
- type = attribute.options.fetch(:type, nil)
36
-
37
- if [Set, Array].include?(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: (
46
- if (type = attribute.options.fetch(:consists_of, false)) == Hash
47
- Datory::Result
48
- else
49
- type
50
- end
51
- ),
52
- type: if (type = attribute.options.fetch(:type)) == Hash
53
- Datory::Result
54
- else
55
- type
56
- end
23
+ collection_of_attributes.each do |attribute|
24
+ input_internal_name = attribute.options.fetch(:to, attribute.name)
25
+
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
 
60
32
  define_method(:"assign_#{input_internal_name}_output") do
61
- outputs.public_send(:"#{input_internal_name}=", inputs.public_send(input_internal_name))
33
+ value = inputs.public_send(input_internal_name)
34
+
35
+ option_as = attribute.options.fetch(:as, nil)
36
+
37
+ value = Datory::Utils.transform_value_with(value, option_as)
38
+
39
+ outputs.public_send(:"#{input_internal_name}=", value)
62
40
  end
63
41
  end
64
42
 
@@ -78,7 +56,7 @@ module Datory
78
56
 
79
57
  @model_class.const_set(ServiceBuilder::SERVICE_CLASS_NAME, class_sample)
80
58
  end
81
- # rubocop:enable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
59
+ # rubocop:enable Metrics/MethodLength, Metrics/AbcSize
82
60
  end
83
61
  end
84
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 = "rc3"
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.rc3
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: