activeinteractor 2.0.0.alpha.2.2.0 → 2.0.0.alpha.2.3.0

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: e288a5c77ab45d9b0009db2a3d1a2997bd577207bb20f16386c8aa29be50f454
4
- data.tar.gz: 6e045fd1af8f93d2f22aeed4d96ddb44ac93f83cce65ea9237da3e2de3d167db
3
+ metadata.gz: c201330d4b6c13c9b409888776d669e0b1ea73fcae310f4869de55fc0bbcc500
4
+ data.tar.gz: 62be913b6bee629928ba114f82a7c2a526610cab1d9978459fc8e9f4f70869b3
5
5
  SHA512:
6
- metadata.gz: e22e0efcf6e53c4c70cf1b30e7871b17de7ebf13933779b989b40119ce3628eab3ade0228f1f3bbf96e7976440661cb0d4b8ec8d09f7af64d56074bf15e47ed1
7
- data.tar.gz: 7cef976acbe9bfd3ef6a9e7d24237587d4a51220b5c08b8595cdfa4522ae6e84aba8903c6ab5fadb97d1529529fb216a00eab92adc171cc3c997eb0f05c7cdda
6
+ metadata.gz: ef8f56b5ca0f79d60b9f45b3c8a1e9e394b15c15c89c98e7f747eaecf6621671020b624a0d5ba141d7948468eb83938ecd8bf62849038ad39e2f974a03bfce11
7
+ data.tar.gz: ed637132df8ef4ae80843e042d780b0423480e74f5d4cd9d12128118c10e3197467cb2e31f49d2d6a62cbadfd9c83ef4abad0beb123959ddb908d2f5f39fadc2
@@ -2,6 +2,8 @@
2
2
 
3
3
  module ActiveInteractor
4
4
  class Base
5
+ include Type::HasTypes
6
+
5
7
  class << self
6
8
  delegate :argument, :argument_names, :arguments, to: :input_context_class
7
9
  delegate :returns, :field_names, :fields, to: :output_context_class
@@ -34,13 +34,38 @@ module ActiveInteractor
34
34
  end
35
35
 
36
36
  def validate!
37
+ validate_presence! && validate_type!
38
+ end
39
+
40
+ def value
41
+ @user_provided_value || default_value
42
+ end
43
+
44
+ private
45
+
46
+ def type_is_a_active_interactor_type?
47
+ type.is_a?(ActiveInteractor::Type::Base) || type.superclass == ActiveInteractor::Type::Base
48
+ end
49
+
50
+ def validate_presence!
37
51
  return true unless required? && value.nil?
38
52
 
39
53
  error_messages << :blank
40
54
  end
41
55
 
42
- def value
43
- @user_provided_value || default_value
56
+ def validate_type!
57
+ return true if value.nil?
58
+ return true if %i[any untyped].include?(type)
59
+
60
+ if type_is_a_active_interactor_type?
61
+ return true if type.valid?(value)
62
+
63
+ error_messages << :invalid
64
+ elsif value.is_a?(type)
65
+ return true
66
+ end
67
+
68
+ error_messages << :invalid
44
69
  end
45
70
  end
46
71
  end
@@ -7,6 +7,7 @@ module ActiveInteractor
7
7
  include HasActiveModelErrors
8
8
  include AttributeRegistration
9
9
  include AttributeAssignment
10
+ include Type::HasTypes
10
11
 
11
12
  attr_reader :errors
