treaty 0.18.0 → 0.20.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.
Files changed (129) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -1
  3. data/config/locales/en.yml +3 -3
  4. data/lib/treaty/action/base.rb +11 -0
  5. data/lib/treaty/action/context/callable.rb +90 -0
  6. data/lib/treaty/action/context/dsl.rb +56 -0
  7. data/lib/treaty/action/context/workspace.rb +92 -0
  8. data/lib/treaty/action/executor/inventory.rb +136 -0
  9. data/lib/treaty/{info/rest → action/info}/builder.rb +2 -2
  10. data/lib/treaty/{info/rest → action/info}/dsl.rb +2 -2
  11. data/lib/treaty/{info/rest → action/info}/result.rb +2 -2
  12. data/lib/treaty/action/inventory/collection.rb +77 -0
  13. data/lib/treaty/action/inventory/factory.rb +108 -0
  14. data/lib/treaty/action/inventory/inventory.rb +146 -0
  15. data/lib/treaty/action/request/attribute/attribute.rb +76 -0
  16. data/lib/treaty/action/request/attribute/builder.rb +98 -0
  17. data/lib/treaty/action/request/entity.rb +78 -0
  18. data/lib/treaty/action/request/factory.rb +116 -0
  19. data/lib/treaty/action/request/validator.rb +120 -0
  20. data/lib/treaty/action/response/attribute/attribute.rb +79 -0
  21. data/lib/treaty/action/response/attribute/builder.rb +96 -0
  22. data/lib/treaty/action/response/entity.rb +79 -0
  23. data/lib/treaty/action/response/factory.rb +129 -0
  24. data/lib/treaty/action/response/validator.rb +111 -0
  25. data/lib/treaty/action/result.rb +81 -0
  26. data/lib/treaty/action/versions/collection.rb +47 -0
  27. data/lib/treaty/action/versions/dsl.rb +116 -0
  28. data/lib/treaty/action/versions/execution/request.rb +287 -0
  29. data/lib/treaty/action/versions/executor.rb +61 -0
  30. data/lib/treaty/action/versions/factory.rb +253 -0
  31. data/lib/treaty/action/versions/resolver.rb +150 -0
  32. data/lib/treaty/action/versions/semantic.rb +64 -0
  33. data/lib/treaty/action/versions/workspace.rb +106 -0
  34. data/lib/treaty/action.rb +31 -0
  35. data/lib/treaty/controller/dsl.rb +1 -1
  36. data/lib/treaty/engine.rb +1 -1
  37. data/lib/treaty/{attribute/entity → entity/attribute}/attribute.rb +4 -4
  38. data/lib/treaty/entity/attribute/base.rb +184 -0
  39. data/lib/treaty/entity/attribute/builder/base.rb +275 -0
  40. data/lib/treaty/entity/attribute/collection.rb +67 -0
  41. data/lib/treaty/entity/attribute/dsl.rb +92 -0
  42. data/lib/treaty/entity/attribute/helper_mapper.rb +74 -0
  43. data/lib/treaty/entity/attribute/option/base.rb +190 -0
  44. data/lib/treaty/entity/attribute/option/conditionals/base.rb +92 -0
  45. data/lib/treaty/entity/attribute/option/conditionals/if_conditional.rb +136 -0
  46. data/lib/treaty/entity/attribute/option/conditionals/unless_conditional.rb +153 -0
  47. data/lib/treaty/entity/attribute/option/modifiers/as_modifier.rb +93 -0
  48. data/lib/treaty/entity/attribute/option/modifiers/cast_modifier.rb +285 -0
  49. data/lib/treaty/entity/attribute/option/modifiers/computed_modifier.rb +128 -0
  50. data/lib/treaty/entity/attribute/option/modifiers/default_modifier.rb +105 -0
  51. data/lib/treaty/entity/attribute/option/modifiers/transform_modifier.rb +114 -0
  52. data/lib/treaty/entity/attribute/option/registry.rb +157 -0
  53. data/lib/treaty/entity/attribute/option/registry_initializer.rb +117 -0
  54. data/lib/treaty/entity/attribute/option/validators/format_validator.rb +222 -0
  55. data/lib/treaty/entity/attribute/option/validators/inclusion_validator.rb +94 -0
  56. data/lib/treaty/entity/attribute/option/validators/required_validator.rb +100 -0
  57. data/lib/treaty/entity/attribute/option/validators/type_validator.rb +219 -0
  58. data/lib/treaty/entity/attribute/option_normalizer.rb +168 -0
  59. data/lib/treaty/entity/attribute/option_orchestrator.rb +192 -0
  60. data/lib/treaty/entity/attribute/validation/attribute_validator.rb +147 -0
  61. data/lib/treaty/entity/attribute/validation/base.rb +76 -0
  62. data/lib/treaty/entity/attribute/validation/nested_array_validator.rb +207 -0
  63. data/lib/treaty/entity/attribute/validation/nested_object_validator.rb +105 -0
  64. data/lib/treaty/entity/attribute/validation/nested_transformer.rb +432 -0
  65. data/lib/treaty/entity/attribute/validation/orchestrator/base.rb +262 -0
  66. data/lib/treaty/entity/base.rb +90 -0
  67. data/lib/treaty/entity/builder.rb +101 -0
  68. data/lib/treaty/{info/entity → entity/info}/builder.rb +8 -8
  69. data/lib/treaty/{info/entity → entity/info}/dsl.rb +2 -2
  70. data/lib/treaty/{info/entity → entity/info}/result.rb +2 -2
  71. data/lib/treaty/entity.rb +7 -79
  72. data/lib/treaty/version.rb +1 -1
  73. metadata +66 -64
  74. data/lib/treaty/attribute/base.rb +0 -182
  75. data/lib/treaty/attribute/builder/base.rb +0 -273
  76. data/lib/treaty/attribute/collection.rb +0 -65
  77. data/lib/treaty/attribute/dsl.rb +0 -90
  78. data/lib/treaty/attribute/entity/builder.rb +0 -46
  79. data/lib/treaty/attribute/helper_mapper.rb +0 -72
  80. data/lib/treaty/attribute/option/base.rb +0 -188
  81. data/lib/treaty/attribute/option/conditionals/base.rb +0 -90
  82. data/lib/treaty/attribute/option/conditionals/if_conditional.rb +0 -134
  83. data/lib/treaty/attribute/option/conditionals/unless_conditional.rb +0 -151
  84. data/lib/treaty/attribute/option/modifiers/as_modifier.rb +0 -91
  85. data/lib/treaty/attribute/option/modifiers/cast_modifier.rb +0 -283
  86. data/lib/treaty/attribute/option/modifiers/computed_modifier.rb +0 -126
  87. data/lib/treaty/attribute/option/modifiers/default_modifier.rb +0 -103
  88. data/lib/treaty/attribute/option/modifiers/transform_modifier.rb +0 -112
  89. data/lib/treaty/attribute/option/registry.rb +0 -155
  90. data/lib/treaty/attribute/option/registry_initializer.rb +0 -115
  91. data/lib/treaty/attribute/option/validators/format_validator.rb +0 -220
  92. data/lib/treaty/attribute/option/validators/inclusion_validator.rb +0 -92
  93. data/lib/treaty/attribute/option/validators/required_validator.rb +0 -98
  94. data/lib/treaty/attribute/option/validators/type_validator.rb +0 -217
  95. data/lib/treaty/attribute/option_normalizer.rb +0 -166
  96. data/lib/treaty/attribute/option_orchestrator.rb +0 -190
  97. data/lib/treaty/attribute/validation/attribute_validator.rb +0 -145
  98. data/lib/treaty/attribute/validation/base.rb +0 -74
  99. data/lib/treaty/attribute/validation/nested_array_validator.rb +0 -205
  100. data/lib/treaty/attribute/validation/nested_object_validator.rb +0 -103
  101. data/lib/treaty/attribute/validation/nested_transformer.rb +0 -430
  102. data/lib/treaty/attribute/validation/orchestrator/base.rb +0 -260
  103. data/lib/treaty/base.rb +0 -9
  104. data/lib/treaty/context/callable.rb +0 -26
  105. data/lib/treaty/context/dsl.rb +0 -12
  106. data/lib/treaty/context/workspace.rb +0 -32
  107. data/lib/treaty/executor/inventory.rb +0 -122
  108. data/lib/treaty/inventory/collection.rb +0 -71
  109. data/lib/treaty/inventory/factory.rb +0 -91
  110. data/lib/treaty/inventory/inventory.rb +0 -92
  111. data/lib/treaty/request/attribute/attribute.rb +0 -25
  112. data/lib/treaty/request/attribute/builder.rb +0 -46
  113. data/lib/treaty/request/entity.rb +0 -33
  114. data/lib/treaty/request/factory.rb +0 -81
  115. data/lib/treaty/request/validator.rb +0 -60
  116. data/lib/treaty/response/attribute/attribute.rb +0 -25
  117. data/lib/treaty/response/attribute/builder.rb +0 -46
  118. data/lib/treaty/response/entity.rb +0 -33
  119. data/lib/treaty/response/factory.rb +0 -87
  120. data/lib/treaty/response/validator.rb +0 -53
  121. data/lib/treaty/result.rb +0 -23
  122. data/lib/treaty/versions/collection.rb +0 -15
  123. data/lib/treaty/versions/dsl.rb +0 -42
  124. data/lib/treaty/versions/execution/request.rb +0 -177
  125. data/lib/treaty/versions/executor.rb +0 -14
  126. data/lib/treaty/versions/factory.rb +0 -112
  127. data/lib/treaty/versions/resolver.rb +0 -70
  128. data/lib/treaty/versions/semantic.rb +0 -22
  129. data/lib/treaty/versions/workspace.rb +0 -43
@@ -1,25 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Treaty
4
- module Response
5
- module Attribute
6
- # Response-specific attribute that defaults to required: false
7
- class Attribute < Treaty::Attribute::Base
8
- private
9
-
10
- def apply_defaults!
11
- # For response: optional by default (false).
12
- # message: nil means use I18n default message from validators
13
- @options[:required] ||= { is: false, message: nil }
14
- end
15
-
16
- def process_nested_attributes(&block)
17
- return unless object_or_array?
18
-
19
- builder = Builder.new(collection_of_attributes, @nesting_level + 1)
20
- builder.instance_eval(&block)
21
- end
22
- end
23
- end
24
- end
25
- end
@@ -1,46 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Treaty
4
- module Response
5
- module Attribute
6
- # Response-specific attribute builder
7
- class Builder < Treaty::Attribute::Builder::Base
8
- private
9
-
10
- def create_attribute(name, type, *helpers, nesting_level:, **options, &block)
11
- Attribute.new(
12
- name,
13
- type,
14
- *helpers,
15
- nesting_level:,
16
- **options,
17
- &block
18
- )
19
- end
20
-
21
- # Deep copies an attribute with adjusted nesting level for Response context.
22
- #
23
- # @param source_attribute [Treaty::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
43
- end
44
- end
45
- end
46
- end
@@ -1,33 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Treaty
4
- module Response
5
- # Entity class for response definitions.
6
- # Attributes are optional by default.
7
- #
8
- # This class is used internally when defining response blocks.
9
- # When you write a response block, Treaty creates an anonymous
10
- # class based on Response::Entity.
11
- class Entity
12
- include Treaty::Attribute::DSL
13
-
14
- class << self
15
- private
16
-
17
- # Creates a Response::Attribute::Attribute for this Response::Entity class
18
- #
19
- # @return [Response::Attribute::Attribute] Created attribute instance
20
- def create_attribute(name, type, *helpers, nesting_level:, **options, &block)
21
- Attribute::Attribute.new(
22
- name,
23
- type,
24
- *helpers,
25
- nesting_level:,
26
- **options,
27
- &block
28
- )
29
- end
30
- end
31
- end
32
- end
33
- end
@@ -1,87 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Treaty
4
- module Response
5
- # Factory for creating response definitions.
6
- #
7
- # Supports two modes:
8
- # 1. Block mode: Creates an anonymous Response::Entity class with the block
9
- # 2. Entity mode: Uses a provided Entity class directly
10
- #
11
- # ## Block Mode
12
- #
13
- # ```ruby
14
- # response 200 do
15
- # object :post do
16
- # string :id
17
- # end
18
- # end
19
- # ```
20
- #
21
- # ## Entity Mode
22
- #
23
- # ```ruby
24
- # response 200, Posts::Create::ResponseEntity
25
- # ```
26
- class Factory
27
- attr_reader :status
28
-
29
- def initialize(status)
30
- @status = status
31
- end
32
-
33
- # Uses a provided Entity class
34
- #
35
- # @param entity_class [Class] Entity class to use
36
- # @return [void]
37
- # @raise [Treaty::Exceptions::Validation] if entity_class is not a valid Treaty::Entity subclass
38
- def use_entity(entity_class)
39
- validate_entity_class!(entity_class)
40
- @entity_class = entity_class
41
- end
42
-
43
- # Returns collection of attributes from the entity class
44
- #
45
- # @return [Collection] Collection of attributes
46
- def collection_of_attributes
47
- return Treaty::Attribute::Collection.new if @entity_class.nil?
48
-
49
- @entity_class.collection_of_attributes
50
- end
51
-
52
- # Handles DSL methods for defining attributes
53
- #
54
- # This allows the factory to be used with method_missing
55
- # for backwards compatibility with direct method calls.
56
- # Creates an anonymous Response::Entity class on first use.
57
- def method_missing(type, *helpers, **options, &block)
58
- # If no entity class yet, create one
59
- @entity_class ||= Class.new(Entity)
60
-
61
- # Call the method on the entity class
62
- @entity_class.public_send(type, *helpers, **options, &block)
63
- end
64
-
65
- def respond_to_missing?(name, *)
66
- super
67
- end
68
-
69
- private
70
-
71
- # Validates that the provided entity_class is a valid Treaty::Entity subclass
72
- #
73
- # @param entity_class [Class] Entity class to validate
74
- # @raise [Treaty::Exceptions::Validation] if entity_class is not a valid Treaty::Entity subclass
75
- def validate_entity_class!(entity_class)
76
- return if entity_class.is_a?(Class) && entity_class < Treaty::Entity
77
-
78
- raise Treaty::Exceptions::Validation,
79
- I18n.t(
80
- "treaty.response.factory.invalid_entity_class",
81
- type: entity_class.class,
82
- value: entity_class
83
- )
84
- end
85
- end
86
- end
87
- end
@@ -1,53 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Treaty
4
- module Response
5
- # Validator for response data
6
- class Validator
7
- class << self
8
- # Validates response data against the response definition
9
- #
10
- # @param response_data [Hash] Response data to validate
11
- # @param version_factory [Versions::Factory] Version factory with response definition
12
- # @return [Hash] Validated and transformed response data
13
- def validate!(version_factory:, response_data: {})
14
- new(version_factory:, response_data:).validate!
15
- end
16
- end
17
-
18
- def initialize(version_factory:, response_data: {})
19
- @version_factory = version_factory
20
- @response_data = response_data
21
- end
22
-
23
- def validate!
24
- validate_response_attributes!
25
- end
26
-
27
- private
28
-
29
- def validate_response_attributes!
30
- return @response_data unless response_attributes_exist?
31
-
32
- # Create orchestrator for response validation
33
- # Orchestrator filters data by attributes and performs transformation
34
- orchestrator_class = Class.new(Treaty::Attribute::Validation::Orchestrator::Base) do
35
- define_method(:collection_of_attributes) do
36
- @version_factory.response_factory.collection_of_attributes
37
- end
38
- end
39
-
40
- orchestrator_class.validate!(
41
- version_factory: @version_factory,
42
- data: @response_data
43
- )
44
- end
45
-
46
- def response_attributes_exist?
47
- return false if @version_factory.response_factory&.collection_of_attributes&.empty?
48
-
49
- @version_factory.response_factory.collection_of_attributes.exists?
50
- end
51
- end
52
- end
53
- end
data/lib/treaty/result.rb DELETED
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Treaty
4
- class Result
5
- attr_reader :data, :status, :version
6
-
7
- def initialize(data:, status:, version:)
8
- @data = data
9
- @status = status
10
- @version = version
11
- end
12
-
13
- def inspect
14
- "#<#{self.class.name} #{draw_result}>"
15
- end
16
-
17
- private
18
-
19
- def draw_result
20
- "@data=#{@data.inspect}, @status=#{@status.inspect}, @version=#{@version.inspect}"
21
- end
22
- end
23
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Treaty
4
- module Versions
5
- class Collection
6
- extend Forwardable
7
-
8
- def_delegators :@collection, :<<, :map, :find
9
-
10
- def initialize(collection = Set.new)
11
- @collection = collection
12
- end
13
- end
14
- end
15
- end
@@ -1,42 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Treaty
4
- module Versions
5
- module DSL
6
- def self.included(base)
7
- base.extend(ClassMethods)
8
- base.include(Workspace)
9
- end
10
-
11
- module ClassMethods
12
- private
13
-
14
- def version(version, default: false, &block)
15
- @version_factory = Factory.new(version:, default:)
16
-
17
- @version_factory.instance_eval(&block)
18
- @version_factory.validate_after_block!
19
-
20
- validate_multiple_defaults! if @version_factory.default_result == true
21
-
22
- collection_of_versions << @version_factory
23
-
24
- @version_factory = nil
25
- end
26
-
27
- def collection_of_versions
28
- @collection_of_versions ||= Collection.new
29
- end
30
-
31
- def validate_multiple_defaults!
32
- existing_defaults = collection_of_versions.map(&:default_result).count(true)
33
-
34
- return if existing_defaults.zero?
35
-
36
- raise Treaty::Exceptions::VersionMultipleDefaults,
37
- I18n.t("treaty.versioning.factory.multiple_defaults")
38
- end
39
- end
40
- end
41
- end
42
- end
@@ -1,177 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Treaty
4
- module Versions
5
- module Execution
6
- class Request # rubocop:disable Metrics/ClassLength
7
- def self.execute!(...)
8
- new(...).execute!
9
- end
10
-
11
- def initialize(version_factory:, validated_params:, inventory: nil, context: nil)
12
- @inventory = inventory
13
- @context = context
14
- @version_factory = version_factory
15
- @validated_params = validated_params
16
- end
17
-
18
- def execute!
19
- raise_executor_missing_error! if @version_factory.executor.nil?
20
-
21
- extract_data_from_result
22
- end
23
-
24
- private
25
-
26
- def extract_data_from_result
27
- return execution_result if executor.is_a?(Proc)
28
- return execution_result.data if execution_result.respond_to?(:data)
29
-
30
- execution_result
31
- end
32
-
33
- ########################################################################
34
-
35
- def execution_result
36
- @execution_result ||=
37
- if executor.is_a?(Proc)
38
- execute_proc
39
- elsif servactory_service?
40
- execute_servactory
41
- else
42
- execute_regular_class
43
- end
44
- end
45
-
46
- ########################################################################
47
-
48
- def executor
49
- @executor ||= resolve_executor(@version_factory.executor.executor)
50
- end
51
-
52
- ########################################################################
53
-
54
- def resolve_executor(executor) # rubocop:disable Metrics/MethodLength
55
- return executor if executor.is_a?(Proc) || executor.is_a?(Class)
56
-
57
- if executor.is_a?(String) || executor.is_a?(Symbol)
58
- string_executor = executor.to_s
59
-
60
- if string_executor.empty?
61
- raise Treaty::Exceptions::Execution,
62
- I18n.t("treaty.execution.executor_empty")
63
- end
64
-
65
- constant_name = normalize_constant_name(executor)
66
-
67
- begin
68
- constant_name.constantize
69
- rescue NameError
70
- raise Treaty::Exceptions::Execution,
71
- I18n.t("treaty.execution.executor_not_found", class_name: constant_name)
72
- end
73
- else
74
- raise Treaty::Exceptions::Execution,
75
- I18n.t("treaty.execution.executor_invalid_type", type: executor.class)
76
- end
77
- end
78
-
79
- ########################################################################
80
-
81
- def normalize_constant_name(name)
82
- string = name.to_s
83
-
84
- return string if string.include?("::")
85
- return string.split("/").map(&:camelize).join("::") if string.include?("/")
86
-
87
- string
88
- end
89
-
90
- ########################################################################
91
- ########################################################################
92
- ########################################################################
93
-
94
- # Creates inventory wrapper with lazy evaluation
95
- #
96
- # @return [Treaty::Executor::Inventory] Inventory wrapper with method-based access
97
- def evaluated_inventory
98
- @evaluated_inventory ||= Treaty::Executor::Inventory.new(@inventory, @context)
99
- end
100
-
101
- ########################################################################
102
-
103
- def execute_proc
104
- # For Proc executors, pass inventory if collection exists
105
- executor.call(**build_call_params)
106
- rescue StandardError => e
107
- raise Treaty::Exceptions::Execution,
108
- I18n.t("treaty.execution.proc_error", message: e.message)
109
- end
110
-
111
- def execute_servactory # rubocop:disable Metrics/MethodLength
112
- # For Servactory services, pass inventory if collection exists
113
- executor.call!(**build_call_params)
114
- rescue Servactory::Exceptions::Input => e
115
- raise Treaty::Exceptions::Execution,
116
- I18n.t("treaty.execution.servactory_input_error", message: e.message)
117
- rescue Servactory::Exceptions::Internal => e
118
- raise Treaty::Exceptions::Execution,
119
- I18n.t("treaty.execution.servactory_internal_error", message: e.message)
120
- rescue Servactory::Exceptions::Output => e
121
- raise Treaty::Exceptions::Execution,
122
- I18n.t("treaty.execution.servactory_output_error", message: e.message)
123
- rescue Servactory::Exceptions::Failure => e
124
- raise Treaty::Exceptions::Execution,
125
- I18n.t("treaty.execution.servactory_failure_error", message: e.message)
126
- end
127
-
128
- def execute_regular_class # rubocop:disable Metrics/MethodLength
129
- method_name = @version_factory.executor.method
130
-
131
- unless executor.respond_to?(method_name)
132
- raise Treaty::Exceptions::Execution,
133
- I18n.t(
134
- "treaty.execution.method_not_found",
135
- method: method_name,
136
- class_name: executor
137
- )
138
- end
139
-
140
- # For regular classes, pass inventory if collection exists
141
- executor.public_send(method_name, **build_call_params)
142
- rescue StandardError => e
143
- raise Treaty::Exceptions::Execution,
144
- I18n.t("treaty.execution.regular_service_error", message: e.message)
145
- end
146
-
147
- ########################################################################
148
- ########################################################################
149
- ########################################################################
150
-
151
- # Builds call parameters hash with inventory if it exists
152
- #
153
- # @return [Hash] Parameters hash with :params and optionally :inventory
154
- def build_call_params
155
- if @inventory&.exists?
156
- { params: @validated_params, inventory: evaluated_inventory }
157
- else
158
- { params: @validated_params }
159
- end
160
- end
161
-
162
- def raise_executor_missing_error!
163
- raise Treaty::Exceptions::Execution,
164
- I18n.t(
165
- "treaty.execution.executor_missing",
166
- version: @version_factory.version
167
- )
168
- end
169
-
170
- def servactory_service?
171
- executor.respond_to?(:servactory?) &&
172
- executor.servactory?
173
- end
174
- end
175
- end
176
- end
177
- end
@@ -1,14 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Treaty
4
- module Versions
5
- class Executor
6
- attr_reader :executor, :method
7
-
8
- def initialize(executor, method)
9
- @executor = executor
10
- @method = method
11
- end
12
- end
13
- end
14
- end
@@ -1,112 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Treaty
4
- module Versions
5
- class Factory
6
- attr_reader :version,
7
- :default_result,
8
- :summary_text,
9
- :deprecated_result,
10
- :executor,
11
- :request_factory,
12
- :response_factory
13
-
14
- def initialize(version:, default:)
15
- @version = Semantic.new(version)
16
- @default_result = default.is_a?(Proc) ? default.call : default
17
- @summary_text = nil
18
- @deprecated_result = false
19
- @executor = nil
20
-
21
- validate!
22
- end
23
-
24
- def validate!
25
- validate_default_option!
26
- end
27
-
28
- def validate_after_block!
29
- validate_default_deprecated_conflict!
30
- end
31
-
32
- def summary(text)
33
- @summary_text = text
34
- end
35
-
36
- def deprecated(condition = nil)
37
- result =
38
- if condition.is_a?(Proc)
39
- condition.call
40
- elsif condition.is_a?(TrueClass) || condition.is_a?(FalseClass)
41
- condition
42
- else
43
- yield
44
- end
45
-
46
- @deprecated_result = result
47
- end
48
-
49
- def request(entity_class = nil, &block)
50
- @request_factory ||= Request::Factory.new
51
-
52
- if entity_class.present?
53
- @request_factory.use_entity(entity_class)
54
- elsif block_given?
55
- @request_factory.instance_eval(&block)
56
- end
57
- end
58
-
59
- def response(status, entity_class = nil, &block)
60
- @response_factory ||= Response::Factory.new(status)
61
-
62
- if entity_class.present?
63
- @response_factory.use_entity(entity_class)
64
- elsif block_given?
65
- @response_factory.instance_eval(&block)
66
- end
67
- end
68
-
69
- def delegate_to(executor, method = :call)
70
- @executor = Executor.new(executor, method)
71
- end
72
-
73
- ##########################################################################
74
-
75
- private
76
-
77
- def validate_default_option!
78
- if @default_result.is_a?(TrueClass) || @default_result.is_a?(FalseClass) || @default_result.is_a?(Proc)
79
- return @default_result
80
- end
81
-
82
- raise Treaty::Exceptions::Validation,
83
- I18n.t(
84
- "treaty.versioning.factory.invalid_default_option",
85
- type: @default_result.class
86
- )
87
- end
88
-
89
- def validate_default_deprecated_conflict!
90
- return unless @default_result == true
91
- return unless @deprecated_result == true
92
-
93
- raise Treaty::Exceptions::VersionDefaultDeprecatedConflict,
94
- I18n.t(
95
- "treaty.versioning.factory.default_deprecated_conflict",
96
- version: @version.version
97
- )
98
- end
99
-
100
- ##########################################################################
101
-
102
- def method_missing(name, *, &_block)
103
- raise Treaty::Exceptions::MethodName,
104
- I18n.t("treaty.versioning.factory.unknown_method", method: name)
105
- end
106
-
107
- def respond_to_missing?(name, *)
108
- super
109
- end
110
- end
111
- end
112
- end