datory 1.0.0.rc2 → 1.0.0.rc3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/datory/attributes/dsl.rb +5 -0
- data/lib/datory/attributes/serialization/serializator.rb +52 -0
- data/lib/datory/attributes/tools/service_builder.rb +33 -0
- data/lib/datory/attributes/tools/service_factory.rb +85 -0
- data/lib/datory/attributes/workspace.rb +1 -65
- data/lib/datory/context/callable.rb +4 -34
- data/lib/datory/version.rb +1 -1
- data/lib/datory.rb +1 -1
- metadata +9 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8562a68170d39d6d97a80eceae372d7f043cbb08db2890faa2d6d22a792748bf
|
4
|
+
data.tar.gz: 89ef0b5c9a73289a3567e01202a9a83beeaefee6db2f084168c6e755da4e3044
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3c6ed842a16e265979fc1d5ebc4b517390884bac7118500d39cc48d6b180c7c842b08cadfdf853db111b2a56a8c016cbcbf9654d4b290272399f461d9eec48ba
|
7
|
+
data.tar.gz: 6428856321dd65efb127aca09955e5e34eb1d3cff3cdc2f8da0fd669e4556582ecef976919b60f6808ce84a602a9dc60f332ffcc42b0421c613b0dc43d1ff429
|
@@ -29,6 +29,11 @@ module Datory
|
|
29
29
|
collection_of_attributes << Attribute.new(name, **options)
|
30
30
|
end
|
31
31
|
|
32
|
+
def symbol(name, **options)
|
33
|
+
options = options.merge(type: Symbol)
|
34
|
+
attribute(name, **options)
|
35
|
+
end
|
36
|
+
|
32
37
|
def string(name, **options)
|
33
38
|
options = options.merge(type: String)
|
34
39
|
attribute(name, **options)
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Datory
|
2
|
+
module Attributes
|
3
|
+
module Serialization
|
4
|
+
class Serializator
|
5
|
+
def self.serialize(model:, collection_of_attributes:)
|
6
|
+
new(collection_of_attributes: collection_of_attributes).serialize(model: model)
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize(collection_of_attributes:)
|
10
|
+
@collection_of_attributes = collection_of_attributes
|
11
|
+
end
|
12
|
+
|
13
|
+
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
14
|
+
def serialize(model:)
|
15
|
+
if [Set, Array].include?(model.class)
|
16
|
+
model.map do |nested_model|
|
17
|
+
serialize(model: nested_model)
|
18
|
+
end
|
19
|
+
else
|
20
|
+
@collection_of_attributes.to_h do |attribute|
|
21
|
+
internal_name = attribute.options.fetch(:as, attribute.name)
|
22
|
+
include_class = attribute.options.fetch(:include, nil)
|
23
|
+
output_formatter = attribute.options.fetch(:output, nil)
|
24
|
+
|
25
|
+
value = model.public_send(internal_name)
|
26
|
+
|
27
|
+
value =
|
28
|
+
if include_class.present?
|
29
|
+
type = attribute.options.fetch(:type, nil)
|
30
|
+
|
31
|
+
if [Set, Array].include?(type)
|
32
|
+
value.map { |item| include_class.serialize(item) }
|
33
|
+
else
|
34
|
+
include_class.serialize(value)
|
35
|
+
end
|
36
|
+
elsif output_formatter.is_a?(Proc)
|
37
|
+
output_formatter.call(value: value)
|
38
|
+
elsif [Date, Time, DateTime].include?(value.class)
|
39
|
+
value.to_s
|
40
|
+
else
|
41
|
+
value
|
42
|
+
end
|
43
|
+
|
44
|
+
[attribute.name, value]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Datory
|
4
|
+
module Attributes
|
5
|
+
module Tools
|
6
|
+
class ServiceBuilder
|
7
|
+
SERVICE_CLASS_NAME = "Builder"
|
8
|
+
|
9
|
+
def self.build!(...)
|
10
|
+
new(...).build!
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(context, incoming_attributes, collection_of_attributes)
|
14
|
+
@context = context
|
15
|
+
@incoming_attributes = incoming_attributes
|
16
|
+
@collection_of_attributes = collection_of_attributes
|
17
|
+
end
|
18
|
+
|
19
|
+
def build!
|
20
|
+
ServiceFactory.create(@context.class, @collection_of_attributes)
|
21
|
+
|
22
|
+
builder_class.call!(**@incoming_attributes)
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def builder_class
|
28
|
+
"#{@context.class.name}::#{SERVICE_CLASS_NAME}".constantize
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Datory
|
4
|
+
module Attributes
|
5
|
+
module Tools
|
6
|
+
class ServiceFactory
|
7
|
+
def self.create(...)
|
8
|
+
new(...).create
|
9
|
+
end
|
10
|
+
|
11
|
+
def initialize(model_class, collection_of_attributes)
|
12
|
+
@model_class = model_class
|
13
|
+
@collection_of_attributes = collection_of_attributes
|
14
|
+
end
|
15
|
+
|
16
|
+
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
17
|
+
def create
|
18
|
+
return if @model_class.const_defined?(ServiceBuilder::SERVICE_CLASS_NAME)
|
19
|
+
|
20
|
+
collection_of_attributes = @collection_of_attributes
|
21
|
+
|
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
|
57
|
+
|
58
|
+
make :"assign_#{input_internal_name}_output"
|
59
|
+
|
60
|
+
define_method(:"assign_#{input_internal_name}_output") do
|
61
|
+
outputs.public_send(:"#{input_internal_name}=", inputs.public_send(input_internal_name))
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
# output :model, type: Class
|
66
|
+
|
67
|
+
# make :assign_model_name
|
68
|
+
|
69
|
+
# def assign_model_name
|
70
|
+
# model_class_name_array = self.class.name.split("::")
|
71
|
+
# model_class_name_array.pop
|
72
|
+
# model_class_name = model_class_name_array.join("::")
|
73
|
+
# model_class = model_class_name.constantize
|
74
|
+
#
|
75
|
+
# outputs.model = model_class
|
76
|
+
# end
|
77
|
+
end
|
78
|
+
|
79
|
+
@model_class.const_set(ServiceBuilder::SERVICE_CLASS_NAME, class_sample)
|
80
|
+
end
|
81
|
+
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -3,76 +3,12 @@
|
|
3
3
|
module Datory
|
4
4
|
module Attributes
|
5
5
|
module Workspace
|
6
|
-
class ServiceFactory
|
7
|
-
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
8
|
-
def self.create(model_class, class_name, collection_of_attributes)
|
9
|
-
return if model_class.const_defined?(class_name)
|
10
|
-
|
11
|
-
class_sample = Class.new(Datory::Service::Builder) do
|
12
|
-
collection_of_attributes.each do |attribute| # rubocop:disable Metrics/BlockLength
|
13
|
-
input_internal_name = attribute.options.fetch(:as, attribute.name)
|
14
|
-
|
15
|
-
input attribute.name,
|
16
|
-
as: input_internal_name,
|
17
|
-
type: attribute.options.fetch(:type),
|
18
|
-
required: attribute.options.fetch(:required, true),
|
19
|
-
consists_of: attribute.options.fetch(:consists_of, false),
|
20
|
-
prepare: (lambda do |value:|
|
21
|
-
include_class = attribute.options.fetch(:include, nil)
|
22
|
-
return value unless include_class.present?
|
23
|
-
|
24
|
-
type = attribute.options.fetch(:type, nil)
|
25
|
-
|
26
|
-
if [Set, Array].include?(type)
|
27
|
-
value.map { |item| include_class.build(**item) }
|
28
|
-
else
|
29
|
-
include_class.build(**value)
|
30
|
-
end
|
31
|
-
end)
|
32
|
-
|
33
|
-
output input_internal_name,
|
34
|
-
consists_of: (
|
35
|
-
if (type = attribute.options.fetch(:consists_of, false)) == Hash
|
36
|
-
Datory::Result
|
37
|
-
else
|
38
|
-
type
|
39
|
-
end
|
40
|
-
),
|
41
|
-
type: if (type = attribute.options.fetch(:type)) == Hash
|
42
|
-
Datory::Result
|
43
|
-
else
|
44
|
-
type
|
45
|
-
end
|
46
|
-
|
47
|
-
make :"assign_#{input_internal_name}_output"
|
48
|
-
|
49
|
-
define_method(:"assign_#{input_internal_name}_output") do
|
50
|
-
outputs.public_send(:"#{input_internal_name}=", inputs.public_send(input_internal_name))
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
model_class.const_set(class_name, class_sample)
|
56
|
-
end
|
57
|
-
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
58
|
-
end
|
59
|
-
|
60
6
|
private
|
61
7
|
|
62
8
|
def build!(incoming_attributes:, collection_of_attributes:, **)
|
63
9
|
super
|
64
10
|
|
65
|
-
|
66
|
-
|
67
|
-
ServiceFactory.create(self.class, builder_class_name, collection_of_attributes)
|
68
|
-
|
69
|
-
builder_class = "#{self.class.name}::#{builder_class_name}".constantize
|
70
|
-
|
71
|
-
builder_class.call!(**incoming_attributes)
|
72
|
-
|
73
|
-
# Tools::Unnecessary.find!(self, attributes, collection_of_attributes)
|
74
|
-
# Tools::Rules.check!(self, collection_of_attributes)
|
75
|
-
# Tools::Validation.validate!(self, attributes, collection_of_attributes)
|
11
|
+
Tools::ServiceBuilder.build!(self, incoming_attributes, collection_of_attributes)
|
76
12
|
end
|
77
13
|
end
|
78
14
|
end
|
@@ -3,42 +3,12 @@
|
|
3
3
|
module Datory
|
4
4
|
module Context
|
5
5
|
module Callable
|
6
|
-
# rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
7
6
|
def serialize(model)
|
8
|
-
|
9
|
-
model
|
10
|
-
|
11
|
-
|
12
|
-
else
|
13
|
-
collection_of_attributes.to_h do |attribute|
|
14
|
-
internal_name = attribute.options.fetch(:as, attribute.name)
|
15
|
-
include_class = attribute.options.fetch(:include, nil)
|
16
|
-
output_formatter = attribute.options.fetch(:output, nil)
|
17
|
-
|
18
|
-
value = model.public_send(internal_name)
|
19
|
-
|
20
|
-
value =
|
21
|
-
if include_class.present?
|
22
|
-
type = attribute.options.fetch(:type, nil)
|
23
|
-
|
24
|
-
if [Set, Array].include?(type)
|
25
|
-
value.map { |item| include_class.serialize(item) }
|
26
|
-
else
|
27
|
-
include_class.serialize(value)
|
28
|
-
end
|
29
|
-
elsif output_formatter.is_a?(Proc)
|
30
|
-
output_formatter.call(value: value)
|
31
|
-
elsif [Date, Time, DateTime].include?(value.class)
|
32
|
-
value.to_s
|
33
|
-
else
|
34
|
-
value
|
35
|
-
end
|
36
|
-
|
37
|
-
[attribute.name, value]
|
38
|
-
end
|
39
|
-
end
|
7
|
+
Datory::Attributes::Serialization::Serializator.serialize(
|
8
|
+
model: model,
|
9
|
+
collection_of_attributes: collection_of_attributes
|
10
|
+
)
|
40
11
|
end
|
41
|
-
# rubocop:enable Metrics/MethodLength, Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
42
12
|
|
43
13
|
def deserialize(json)
|
44
14
|
if [Set, Array].include?(json.class)
|
data/lib/datory/version.rb
CHANGED
data/lib/datory.rb
CHANGED
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: 1.0.0.rc3
|
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-
|
11
|
+
date: 2024-04-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -48,16 +48,16 @@ dependencies:
|
|
48
48
|
name: servactory
|
49
49
|
requirement: !ruby/object:Gem::Requirement
|
50
50
|
requirements:
|
51
|
-
- -
|
51
|
+
- - '='
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version:
|
53
|
+
version: 2.5.0.rc2
|
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:
|
60
|
+
version: 2.5.0.rc2
|
61
61
|
- !ruby/object:Gem::Dependency
|
62
62
|
name: zeitwerk
|
63
63
|
requirement: !ruby/object:Gem::Requirement
|
@@ -199,6 +199,9 @@ files:
|
|
199
199
|
- lib/datory/attributes/attribute.rb
|
200
200
|
- lib/datory/attributes/collection.rb
|
201
201
|
- lib/datory/attributes/dsl.rb
|
202
|
+
- lib/datory/attributes/serialization/serializator.rb
|
203
|
+
- lib/datory/attributes/tools/service_builder.rb
|
204
|
+
- lib/datory/attributes/tools/service_factory.rb
|
202
205
|
- lib/datory/attributes/workspace.rb
|
203
206
|
- lib/datory/base.rb
|
204
207
|
- lib/datory/context/callable.rb
|