treaty 0.2.0 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2697d0627eb77c0671a3b71a5aacefc47acd90339214badcc68ab30ad61b96b0
4
- data.tar.gz: 32dfce4473730165c99715bbc83d2c822148fb6392bc47a098bcd7771131b748
3
+ metadata.gz: 9a2906d1588e4080f19519805a175efdc8df6178b4ea830f775b9ee1ddbd198d
4
+ data.tar.gz: 6ee3d6267f2b7df3d506ec4fcb9c8c5c7ba6a51eb813f5480ea7bfff0aaef2d1
5
5
  SHA512:
6
- metadata.gz: b6681d643573b27f56102619b50ef8f48e332b834e580f4d0dc164f0c5e298f221da2877db78d2c88853857f682b5f183b58c86e0b2d72c540a7462670d150d7
7
- data.tar.gz: 70f69a8794ff010e115fdb80c81fc954ed1bd2881509cbcbf9d714957714c298775291e03f2268633d4fc8e293820a3a3cdd93cc2cb249b3beb51ffd64029037
6
+ metadata.gz: 661e88b1f8cafb5912af30b56a31e1a39e499379bbc85edf966e0af53c22dee2907d6b4e3c6371564f2b4cbfd2f84f5f5cdbf8aff17ce909813548db0cc8b5a1
7
+ data.tar.gz: 4be479a20f9d77f59e4e6d09b67b052cf5fd474e87896d11307ece42d335d198f752a5af858f9f824d832f0507bb98add19d3b5ade406824c7cc662a07af08af
@@ -14,6 +14,7 @@ en:
14
14
  mismatch:
15
15
  integer: "Attribute '%{attribute}' must be an Integer, got %{actual}"
16
16
  string: "Attribute '%{attribute}' must be a String, got %{actual}"
17
+ boolean: "Attribute '%{attribute}' must be a Boolean (true or false), got %{actual}"
17
18
  object: "Attribute '%{attribute}' must be a Hash (object), got %{actual}"
18
19
  array: "Attribute '%{attribute}' must be an Array, got %{actual}"
19
20
  datetime: "Attribute '%{attribute}' must be a DateTime/Time/Date, got %{actual}"
@@ -10,6 +10,7 @@ module Treaty
10
10
  #
11
11
  # - `:integer` - Ruby Integer
12
12
  # - `:string` - Ruby String
13
+ # - `:boolean` - Ruby TrueClass or FalseClass
13
14
  # - `:object` - Ruby Hash (for nested objects)
14
15
  # - `:array` - Ruby Array (for collections)
15
16
  # - `:datetime` - Ruby DateTime, Time, or Date
@@ -19,6 +20,7 @@ module Treaty
19
20
  # Simple types:
20
21
  # integer :age
21
22
  # string :name
23
+ # boolean :published
22
24
  # datetime :created_at
23
25
  #
24
26
  # Nested structures:
@@ -41,7 +43,7 @@ module Treaty
41
43
  # TypeValidator doesn't use option_schema - it validates based on attribute_type.
42
44
  # This validator is always active for all attributes.
43
45
  class TypeValidator < Treaty::Attribute::Option::Base
44
- ALLOWED_TYPES = %i[integer string object array datetime].freeze
46
+ ALLOWED_TYPES = %i[integer string boolean object array datetime].freeze
45
47
 
46
48
  # Validates that the attribute type is one of the allowed types
47
49
  #
@@ -63,7 +65,7 @@ module Treaty
63
65
  # @param value [Object] The value to validate
64
66
  # @raise [Treaty::Exceptions::Validation] If value type doesn't match
65
67
  # @return [void]
66
- def validate_value!(value) # rubocop:disable Metrics/MethodLength
68
+ def validate_value!(value) # rubocop:disable Metrics/MethodLength, Metrics/CyclomaticComplexity
67
69
  return if value.nil? # Type validation doesn't check for nil, required does.
68
70
 
69
71
  case @attribute_type
@@ -71,6 +73,8 @@ module Treaty
71
73
  validate_integer!(value)
72
74
  when :string
73
75
  validate_string!(value)
76
+ when :boolean
77
+ validate_boolean!(value)
74
78
  when :object
75
79
  validate_object!(value)
76
80
  when :array
@@ -110,6 +114,20 @@ module Treaty
110
114
  actual: value.class)
111
115
  end
112
116
 
117
+ # Validates that value is a Boolean (TrueClass or FalseClass)
118
+ #
119
+ # @param value [Object] The value to validate
120
+ # @raise [Treaty::Exceptions::Validation] If value is not a Boolean
121
+ # @return [void]
122
+ def validate_boolean!(value)
123
+ return if value.is_a?(TrueClass) || value.is_a?(FalseClass)
124
+
125
+ raise Treaty::Exceptions::Validation,
126
+ I18n.t("treaty.attributes.validators.type.mismatch.boolean",
127
+ attribute: @attribute_name,
128
+ actual: value.class)
129
+ end
130
+
113
131
  # Validates that value is a Hash (object type)
114
132
  #
115
133
  # @param value [Object] The value to validate
@@ -3,18 +3,18 @@
3
3
  module Treaty
4
4
  module Context
5
5
  module Callable
6
- def call!(controller:, params:)
6
+ def call!(version:, params:)
7
7
  context = send(:new)
8
8
 
9
- _call!(context, controller:, params:)
9
+ _call!(context, version:, params:)
10
10
  end
