datory 1.0.0.rc11 → 1.0.0.rc12

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: 0ef2274984450ba2915867f37000b443ffd18a51eeb497adecaaaeae7d0821ae
4
- data.tar.gz: ddab73410ef68b0a65f02ed24f9e7177ea43f6779015aa9f5208d4583f8f2882
3
+ metadata.gz: 198bd72a333d4fa2fe2ee18935d586ab10f6185c7b51c6cb94f2627f5698fb52
4
+ data.tar.gz: 6aa7c4bf85587d02a38e79de2135c83bed01be5153a644c0892433887dd1b982
5
5
  SHA512:
6
- metadata.gz: 0c794e11db75f222df975ddf31d0e0c9ff2c37ed7dc33e9c6707a5addfd53c12265f45b8808a3c5d7eebbf426674ccf8442bc3f74ec367ee4f67467ed8e0ea51
7
- data.tar.gz: 235ed83fe57e9db74fb93803e83820ecba0aea31a673ede0ae6b485a8d4c219394cf1cd0ceb04efd06048e554039105a05538fda8eb520404f9eaf7bb904c1fd
6
+ metadata.gz: ed216465c59651df53b3170a9381a058a26b5dcaa48058b20300eae6111f9ef582b198391f3813f49e886759c18844a621c4b1a77d9d4a131080fda9ed2e5608
7
+ data.tar.gz: df02fd6e1eabc40683e506052d448aa9f563f857a52fc48972482b30e962a6571801f9c816ca805bbe1fed8b1acf4c8310d6784374281ecfeb075c1d182480ab
data/README.md CHANGED
@@ -67,11 +67,132 @@ class UserLoginDto < Datory::Base
67
67
  end