12
13
 
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveInteractor
4
+ module Type
5
+ class Base
6
+ def self.valid?(value); end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveInteractor
4
+ module Type
5
+ class Boolean < Base
6
+ TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON', 'yes', 'YES'].freeze
7
+ FALSE_VALUES = [false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF', 'no', 'NO'].freeze
8
+
9
+ def self.valid?(value)
10
+ (TRUE_VALUES + FALSE_VALUES).include?(value)
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveInteractor
4
+ module Type
5
+ module HasTypes
6
+ extend ActiveSupport::Concern
7
+
8
+ Boolean = ActiveInteractor::Type::Boolean
9
+
10
+ module ClassMethods
11
+ def any
12
+ :any
13
+ end
14
+
15
+ def list(type)
16
+ List.new(type)
17
+ end
18
+ alias array list
19
+
20
+ def union(*types)
21
+ Union.new(*types)
22
+ end
23
+
24
+ def untyped
25
+ :untyped
26
+ end
27
+ end
28
+
29
+ included do
30
+ extend ClassMethods
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveInteractor
4
+ module Type
5
+ class List < Base
6
+ def initialize(type) # rubocop:disable Lint/MissingSuper
7
+ @type = type
8
+ end
9
+
10
+ def valid?(value)
11
+ return false unless value.is_a?(Array)
12
+
13
+ value.all? do |element|
14
+ element.nil? || element.is_a?(@type) || (@type.is_a?(ActiveInteractor::Type::Base) && @type.valid?(element))
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def value_is_a_valid_active_interactor_type?(_type, value)
21
+ unless @type.is_a?(ActiveInteractor::Type::Base) || @type.superclass == ActiveInteractor::Type::Base
22
+ return false
23
+ end
24
+
25
+ @type.valid?(value)
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveInteractor
4
+ module Type
5
+ class Union < Base
6
+ def initialize(*types) # rubocop:disable Lint/MissingSuper
7
+ @valid_types = types
8
+ end
9
+
10
+ def valid?(value)
11
+ return true if value.nil?
12
+
13
+ @valid_types.any? do |type|
14
+ value_is_a_valid_active_interactor_type?(type, value) || value.is_a?(type)
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def value_is_a_valid_active_interactor_type?(type, value)
21
+ return false unless type.is_a?(ActiveInteractor::Type::Base) || type.superclass == ActiveInteractor::Type::Base
22
+
23
+ type.valid?(value)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActiveInteractor
4
+ module Type
5
+ extend ActiveSupport::Autoload
6
+
7
+ autoload :Base
8
+ autoload :Boolean
9
+ autoload :HasTypes
10
+ autoload :List
11
+ autoload :Union
12
+ end
13
+ end
@@ -13,4 +13,5 @@ module ActiveInteractor
13
13
  autoload :Context
14
14
  autoload :HasActiveModelErrors
15
15
  autoload :Result
16
+ autoload :Type
16
17
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activeinteractor
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.alpha.2.2.0
4
+ version: 2.0.0.alpha.2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aaron Allen
@@ -76,15 +76,21 @@ files:
76
76
  - lib/active_interactor/errors.rb
77
77
  - lib/active_interactor/has_active_model_errors.rb
78
78
  - lib/active_interactor/result.rb
79
+ - lib/active_interactor/type.rb
80
+ - lib/active_interactor/type/base.rb
81
+ - lib/active_interactor/type/boolean.rb
82
+ - lib/active_interactor/type/has_types.rb
83
+ - lib/active_interactor/type/list.rb
84
+ - lib/active_interactor/type/union.rb
79
85
  - sig/active_interactor.rbs
80
86
  homepage: https://github.com/activeinteractor/activeinteractor
81
87
  licenses:
82
88
  - MIT
83
89
  metadata:
84
90
  bug_tracker_uri: https://github.com/activeinteractor/activeinteractor/issues
85
- changelog_uri: https://github.com/activeinteractor/activeinteractor/blob/v2.0.0-alpha.2.2.0/CHANGELOG.md
91
+ changelog_uri: https://github.com/activeinteractor/activeinteractor/blob/v2.0.0-alpha.2.3.0/CHANGELOG.md
86
92
  homepage_uri: https://github.com/activeinteractor/activeinteractor
87
- source_code_uri: https://github.com/activeinteractor/activeinteractor/tree/v2.0.0-alpha.2.2.0
93
+ source_code_uri: https://github.com/activeinteractor/activeinteractor/tree/v2.0.0-alpha.2.3.0
88
94
  wiki_uri: https://github.com/activeinteractor/activeinteractor/wiki
89
95
  rubygems_mfa_required: 'true'
90
96
  post_install_message: