treaty 0.17.0 → 0.19.0
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 +4 -4
- data/README.md +2 -2
- data/config/locales/en.yml +6 -2
- data/lib/treaty/engine.rb +1 -1
- data/lib/treaty/{attribute/entity → entity/attribute}/attribute.rb +4 -4
- data/lib/treaty/entity/attribute/base.rb +184 -0
- data/lib/treaty/entity/attribute/builder/base.rb +275 -0
- data/lib/treaty/entity/attribute/collection.rb +67 -0
- data/lib/treaty/entity/attribute/dsl.rb +92 -0
- data/lib/treaty/entity/attribute/helper_mapper.rb +74 -0
- data/lib/treaty/entity/attribute/option/base.rb +190 -0
- data/lib/treaty/entity/attribute/option/conditionals/base.rb +92 -0
- data/lib/treaty/entity/attribute/option/conditionals/if_conditional.rb +136 -0
- data/lib/treaty/entity/attribute/option/conditionals/unless_conditional.rb +153 -0
- data/lib/treaty/entity/attribute/option/modifiers/as_modifier.rb +93 -0
- data/lib/treaty/entity/attribute/option/modifiers/cast_modifier.rb +285 -0
- data/lib/treaty/entity/attribute/option/modifiers/computed_modifier.rb +128 -0
- data/lib/treaty/entity/attribute/option/modifiers/default_modifier.rb +105 -0
- data/lib/treaty/entity/attribute/option/modifiers/transform_modifier.rb +114 -0
- data/lib/treaty/entity/attribute/option/registry.rb +157 -0
- data/lib/treaty/entity/attribute/option/registry_initializer.rb +117 -0
- data/lib/treaty/entity/attribute/option/validators/format_validator.rb +222 -0
- data/lib/treaty/entity/attribute/option/validators/inclusion_validator.rb +94 -0
- data/lib/treaty/entity/attribute/option/validators/required_validator.rb +100 -0
- data/lib/treaty/entity/attribute/option/validators/type_validator.rb +219 -0
- data/lib/treaty/entity/attribute/option_normalizer.rb +168 -0
- data/lib/treaty/entity/attribute/option_orchestrator.rb +192 -0
- data/lib/treaty/entity/attribute/validation/attribute_validator.rb +147 -0
- data/lib/treaty/entity/attribute/validation/base.rb +76 -0
- data/lib/treaty/entity/attribute/validation/nested_array_validator.rb +207 -0
- data/lib/treaty/entity/attribute/validation/nested_object_validator.rb +105 -0
- data/lib/treaty/entity/attribute/validation/nested_transformer.rb +432 -0
- data/lib/treaty/entity/attribute/validation/orchestrator/base.rb +262 -0
- data/lib/treaty/entity/base.rb +90 -0
- data/lib/treaty/entity/builder.rb +44 -0
- data/lib/treaty/{info/entity → entity/info}/builder.rb +8 -8
- data/lib/treaty/{info/entity → entity/info}/dsl.rb +2 -2
- data/lib/treaty/{info/entity → entity/info}/result.rb +2 -2
- data/lib/treaty/entity.rb +7 -75
- data/lib/treaty/request/attribute/attribute.rb +1 -1
- data/lib/treaty/request/attribute/builder.rb +24 -1
- data/lib/treaty/request/entity.rb +1 -1
- data/lib/treaty/request/factory.rb +6 -6
- data/lib/treaty/request/validator.rb +1 -1
- data/lib/treaty/response/attribute/attribute.rb +1 -1
- data/lib/treaty/response/attribute/builder.rb +24 -1
- data/lib/treaty/response/entity.rb +1 -1
- data/lib/treaty/response/factory.rb +6 -6
- data/lib/treaty/response/validator.rb +1 -1
- data/lib/treaty/version.rb +1 -1
- metadata +35 -34
- data/lib/treaty/attribute/base.rb +0 -182
- data/lib/treaty/attribute/builder/base.rb +0 -143
- data/lib/treaty/attribute/collection.rb +0 -65
- data/lib/treaty/attribute/dsl.rb +0 -90
- data/lib/treaty/attribute/entity/builder.rb +0 -23
- data/lib/treaty/attribute/helper_mapper.rb +0 -72
- data/lib/treaty/attribute/option/base.rb +0 -188
- data/lib/treaty/attribute/option/conditionals/base.rb +0 -90
- data/lib/treaty/attribute/option/conditionals/if_conditional.rb +0 -134
- data/lib/treaty/attribute/option/conditionals/unless_conditional.rb +0 -151
- data/lib/treaty/attribute/option/modifiers/as_modifier.rb +0 -91
- data/lib/treaty/attribute/option/modifiers/cast_modifier.rb +0 -283
- data/lib/treaty/attribute/option/modifiers/computed_modifier.rb +0 -126
- data/lib/treaty/attribute/option/modifiers/default_modifier.rb +0 -103
- data/lib/treaty/attribute/option/modifiers/transform_modifier.rb +0 -112
- data/lib/treaty/attribute/option/registry.rb +0 -155
- data/lib/treaty/attribute/option/registry_initializer.rb +0 -115
- data/lib/treaty/attribute/option/validators/format_validator.rb +0 -220
- data/lib/treaty/attribute/option/validators/inclusion_validator.rb +0 -92
- data/lib/treaty/attribute/option/validators/required_validator.rb +0 -98
- data/lib/treaty/attribute/option/validators/type_validator.rb +0 -217
- data/lib/treaty/attribute/option_normalizer.rb +0 -166
- data/lib/treaty/attribute/option_orchestrator.rb +0 -190
- data/lib/treaty/attribute/validation/attribute_validator.rb +0 -145
- data/lib/treaty/attribute/validation/base.rb +0 -74
- data/lib/treaty/attribute/validation/nested_array_validator.rb +0 -205
- data/lib/treaty/attribute/validation/nested_object_validator.rb +0 -103
- data/lib/treaty/attribute/validation/nested_transformer.rb +0 -430
- data/lib/treaty/attribute/validation/orchestrator/base.rb +0 -260
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Treaty
|
|
4
|
+
module Entity
|
|
5
|
+
# Base class for defining reusable entity classes in Treaty.
|
|
6
|
+
#
|
|
7
|
+
# ## Purpose
|
|
8
|
+
#
|
|
9
|
+
# Treaty::Entity::Base provides a base class for creating reusable entity classes
|
|
10
|
+
# that can be used in both request and response definitions. This allows
|
|
11
|
+
# for better code organization and reusability of common data structures.
|
|
12
|
+
#
|
|
13
|
+
# ## Usage
|
|
14
|
+
#
|
|
15
|
+
# Create an entity class by inheriting from Treaty::Entity::Base:
|
|
16
|
+
#
|
|
17
|
+
# ```ruby
|
|
18
|
+
# module Posts
|
|
19
|
+
# module Create
|
|
20
|
+
# class ResponseEntity < Treaty::Entity::Base
|
|
21
|
+
# string :id
|
|
22
|
+
# string :title
|
|
23
|
+
# string :content
|
|
24
|
+
# datetime :created_at
|
|
25
|
+
# end
|
|
26
|
+
# end
|
|
27
|
+
# end
|
|
28
|
+
# ```
|
|
29
|
+
#
|
|
30
|
+
# Then use it in your treaty definitions:
|
|
31
|
+
#
|
|
32
|
+
# ```ruby
|
|
33
|
+
# class Posts::CreateTreaty < ApplicationTreaty
|
|
34
|
+
# version 1 do
|
|
35
|
+
# request Posts::Create::RequestEntity
|
|
36
|
+
# response 201, Posts::Create::ResponseEntity
|
|
37
|
+
# end
|
|
38
|
+
# end
|
|
39
|
+
# ```
|
|
40
|
+
#
|
|
41
|
+
# ## Attribute Defaults
|
|
42
|
+
#
|
|
43
|
+
# Unlike request/response blocks, Entity attributes are required by default:
|
|
44
|
+
# - All attributes have `required: true` unless explicitly marked as `:optional`
|
|
45
|
+
# - Use `:optional` helper to make attributes optional:
|
|
46
|
+
# ```ruby
|
|
47
|
+
# string :title # required by default
|
|
48
|
+
# string :summary, :optional # optional
|
|
49
|
+
# ```
|
|
50
|
+
#
|
|
51
|
+
# ## Features
|
|
52
|
+
#
|
|
53
|
+
# - **Type Safety** - Enforce strict type checking for all attributes
|
|
54
|
+
# - **Nested Structures** - Support for nested objects and arrays
|
|
55
|
+
# - **Validation** - Built-in validation for all attribute types
|
|
56
|
+
# - **Reusability** - Define once, use in multiple treaties
|
|
57
|
+
# - **Options** - Full support for attribute options (required, default, as, etc.)
|
|
58
|
+
#
|
|
59
|
+
# ## Supported Types
|
|
60
|
+
#
|
|
61
|
+
# - `string` - String values
|
|
62
|
+
# - `integer` - Integer values
|
|
63
|
+
# - `boolean` - Boolean values (true/false)
|
|
64
|
+
# - `datetime` - DateTime values
|
|
65
|
+
# - `array` - Array values (with nested type definition)
|
|
66
|
+
# - `object` - Object values (with nested attributes)
|
|
67
|
+
class Base
|
|
68
|
+
include Info::DSL
|
|
69
|
+
include Attribute::DSL
|
|
70
|
+
|
|
71
|
+
class << self
|
|
72
|
+
private
|
|
73
|
+
|
|
74
|
+
# Creates an Entity::Attribute for this Entity class
|
|
75
|
+
#
|
|
76
|
+
# @return [Entity::Attribute::Attribute] Created attribute instance
|
|
77
|
+
def create_attribute(name, type, *helpers, nesting_level:, **options, &block)
|
|
78
|
+
Attribute::Attribute.new(
|
|
79
|
+
name,
|
|
80
|
+
type,
|
|
81
|
+
*helpers,
|
|
82
|
+
nesting_level:,
|
|
83
|
+
**options,
|
|
84
|
+
&block
|
|
85
|
+
)
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Treaty
|
|
4
|
+
module Entity
|
|
5
|
+
# Entity-specific attribute builder
|
|
6
|
+
class Builder < Treaty::Entity::Attribute::Builder::Base
|
|
7
|
+
private
|
|
8
|
+
|
|
9
|
+
def create_attribute(name, type, *helpers, nesting_level:, **options, &block)
|
|
10
|
+
Treaty::Entity::Attribute::Attribute.new(
|
|
11
|
+
name,
|
|
12
|
+
type,
|
|
13
|
+
*helpers,
|
|
14
|
+
nesting_level:,
|
|
15
|
+
**options,
|
|
16
|
+
&block
|
|
17
|
+
)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Deep copies an attribute with adjusted nesting level for Entity context.
|
|
21
|
+
#
|
|
22
|
+
# @param source_attribute [Treaty::Entity::Attribute::Base] Attribute to copy
|
|
23
|
+
# @param new_nesting_level [Integer] New nesting level
|
|
24
|
+
# @return [Treaty::Entity::Attribute::Attribute] Copied attribute
|
|
25
|
+
def deep_copy_attribute(source_attribute, new_nesting_level) # rubocop:disable Metrics/MethodLength
|
|
26
|
+
copied = Treaty::Entity::Attribute::Attribute.new(
|
|
27
|
+
source_attribute.name,
|
|
28
|
+
source_attribute.type,
|
|
29
|
+
nesting_level: new_nesting_level,
|
|
30
|
+
**deep_copy_options(source_attribute.options)
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
return copied unless source_attribute.nested?
|
|
34
|
+
|
|
35
|
+
source_attribute.collection_of_attributes.each do |nested_source|
|
|
36
|
+
nested_copied = deep_copy_attribute(nested_source, new_nesting_level + 1)
|
|
37
|
+
copied.collection_of_attributes << nested_copied
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
copied
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Treaty
|
|
4
|
-
module
|
|
5
|
-
module
|
|
4
|
+
module Entity
|
|
5
|
+
module Info
|
|
6
6
|
class Builder
|
|
7
7
|
attr_reader :attributes
|
|
8
8
|
|
|
@@ -26,6 +26,12 @@ module Treaty
|
|
|
26
26
|
|
|
27
27
|
##########################################################################
|
|
28
28
|
|
|
29
|
+
def build_nested_attributes(attribute, current_level)
|
|
30
|
+
return {} unless attribute.nested?
|
|
31
|
+
|
|
32
|
+
build_versions_with(attribute.collection_of_attributes, current_level + 1)
|
|
33
|
+
end
|
|
34
|
+
|
|
29
35
|
def build_versions_with(collection, current_level = 0)
|
|
30
36
|
collection.to_h do |attribute|
|
|
31
37
|
[
|
|
@@ -38,12 +44,6 @@ module Treaty
|
|
|
38
44
|
]
|
|
39
45
|
end
|
|
40
46
|
end
|
|
41
|
-
|
|
42
|
-
def build_nested_attributes(attribute, current_level)
|
|
43
|
-
return {} unless attribute.nested?
|
|
44
|
-
|
|
45
|
-
build_versions_with(attribute.collection_of_attributes, current_level + 1)
|
|
46
|
-
end
|
|
47
47
|
end
|
|
48
48
|
end
|
|
49
49
|
end
|
data/lib/treaty/entity.rb
CHANGED
|
@@ -1,84 +1,16 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module Treaty
|
|
4
|
-
#
|
|
4
|
+
# Entity namespace containing the base class and attribute system.
|
|
5
5
|
#
|
|
6
|
-
#
|
|
6
|
+
# Users should inherit from Treaty::Entity::Base:
|
|
7
7
|
#
|
|
8
|
-
# Treaty::Entity
|
|
9
|
-
#
|
|
10
|
-
#
|
|
11
|
-
#
|
|
12
|
-
# ## Usage
|
|
13
|
-
#
|
|
14
|
-
# Create a DTO class by inheriting from Treaty::Entity:
|
|
15
|
-
#
|
|
16
|
-
# ```ruby
|
|
17
|
-
# class PostEntity < Treaty::Entity
|
|
18
|
-
# string :id
|
|
19
|
-
# string :title
|
|
20
|
-
# string :content
|
|
21
|
-
# datetime :created_at
|
|
22
|
-
# end
|
|
23
|
-
# ```
|
|
24
|
-
#
|
|
25
|
-
# Then use it in your treaty definitions:
|
|
26
|
-
#
|
|
27
|
-
# ```ruby
|
|
28
|
-
# class CreateTreaty < ApplicationTreaty
|
|
29
|
-
# version 1 do
|
|
30
|
-
# request PostEntity
|
|
31
|
-
# response 201, PostEntity
|
|
8
|
+
# class MyEntity < Treaty::Entity::Base
|
|
9
|
+
# string :name
|
|
10
|
+
# integer :age
|
|
32
11
|
# end
|
|
33
|
-
# end
|
|
34
|
-
# ```
|
|
35
|
-
#
|
|
36
|
-
# ## Attribute Defaults
|
|
37
12
|
#
|
|
38
|
-
#
|
|
39
|
-
|
|
40
|
-
# - Use `:optional` helper to make attributes optional:
|
|
41
|
-
# ```ruby
|
|
42
|
-
# string :title # required by default
|
|
43
|
-
# string :summary, :optional # optional
|
|
44
|
-
# ```
|
|
45
|
-
#
|
|
46
|
-
# ## Features
|
|
47
|
-
#
|
|
48
|
-
# - **Type Safety** - Enforce strict type checking for all attributes
|
|
49
|
-
# - **Nested Structures** - Support for nested objects and arrays
|
|
50
|
-
# - **Validation** - Built-in validation for all attribute types
|
|
51
|
-
# - **Reusability** - Define once, use in multiple treaties
|
|
52
|
-
# - **Options** - Full support for attribute options (required, default, as, etc.)
|
|
53
|
-
#
|
|
54
|
-
# ## Supported Types
|
|
55
|
-
#
|
|
56
|
-
# - `string` - String values
|
|
57
|
-
# - `integer` - Integer values
|
|
58
|
-
# - `boolean` - Boolean values (true/false)
|
|
59
|
-
# - `datetime` - DateTime values
|
|
60
|
-
# - `array` - Array values (with nested type definition)
|
|
61
|
-
# - `object` - Object values (with nested attributes)
|
|
62
|
-
class Entity
|
|
63
|
-
include Info::Entity::DSL
|
|
64
|
-
include Attribute::DSL
|
|
65
|
-
|
|
66
|
-
class << self
|
|
67
|
-
private
|
|
68
|
-
|
|
69
|
-
# Creates an Attribute::Entity::Attribute for this Entity class
|
|
70
|
-
#
|
|
71
|
-
# @return [Attribute::Entity::Attribute] Created attribute instance
|
|
72
|
-
def create_attribute(name, type, *helpers, nesting_level:, **options, &block)
|
|
73
|
-
Attribute::Entity::Attribute.new(
|
|
74
|
-
name,
|
|
75
|
-
type,
|
|
76
|
-
*helpers,
|
|
77
|
-
nesting_level:,
|
|
78
|
-
**options,
|
|
79
|
-
&block
|
|
80
|
-
)
|
|
81
|
-
end
|
|
82
|
-
end
|
|
13
|
+
# @see Treaty::Entity::Base for full documentation and examples
|
|
14
|
+
module Entity
|
|
83
15
|
end
|
|
84
16
|
end
|
|
@@ -4,7 +4,7 @@ module Treaty
|
|
|
4
4
|
module Request
|
|
5
5
|
module Attribute
|
|
6
6
|
# Request-specific attribute builder
|
|
7
|
-
class Builder < Treaty::Attribute::Builder::Base
|
|
7
|
+
class Builder < Treaty::Entity::Attribute::Builder::Base
|
|
8
8
|
private
|
|
9
9
|
|
|
10
10
|
def create_attribute(name, type, *helpers, nesting_level:, **options, &block)
|
|
@@ -17,6 +17,29 @@ module Treaty
|
|
|
17
17
|
&block
|
|
18
18
|
)
|
|
19
19
|
end
|
|
20
|
+
|
|
21
|
+
# Deep copies an attribute with adjusted nesting level for Request context.
|
|
22
|
+
#
|
|
23
|
+
# @param source_attribute [Treaty::Entity::Attribute::Base] Attribute to copy
|
|
24
|
+
# @param new_nesting_level [Integer] New nesting level
|
|
25
|
+
# @return [Request::Attribute::Attribute] Copied attribute
|
|
26
|
+
def deep_copy_attribute(source_attribute, new_nesting_level) # rubocop:disable Metrics/MethodLength
|
|
27
|
+
copied = Attribute.new(
|
|
28
|
+
source_attribute.name,
|
|
29
|
+
source_attribute.type,
|
|
30
|
+
nesting_level: new_nesting_level,
|
|
31
|
+
**deep_copy_options(source_attribute.options)
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
return copied unless source_attribute.nested?
|
|
35
|
+
|
|
36
|
+
source_attribute.collection_of_attributes.each do |nested_source|
|
|
37
|
+
nested_copied = deep_copy_attribute(nested_source, new_nesting_level + 1)
|
|
38
|
+
copied.collection_of_attributes << nested_copied
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
copied
|
|
42
|
+
end
|
|
20
43
|
end
|
|
21
44
|
end
|
|
22
45
|
end
|
|
@@ -21,14 +21,14 @@ module Treaty
|
|
|
21
21
|
# ## Entity Mode
|
|
22
22
|
#
|
|
23
23
|
# ```ruby
|
|
24
|
-
# request
|
|
24
|
+
# request Posts::Create::RequestEntity
|
|
25
25
|
# ```
|
|
26
26
|
class Factory
|
|
27
27
|
# Uses a provided Entity class
|
|
28
28
|
#
|
|
29
29
|
# @param entity_class [Class] Entity class to use
|
|
30
30
|
# @return [void]
|
|
31
|
-
# @raise [Treaty::Exceptions::Validation] if entity_class is not a valid Treaty::Entity subclass
|
|
31
|
+
# @raise [Treaty::Exceptions::Validation] if entity_class is not a valid Treaty::Entity::Base subclass
|
|
32
32
|
def use_entity(entity_class)
|
|
33
33
|
validate_entity_class!(entity_class)
|
|
34
34
|
@entity_class = entity_class
|
|
@@ -38,7 +38,7 @@ module Treaty
|
|
|
38
38
|
#
|
|
39
39
|
# @return [Collection] Collection of attributes
|
|
40
40
|
def collection_of_attributes
|
|
41
|
-
return Treaty::Attribute::Collection.new if @entity_class.nil?
|
|
41
|
+
return Treaty::Entity::Attribute::Collection.new if @entity_class.nil?
|
|
42
42
|
|
|
43
43
|
@entity_class.collection_of_attributes
|
|
44
44
|
end
|
|
@@ -62,12 +62,12 @@ module Treaty
|
|
|
62
62
|
|
|
63
63
|
private
|
|
64
64
|
|
|
65
|
-
# Validates that the provided entity_class is a valid Treaty::Entity subclass
|
|
65
|
+
# Validates that the provided entity_class is a valid Treaty::Entity::Base subclass
|
|
66
66
|
#
|
|
67
67
|
# @param entity_class [Class] Entity class to validate
|
|
68
|
-
# @raise [Treaty::Exceptions::Validation] if entity_class is not a valid Treaty::Entity subclass
|
|
68
|
+
# @raise [Treaty::Exceptions::Validation] if entity_class is not a valid Treaty::Entity::Base subclass
|
|
69
69
|
def validate_entity_class!(entity_class)
|
|
70
|
-
return if entity_class.is_a?(Class) && entity_class < Treaty::Entity
|
|
70
|
+
return if entity_class.is_a?(Class) && entity_class < Treaty::Entity::Base
|
|
71
71
|
|
|
72
72
|
raise Treaty::Exceptions::Validation,
|
|
73
73
|
I18n.t(
|
|
@@ -38,7 +38,7 @@ module Treaty
|
|
|
38
38
|
return request_data unless request_attributes_exist?
|
|
39
39
|
|
|
40
40
|
# Validate request attributes with orchestrator:
|
|
41
|
-
orchestrator_class = Class.new(Treaty::Attribute::Validation::Orchestrator::Base) do
|
|
41
|
+
orchestrator_class = Class.new(Treaty::Entity::Attribute::Validation::Orchestrator::Base) do
|
|
42
42
|
define_method(:collection_of_attributes) do
|
|
43
43
|
@version_factory.request_factory.collection_of_attributes
|
|
44
44
|
end
|
|
@@ -4,7 +4,7 @@ module Treaty
|
|
|
4
4
|
module Response
|
|
5
5
|
module Attribute
|
|
6
6
|
# Response-specific attribute builder
|
|
7
|
-
class Builder < Treaty::Attribute::Builder::Base
|
|
7
|
+
class Builder < Treaty::Entity::Attribute::Builder::Base
|
|
8
8
|
private
|
|
9
9
|
|
|
10
10
|
def create_attribute(name, type, *helpers, nesting_level:, **options, &block)
|
|
@@ -17,6 +17,29 @@ module Treaty
|
|
|
17
17
|
&block
|
|
18
18
|
)
|
|
19
19
|
end
|
|
20
|
+
|
|
21
|
+
# Deep copies an attribute with adjusted nesting level for Response context.
|
|
22
|
+
#
|
|
23
|
+
# @param source_attribute [Treaty::Entity::Attribute::Base] Attribute to copy
|
|
24
|
+
# @param new_nesting_level [Integer] New nesting level
|
|
25
|
+
# @return [Response::Attribute::Attribute] Copied attribute
|
|
26
|
+
def deep_copy_attribute(source_attribute, new_nesting_level) # rubocop:disable Metrics/MethodLength
|
|
27
|
+
copied = Attribute.new(
|
|
28
|
+
source_attribute.name,
|
|
29
|
+
source_attribute.type,
|
|
30
|
+
nesting_level: new_nesting_level,
|
|
31
|
+
**deep_copy_options(source_attribute.options)
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
return copied unless source_attribute.nested?
|
|
35
|
+
|
|
36
|
+
source_attribute.collection_of_attributes.each do |nested_source|
|
|
37
|
+
nested_copied = deep_copy_attribute(nested_source, new_nesting_level + 1)
|
|
38
|
+
copied.collection_of_attributes << nested_copied
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
copied
|
|
42
|
+
end
|
|
20
43
|
end
|
|
21
44
|
end
|
|
22
45
|
end
|
|
@@ -21,7 +21,7 @@ module Treaty
|
|
|
21
21
|
# ## Entity Mode
|
|
22
22
|
#
|
|
23
23
|
# ```ruby
|
|
24
|
-
# response 200,
|
|
24
|
+
# response 200, Posts::Create::ResponseEntity
|
|
25
25
|
# ```
|
|
26
26
|
class Factory
|
|
27
27
|
attr_reader :status
|
|
@@ -34,7 +34,7 @@ module Treaty
|
|
|
34
34
|
#
|
|
35
35
|
# @param entity_class [Class] Entity class to use
|
|
36
36
|
# @return [void]
|
|
37
|
-
# @raise [Treaty::Exceptions::Validation] if entity_class is not a valid Treaty::Entity subclass
|
|
37
|
+
# @raise [Treaty::Exceptions::Validation] if entity_class is not a valid Treaty::Entity::Base subclass
|
|
38
38
|
def use_entity(entity_class)
|
|
39
39
|
validate_entity_class!(entity_class)
|
|
40
40
|
@entity_class = entity_class
|
|
@@ -44,7 +44,7 @@ module Treaty
|
|
|
44
44
|
#
|
|
45
45
|
# @return [Collection] Collection of attributes
|
|
46
46
|
def collection_of_attributes
|
|
47
|
-
return Treaty::Attribute::Collection.new if @entity_class.nil?
|
|
47
|
+
return Treaty::Entity::Attribute::Collection.new if @entity_class.nil?
|
|
48
48
|
|
|
49
49
|
@entity_class.collection_of_attributes
|
|
50
50
|
end
|
|
@@ -68,12 +68,12 @@ module Treaty
|
|
|
68
68
|
|
|
69
69
|
private
|
|
70
70
|
|
|
71
|
-
# Validates that the provided entity_class is a valid Treaty::Entity subclass
|
|
71
|
+
# Validates that the provided entity_class is a valid Treaty::Entity::Base subclass
|
|
72
72
|
#
|
|
73
73
|
# @param entity_class [Class] Entity class to validate
|
|
74
|
-
# @raise [Treaty::Exceptions::Validation] if entity_class is not a valid Treaty::Entity subclass
|
|
74
|
+
# @raise [Treaty::Exceptions::Validation] if entity_class is not a valid Treaty::Entity::Base subclass
|
|
75
75
|
def validate_entity_class!(entity_class)
|
|
76
|
-
return if entity_class.is_a?(Class) && entity_class < Treaty::Entity
|
|
76
|
+
return if entity_class.is_a?(Class) && entity_class < Treaty::Entity::Base
|
|
77
77
|
|
|
78
78
|
raise Treaty::Exceptions::Validation,
|
|
79
79
|
I18n.t(
|
|
@@ -31,7 +31,7 @@ module Treaty
|
|
|
31
31
|
|
|
32
32
|
# Create orchestrator for response validation
|
|
33
33
|
# Orchestrator filters data by attributes and performs transformation
|
|
34
|
-
orchestrator_class = Class.new(Treaty::Attribute::Validation::Orchestrator::Base) do
|
|
34
|
+
orchestrator_class = Class.new(Treaty::Entity::Attribute::Validation::Orchestrator::Base) do
|
|
35
35
|
define_method(:collection_of_attributes) do
|
|
36
36
|
@version_factory.response_factory.collection_of_attributes
|
|
37
37
|
end
|
data/lib/treaty/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: treaty
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.19.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Anton Sokolov
|
|
@@ -147,36 +147,6 @@ files:
|
|
|
147
147
|
- Rakefile
|
|
148
148
|
- config/locales/en.yml
|
|
149
149
|
- lib/treaty.rb
|
|
150
|
-
- lib/treaty/attribute/base.rb
|
|
151
|
-
- lib/treaty/attribute/builder/base.rb
|
|
152
|
-
- lib/treaty/attribute/collection.rb
|
|
153
|
-
- lib/treaty/attribute/dsl.rb
|
|
154
|
-
- lib/treaty/attribute/entity/attribute.rb
|
|
155
|
-
- lib/treaty/attribute/entity/builder.rb
|
|
156
|
-
- lib/treaty/attribute/helper_mapper.rb
|
|
157
|
-
- lib/treaty/attribute/option/base.rb
|
|
158
|
-
- lib/treaty/attribute/option/conditionals/base.rb
|
|
159
|
-
- lib/treaty/attribute/option/conditionals/if_conditional.rb
|
|
160
|
-
- lib/treaty/attribute/option/conditionals/unless_conditional.rb
|
|
161
|
-
- lib/treaty/attribute/option/modifiers/as_modifier.rb
|
|
162
|
-
- lib/treaty/attribute/option/modifiers/cast_modifier.rb
|
|
163
|
-
- lib/treaty/attribute/option/modifiers/computed_modifier.rb
|
|
164
|
-
- lib/treaty/attribute/option/modifiers/default_modifier.rb
|
|
165
|
-
- lib/treaty/attribute/option/modifiers/transform_modifier.rb
|
|
166
|
-
- lib/treaty/attribute/option/registry.rb
|
|
167
|
-
- lib/treaty/attribute/option/registry_initializer.rb
|
|
168
|
-
- lib/treaty/attribute/option/validators/format_validator.rb
|
|
169
|
-
- lib/treaty/attribute/option/validators/inclusion_validator.rb
|
|
170
|
-
- lib/treaty/attribute/option/validators/required_validator.rb
|
|
171
|
-
- lib/treaty/attribute/option/validators/type_validator.rb
|
|
172
|
-
- lib/treaty/attribute/option_normalizer.rb
|
|
173
|
-
- lib/treaty/attribute/option_orchestrator.rb
|
|
174
|
-
- lib/treaty/attribute/validation/attribute_validator.rb
|
|
175
|
-
- lib/treaty/attribute/validation/base.rb
|
|
176
|
-
- lib/treaty/attribute/validation/nested_array_validator.rb
|
|
177
|
-
- lib/treaty/attribute/validation/nested_object_validator.rb
|
|
178
|
-
- lib/treaty/attribute/validation/nested_transformer.rb
|
|
179
|
-
- lib/treaty/attribute/validation/orchestrator/base.rb
|
|
180
150
|
- lib/treaty/base.rb
|
|
181
151
|
- lib/treaty/configuration.rb
|
|
182
152
|
- lib/treaty/context/callable.rb
|
|
@@ -185,6 +155,40 @@ files:
|
|
|
185
155
|
- lib/treaty/controller/dsl.rb
|
|
186
156
|
- lib/treaty/engine.rb
|
|
187
157
|
- lib/treaty/entity.rb
|
|
158
|
+
- lib/treaty/entity/attribute/attribute.rb
|
|
159
|
+
- lib/treaty/entity/attribute/base.rb
|
|
160
|
+
- lib/treaty/entity/attribute/builder/base.rb
|
|
161
|
+
- lib/treaty/entity/attribute/collection.rb
|
|
162
|
+
- lib/treaty/entity/attribute/dsl.rb
|
|
163
|
+
- lib/treaty/entity/attribute/helper_mapper.rb
|
|
164
|
+
- lib/treaty/entity/attribute/option/base.rb
|
|
165
|
+
- lib/treaty/entity/attribute/option/conditionals/base.rb
|
|
166
|
+
- lib/treaty/entity/attribute/option/conditionals/if_conditional.rb
|
|
167
|
+
- lib/treaty/entity/attribute/option/conditionals/unless_conditional.rb
|
|
168
|
+
- lib/treaty/entity/attribute/option/modifiers/as_modifier.rb
|
|
169
|
+
- lib/treaty/entity/attribute/option/modifiers/cast_modifier.rb
|
|
170
|
+
- lib/treaty/entity/attribute/option/modifiers/computed_modifier.rb
|
|
171
|
+
- lib/treaty/entity/attribute/option/modifiers/default_modifier.rb
|
|
172
|
+
- lib/treaty/entity/attribute/option/modifiers/transform_modifier.rb
|
|
173
|
+
- lib/treaty/entity/attribute/option/registry.rb
|
|
174
|
+
- lib/treaty/entity/attribute/option/registry_initializer.rb
|
|
175
|
+
- lib/treaty/entity/attribute/option/validators/format_validator.rb
|
|
176
|
+
- lib/treaty/entity/attribute/option/validators/inclusion_validator.rb
|
|
177
|
+
- lib/treaty/entity/attribute/option/validators/required_validator.rb
|
|
178
|
+
- lib/treaty/entity/attribute/option/validators/type_validator.rb
|
|
179
|
+
- lib/treaty/entity/attribute/option_normalizer.rb
|
|
180
|
+
- lib/treaty/entity/attribute/option_orchestrator.rb
|
|
181
|
+
- lib/treaty/entity/attribute/validation/attribute_validator.rb
|
|
182
|
+
- lib/treaty/entity/attribute/validation/base.rb
|
|
183
|
+
- lib/treaty/entity/attribute/validation/nested_array_validator.rb
|
|
184
|
+
- lib/treaty/entity/attribute/validation/nested_object_validator.rb
|
|
185
|
+
- lib/treaty/entity/attribute/validation/nested_transformer.rb
|
|
186
|
+
- lib/treaty/entity/attribute/validation/orchestrator/base.rb
|
|
187
|
+
- lib/treaty/entity/base.rb
|
|
188
|
+
- lib/treaty/entity/builder.rb
|
|
189
|
+
- lib/treaty/entity/info/builder.rb
|
|
190
|
+
- lib/treaty/entity/info/dsl.rb
|
|
191
|
+
- lib/treaty/entity/info/result.rb
|
|
188
192
|
- lib/treaty/exceptions/base.rb
|
|
189
193
|
- lib/treaty/exceptions/class_name.rb
|
|
190
194
|
- lib/treaty/exceptions/deprecated.rb
|
|
@@ -200,9 +204,6 @@ files:
|
|
|
200
204
|
- lib/treaty/exceptions/version_multiple_defaults.rb
|
|
201
205
|
- lib/treaty/exceptions/version_not_found.rb
|
|
202
206
|
- lib/treaty/executor/inventory.rb
|
|
203
|
-
- lib/treaty/info/entity/builder.rb
|
|
204
|
-
- lib/treaty/info/entity/dsl.rb
|
|
205
|
-
- lib/treaty/info/entity/result.rb
|
|
206
207
|
- lib/treaty/info/rest/builder.rb
|
|
207
208
|
- lib/treaty/info/rest/dsl.rb
|
|
208
209
|
- lib/treaty/info/rest/result.rb
|