11
11
 
12
12
  private
13
13
 
14
- def _call!(context, controller:, params:)
14
+ def _call!(context, version:, params:)
15
15
  context.send(
16
16
  :_call!,
17
- controller:,
17
+ version:,
18
18
  params:,
19
19
  collection_of_versions:
20
20
  )
@@ -6,12 +6,12 @@ module Treaty
6
6
  private
7
7
 
8
8
  def _call!(
9
- controller:,
9
+ version:,
10
10
  params:,
11
11
  collection_of_versions:
12
12
  )
13
13
  call!(
14
- controller:,
14
+ version:,
15
15
  params:,
16
16
  collection_of_versions:
17
17
  )
@@ -13,7 +13,7 @@ module Treaty
13
13
 
14
14
  def treaty(action_name)
15
15
  define_method(action_name) do
16
- treaty = treaty_class.call!(controller: self, params:)
16
+ treaty = treaty_class.call!(version: treaty_version, params:)
17
17
 
18
18
  render json: treaty.data, status: treaty.status
19
19
  end
@@ -32,6 +32,10 @@ module Treaty
32
32
  # TODO: Need to move `Treaty` to configuration.
33
33
  self.class.name.sub(/Controller$/, "::#{action_name.to_s.classify}Treaty")
34
34
  end
35
+
36
+ def treaty_version
37
+ Treaty::Engine.config.treaty.version.call(self)
38
+ end
35
39
  end
36
40
  end
37
41
  end
@@ -15,7 +15,7 @@ module Treaty
15
15
  #
16
16
  # ```ruby
17
17
  # begin
18
- # Treaty::Base.call!(controller: self, params: params)
18
+ # Treaty::Base.call!(version: treaty_version, params: params)
19
19
  # rescue Treaty::Exceptions::Base => e
20
20
  # # Catches any Treaty-specific exception
21
21
  # handle_treaty_error(e)
@@ -8,10 +8,10 @@ module Treaty
8
8
  new(...).validate!
9
9
  end
10
10
 
11
- def initialize(controller:, version_factory:)
11
+ def initialize(params:, version_factory:)
12
12
  super(version_factory:)
13
13
 
14
- @controller = controller
14
+ @params = params
15
15
  end
16
16
 
17
17
  def validate!
@@ -22,9 +22,9 @@ module Treaty
22
22
 
23
23
  def request_data
24
24
  @request_data ||= begin
25
- @controller.params.to_unsafe_h
25
+ @params.to_unsafe_h
26
26
  rescue NoMethodError
27
- @controller.params
27
+ @params
28
28
  end
29
29
  end
30
30
 
@@ -3,7 +3,7 @@
3
3
  module Treaty
4
4
  module VERSION
5
5
  MAJOR = 0
6
- MINOR = 2
6
+ MINOR = 4
7
7
  PATCH = 0
8
8
  PRE = nil
9
9
 
@@ -7,8 +7,8 @@ module Treaty
7
7
  new(...).resolve!
8
8
  end
9
9
 
10
- def initialize(controller:, collection_of_versions:)
11
- @controller = controller
10
+ def initialize(current_version:, collection_of_versions:)
11
+ @current_version = current_version
12
12
  @collection_of_versions = collection_of_versions
13
13
  end
14
14
 
@@ -27,15 +27,10 @@ module Treaty
27
27
 
28
28
  private
29
29
 
30
- def current_version
31
- @current_version ||=
32
- Treaty::Engine.config.treaty.version.call(@controller)
33
- end
34
-
35
30
  def version_factory
36
31
  @version_factory ||=
37
32
  @collection_of_versions.find do |factory|
38
- factory.version.version == current_version
33
+ factory.version.version == @current_version
39
34
  end
40
35
  end
41
36
 
@@ -45,7 +40,7 @@ module Treaty
45
40
  end
46
41
 
47
42
  def current_version_blank?
48
- current_version.to_s.strip.empty?
43
+ @current_version.to_s.strip.empty?
49
44
  end
50
45
 
51
46
  ##########################################################################
@@ -57,12 +52,12 @@ module Treaty
57
52
 
58
53
  def raise_version_not_found!
59
54
  raise Treaty::Exceptions::Validation,
60
- I18n.t("treaty.versioning.resolver.version_not_found", version: current_version)
55
+ I18n.t("treaty.versioning.resolver.version_not_found", version: @current_version)
61
56
  end
62
57
 
63
58
  def raise_version_deprecated!
64
59
  raise Treaty::Exceptions::Deprecated,
65
- I18n.t("treaty.versioning.resolver.version_deprecated", version: current_version)
60
+ I18n.t("treaty.versioning.resolver.version_deprecated", version: @current_version)
66
61
  end
67
62
  end
68
63
  end
@@ -5,16 +5,16 @@ module Treaty
5
5
  module Workspace
6
6
  private
7
7
 
8
- def call!(controller:, **) # rubocop:disable Metrics/MethodLength
8
+ def call!(version:, params:, **) # rubocop:disable Metrics/MethodLength
9
9
  super
10
10
 
11
11
  version_factory = Resolver.resolve!(
12
- controller:,
12
+ current_version: version,
13
13
  collection_of_versions: @collection_of_versions
14
14
  )
15
15
 
16
16
  validated_params = Request::Attribute::Validator.validate!(
17
- controller:,
17
+ params:,
18
18
  version_factory:
19
19
  )
20
20
 
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.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Anton Sokolov