68
68
  ```
69
69
 
70
+ ## Attribute declaration
71
+
72
+ ### Basic
73
+
74
+ #### attribute
75
+
76
+ ```ruby
77
+ attribute :firstname, from: String, to: :first_name, as: String
78
+ ```
79
+
80
+ #### string
81
+
82
+ ```ruby
83
+ string :first_name
84
+ ```
85
+
86
+ #### integer
87
+
88
+ ```ruby
89
+ integer :attempts, min: 1, max: 10
90
+ ```
91
+
92
+ #### float
93
+
94
+ ```ruby
95
+ float :interest_rate
96
+ ```
97
+
98
+ ### Helpers
99
+
100
+ #### uuid
101
+
102
+ It will also check that the value matches the UUID format.
103
+
104
+ ```ruby
105
+ uuid :id
106
+ ```
107
+
108
+ #### money
109
+
110
+ It will prepare two attributes `*_cents` and `*_currency`.
111
+
112
+ ```ruby
113
+ money :price
114
+ ```
115
+
116
+ #### duration
117
+
118
+ ```ruby
119
+ duration :lifetime
120
+ ```
121
+
122
+ #### date
123
+
124
+ ```ruby
125
+ date :birth_date
126
+ ```
127
+
128
+ #### time
129
+
130
+ ```ruby
131
+ time :registered_at
132
+ ```
133
+
134
+ #### datetime
135
+
136
+ ```ruby
137
+ datetime :registered_at
138
+ ```
139
+
140
+ ### Nesting
141
+
142
+ #### one
143
+
144
+ ```ruby
145
+ one :company, include: UserCompanyDto
146
+ ```
147
+
148
+ #### many
149
+
150
+ ```ruby
151
+ many :addresses, include: UserAddressDto
152
+ ```
153
+
154
+ ## Object information
155
+
156
+ ### Info
157
+
158
+ ```ruby
159
+ UserDto.info
160
+ ```
161
+
162
+ ```
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}}>
164
+ ```
165
+
166
+ ### Describe
167
+
168
+ ```ruby
169
+ UserDto.describe
170
+ ```
171
+
172
+ ```
173
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
174
+ | UserDto |
175
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
176
+ | Attribute | From | To | As | Include |
177
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
178
+ | id | String | id | String | |
179
+ | firstname | String | first_name | String | |
180
+ | lastname | String | last_name | String | |
181
+ | email | String | email | String | |
182
+ | phone | String | phone | String | |
183
+ | website | String | website | String | |
184
+ | birthDate | String | birth_date | Date | |
185
+ | login | Hash | login | [Datory::Result, Hash] | Usual::Example1::UserLogin |
186
+ | company | Hash | company | [Datory::Result, Hash] | Usual::Example1::UserCompany |
187
+ | addresses | Array | addresses | Array | Usual::Example1::UserAddress |
188
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
189
+ ```
190
+
70
191
  ## Contributing
71
192
 
72
193
  This project is intended to be a safe, welcoming space for collaboration.
73
194
  Contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
74
- We recommend reading the [contributing guide](./website/docs/CONTRIBUTING.md) as well.
195
+ We recommend reading the [contributing guide](./CONTRIBUTING.md) as well.
75
196
 
76
197
  ## License
77
198
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Datory
4
4
  module Attributes
5
- class Attribute
5
+ class Attribute # rubocop:disable Metrics/ClassLength
6
6
  attr_reader :name, :options
7
7
 
8
8
  def initialize(name, **options)
@@ -10,7 +10,7 @@ module Datory
10
10
  @options = options
11
11
  end
12
12
 
13
- def input_serialization_options
13
+ def input_serialization_options # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
14
14
  hash = {
15
15
  as: options.fetch(:to, name),
16
16
  type: options.fetch(:as, options.fetch(:from)),
@@ -18,6 +18,14 @@ module Datory
18
18
  consists_of: options.fetch(:consists_of, false)
19
19
  }
20
20
 
21
+ if (min = options.fetch(:min, nil)).present?
22
+ hash[:min] = min
23
+ end
24
+
25
+ if (max = options.fetch(:max, nil)).present?
26
+ hash[:max] = max
27
+ end
28
+
21
29
  if (format = options.fetch(:format, nil)).present?
22
30
  hash[:format] = format
23
31
  end
@@ -45,6 +53,14 @@ module Datory
45
53
  end)
46
54
  }
47
55
 
56
+ if (min = options.fetch(:min, nil)).present?
57
+ hash[:min] = min
58
+ end
59
+
60
+ if (max = options.fetch(:max, nil)).present?
61
+ hash[:max] = max
62
+ end
63
+
48
64
  if (format = options.fetch(:format, nil)).present?
49
65
  hash[:format] = format
50
66
  end
@@ -52,7 +68,7 @@ module Datory
52
68
  hash
53
69
  end
54
70
 
55
- def output_serialization_options # rubocop:disable Metrics/MethodLength
71
+ def output_serialization_options # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/PerceivedComplexity
56
72
  hash = {
57
73
  consists_of: if (consists_of_type = options.fetch(:consists_of, false)) == Hash
58
74
  Datory::Result
@@ -68,6 +84,14 @@ module Datory
68
84
  end
69
85
  }
70
86
 
87
+ if (min = options.fetch(:min, nil)).present?
88
+ hash[:min] = min
89
+ end
90
+
91
+ if (max = options.fetch(:max, nil)).present?
92
+ hash[:max] = max
93
+ end
94
+
71
95
  if (format = options.fetch(:format, nil)).present?
72
96
  hash[:format] = format
73
97
  end
@@ -75,7 +99,7 @@ module Datory
75
99
  hash
76
100
  end
77
101
 
78
- def output_deserialization_options # rubocop:disable Metrics/MethodLength
102
+ def output_deserialization_options # rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/PerceivedComplexity
79
103
  hash = {
80
104
  consists_of: if (consists_of_type = options.fetch(:consists_of, false)) == Hash
81
105
  Datory::Result
@@ -91,6 +115,14 @@ module Datory
91
115
  end
92
116
  }
93
117
 
118
+ if (min = options.fetch(:min, nil)).present?
119
+ hash[:min] = min
120
+ end
121
+
122
+ if (max = options.fetch(:max, nil)).present?
123
+ hash[:max] = max
124
+ end
125
+
94
126
  if (format = options.fetch(:format, nil)).present?
95
127
  hash[:format] = format
96
128
  end
@@ -4,7 +4,7 @@ module Datory
4
4
  module Attributes
5
5
  class Collection
6
6
  extend Forwardable
7
- def_delegators :@collection, :<<, :each, :map, :to_h, :merge
7
+ def_delegators :@collection, :<<, :each, :map, :filter, :to_h, :merge
8
8
 
9
9
  def initialize(collection = Set.new)
10
10
  @collection = collection
@@ -17,6 +17,20 @@ module Datory
17
17
  def internal_names
18
18
  map { |attribute| attribute.options.fetch(:to, attribute.name) }
19
19
  end
20
+
21
+ def include_exist?
22
+ @include_exist ||= filter do |attribute| # rubocop:disable Performance/Count
23
+ include_class = attribute.options.fetch(:include, nil)
24
+
25
+ next false if include_class.nil?
26
+
27
+ if [Set, Array].include?(include_class)
28
+ include_class.any? { |item| item <= Datory::Base }
29
+ else
30
+ include_class <= Datory::Base
31
+ end
32
+ end.size.positive?
33
+ end
20
34
  end
21
35
  end
22
36
  end
@@ -15,6 +15,7 @@ module Datory
15
15
  headings << "From"
16
16
  headings << "To"
17
17
  headings << "As"
18
+ headings << "Include" if collection_of_attributes.include_exist?
18
19
 
19
20
  collection_of_attributes.each do |attribute|
20
21
  row = []
@@ -22,10 +23,13 @@ module Datory
22
23
  row << attribute.name
23
24
 
24
25
  from_type = attribute.options.fetch(:from)
26
+ include = attribute.options.fetch(:include, from_type)
25
27
 
26
28
  row << from_type
27
29
  row << attribute.options.fetch(:to, attribute.name)
28
- row << attribute.options.fetch(:as, attribute.options.fetch(:include, from_type))
30
+ row << attribute.options.fetch(:as, include)
31
+
32
+ row << (include if include <= Datory::Base) if collection_of_attributes.include_exist?
29
33
 
30
34
  rows << row
31
35
  end
@@ -21,28 +21,12 @@ module Datory
21
21
  @model_class.const_set(ServiceBuilder::SERVICE_CLASS_NAME, class_sample)
22
22
  end
23
23
 
24
- def create_service_class # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
24
+ def create_service_class
25
25
  collection_of_attributes = @collection_of_attributes
26
26
 
27
27
  Class.new(Datory::Service::Builder) do
28
28
  collection_of_attributes.each do |attribute|
29
- input_internal_name = attribute.options.fetch(:to, attribute.name)
30
-
31
- input attribute.name, **attribute.input_deserialization_options
32
-
33
- output input_internal_name, **attribute.output_deserialization_options
34
-
35
- make :"assign_#{input_internal_name}_output"
36
-
37
- define_method(:"assign_#{input_internal_name}_output") do
38
- value = inputs.public_send(input_internal_name)
39
-
40
- type_as = attribute.options.fetch(:as, nil)
41
-
42
- value = Datory::Utils.transform_value_with(:d, value, type_as)
43
-
44
- outputs.public_send(:"#{input_internal_name}=", value)
45
- end
29
+ prepare_deserialization_data_for(attribute)
46
30
  end
47
31
  end
48
32
  end
@@ -51,6 +51,34 @@ module Datory
51
51
  string(name, **options)
52
52
  end
53
53
 
54
+ def money(name, **options)
55
+ options_for_cents = options.merge(from: Integer)
56
+ options_for_currency = options.merge(from: [Symbol, String])
57
+
58
+ attribute(:"#{name}_cents", **options_for_cents)
59
+ attribute(:"#{name}_currency", **options_for_currency)
60
+ end
61
+
62
+ def duration(name, **options)
63
+ options = options.merge(from: String, as: ActiveSupport::Duration) # TODO: Add `format: :duration`
64
+ string(name, **options)
65
+ end
66
+
67
+ def date(name, **options)
68
+ options = options.merge(from: String, as: Date) # TODO: Add `format: :date`
69
+ string(name, **options)
70
+ end
71
+
72
+ def time(name, **options)
73
+ options = options.merge(from: String, as: Time) # TODO: Add `format: :time`
74
+ string(name, **options)
75
+ end
76
+
77
+ def datetime(name, **options)
78
+ options = options.merge(from: String, as: DateTime) # TODO: Add `format: :datetime`
79
+ string(name, **options)
80
+ end
81
+
54
82
  ########################################################################
55
83
 
56
84
  def string(name, **options)
@@ -21,28 +21,12 @@ module Datory
21
21
  @model_class.const_set(ServiceBuilder::SERVICE_CLASS_NAME, class_sample)
22
22
  end
23
23
 
24
- def create_service_class # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
24
+ def create_service_class
25
25
  collection_of_attributes = @collection_of_attributes
26
26
 
27
27
  Class.new(Datory::Service::Builder) do
28
28
  collection_of_attributes.each do |attribute|
29
- input_internal_name = attribute.options.fetch(:to, attribute.name)
30
-
31
- input input_internal_name, **attribute.input_serialization_options
32
-
33
- output attribute.name, **attribute.output_serialization_options
34
-
35
- make :"assign_#{attribute.name}_output"
36
-
37
- define_method(:"assign_#{attribute.name}_output") do
38
- value = inputs.public_send(input_internal_name)
39
-
40
- from_type = attribute.options.fetch(:from)
41
-
42
- value = Datory::Utils.transform_value_with(:s, value, from_type)
43
-
44
- outputs.public_send(:"#{attribute.name}=", value)
45
- end
29
+ prepare_serialization_data_for(attribute)
46
30
  end
47
31
  end
48
32
  end
data/lib/datory/base.rb CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  module Datory
4
4
  class Base
5
+ include Info::DSL
5
6
  include Context::DSL
6
7
  include Attributes::DSL
7
8
  end
@@ -3,7 +3,7 @@
3
3
  module Datory
4
4
  module Context
5
5
  module Callable
6
- def serialize(model)
6
+ def serialize(model) # rubocop:disable Metrics/MethodLength
7
7
  if [Set, Array].include?(model.class)
8
8
  model.map do |model_item|
9
9
  serialize(model_item)
@@ -19,7 +19,7 @@ module Datory
19
19
  raise Datory::Exceptions::SerializationError.new(message: e.message)
20
20
  end
21
21
 
22
- def deserialize(json)
22
+ def deserialize(json) # rubocop:disable Metrics/MethodLength
23
23
  if [Set, Array].include?(json.class)
24
24
  json.map do |json_item|
25
25
  deserialize(json_item)
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datory
4
+ module Info
5
+ module DSL
6
+ def self.included(base)
7
+ base.extend(ClassMethods)
8
+ end
9
+
10
+ module ClassMethods
11
+ def info # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
12
+ Datory::Info::Result.new(
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]
26
+ end
27
+ )
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datory
4
+ module Info
5
+ class Result
6
+ attr_reader :attributes
7
+
8
+ def initialize(attributes:)
9
+ @attributes = attributes
10
+ end
11
+ end
12
+ end
13
+ end
@@ -15,11 +15,15 @@ module Datory
15
15
  result_class Datory::Result
16
16
 
17
17
  input_option_helpers [
18
- Servactory::ToolKit::DynamicOptions::Format.use
18
+ Servactory::ToolKit::DynamicOptions::Format.use,
19
+ Servactory::ToolKit::DynamicOptions::Min.use,
20
+ Servactory::ToolKit::DynamicOptions::Max.use
19
21
  ]
20
22
 
21
23
  output_option_helpers [
22
- Servactory::ToolKit::DynamicOptions::Format.use
24
+ Servactory::ToolKit::DynamicOptions::Format.use,
25
+ Servactory::ToolKit::DynamicOptions::Min.use,
26
+ Servactory::ToolKit::DynamicOptions::Max.use
23
27
  ]
24
28
 
25
29
  predicate_methods_enabled false
@@ -3,6 +3,72 @@
3
3
  module Datory
4
4
  module Service
5
5
  class Builder < Base
6
+ TRANSFORMATIONS = {
7
+ SERIALIZATION: {
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) { value.to_s },
13
+ Time => ->(value) { value.to_s },
14
+ DateTime => ->(value) { value.to_s },
15
+ ActiveSupport::Duration => ->(value) { value.iso8601 }
16
+ },
17
+ DESERIALIZATION: {
18
+ Symbol => ->(value) { value.to_sym },
19
+ String => ->(value) { value.to_s },
20
+ Integer => ->(value) { value.to_i },
21
+ Float => ->(value) { value.to_f },
22
+ Date => ->(value) { Date.parse(value) },
23
+ Time => ->(value) { Time.parse(value) },
24
+ DateTime => ->(value) { DateTime.parse(value) },
25
+ ActiveSupport::Duration => ->(value) { ActiveSupport::Duration.parse(value) }
26
+ }
27
+ }.freeze
28
+
29
+ private_constant :TRANSFORMATIONS
30
+
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)
34
+ method_name = :"assign_#{serialized_name}_output"
35
+
36
+ input deserialized_name, **attribute.input_serialization_options
37
+
38
+ output serialized_name, **attribute.output_serialization_options
39
+
40
+ make method_name
41
+
42
+ define_method(method_name) do
43
+ value = inputs.public_send(deserialized_name)
44
+
45
+ value = TRANSFORMATIONS.fetch(:SERIALIZATION).fetch(value.class, ->(v) { v }).call(value)
46
+
47
+ outputs.public_send(:"#{serialized_name}=", value)
48
+ end
49
+ end
50
+
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)
54
+ method_name = :"assign_#{deserialized_name}_output"
55
+
56
+ input serialized_name, **attribute.input_deserialization_options
57
+
58
+ output deserialized_name, **attribute.output_deserialization_options
59
+
60
+ make method_name
61
+
62
+ define_method(method_name) do
63
+ value = inputs.public_send(deserialized_name)
64
+
65
+ type_as = attribute.options.fetch(:as, nil)
66
+
67
+ value = TRANSFORMATIONS.fetch(:DESERIALIZATION).fetch(type_as, ->(v) { v }).call(value)
68
+
69
+ outputs.public_send(:"#{deserialized_name}=", value)
70
+ end
71
+ end
6
72
  end
7
73
  end
8
74
  end
@@ -5,7 +5,7 @@ module Datory
5
5
  MAJOR = 1
6
6
  MINOR = 0
7
7
  PATCH = 0
8
- PRE = "rc11"
8
+ PRE = "rc12"
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.rc11
4
+ version: 1.0.0.rc12
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-26 00:00:00.000000000 Z
11
+ date: 2024-04-28 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.rc6
53
+ version: 2.5.0.rc7
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.rc6
60
+ version: 2.5.0.rc7
61
61
  - !ruby/object:Gem::Dependency
62
62
  name: terminal-table
63
63
  requirement: !ruby/object:Gem::Requirement
@@ -228,11 +228,12 @@ files:
228
228
  - lib/datory/context/workspace.rb
229
229
  - lib/datory/engine.rb
230
230
  - lib/datory/exceptions.rb
231
+ - lib/datory/info/dsl.rb
232
+ - lib/datory/info/result.rb
231
233
  - lib/datory/result.rb
232
234
  - lib/datory/service/base.rb
233
235
  - lib/datory/service/builder.rb
234
236
  - lib/datory/service/exceptions.rb
235
- - lib/datory/utils.rb
236
237
  - lib/datory/version.rb
237
238
  homepage: https://github.com/servactory/datory
238
239
  licenses:
@@ -259,7 +260,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
259
260
  - !ruby/object:Gem::Version
260
261
  version: '0'
261
262
  requirements: []
262
- rubygems_version: 3.5.6
263
+ rubygems_version: 3.5.9
263
264
  signing_key:
264
265
  specification_version: 4
265
266
  summary: A set of tools for building reliable services of any complexity
data/lib/datory/utils.rb DELETED
@@ -1,41 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Datory
4
- module Utils
5
- module_function
6
-
7
- D_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 :D_TRANSFORMATIONS
19
-
20
- S_TRANSFORMATIONS = {
21
- Symbol => ->(value, _type) { value.to_sym },
22
- String => ->(value, _type) { value.to_s },
23
- Integer => ->(value, _type) { value.to_i },
24
- Float => ->(value, _type) { value.to_f },
25
- Date => ->(value, _type) { value.to_s },
26
- Time => ->(value, _type) { value.to_s },
27
- DateTime => ->(value, _type) { value.to_s },
28
- ActiveSupport::Duration => ->(value, _type) { value.iso8601 }
29
- }.freeze
30
-
31
- private_constant :S_TRANSFORMATIONS
32
-
33
- def transform_value_with(format, value, type)
34
- if format == :d
35
- D_TRANSFORMATIONS.fetch(type, ->(v) { v }).call(value)
36
- else
37
- S_TRANSFORMATIONS.fetch(value.class, ->(v, _type) { v }).call(value, type)
38
- end
39
- end
40
- end
41
- end