methodist 1.1.0 → 1.1.1

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: d4b0a1df657cf96583bdecf926cfa5beb74711aa2e942a47756e3eae03712ce8
4
- data.tar.gz: ac96de08d5807f11da740f6356d246f2581c5befbe55e811b199cf6295935cd4
3
+ metadata.gz: 9a4de20033f56a487af8d2b4505604c19bc0c1262c73b81b8e0e6f7fcb3f0dce
4
+ data.tar.gz: 1c84021684ebfac6da5c09cfe87e55e83f9e104ec4ac67fc4b9e58a4c01db108
5
5
  SHA512:
6
- metadata.gz: 934db6fde4c720b1cf7777a1107967a700c710c1430aeb3952b2dbb9021458848b4f243d2f0d5740098e6707002e785a6ae29aba770e41d45c9a1bcbb3c00da6
7
- data.tar.gz: 45aea636ce515ddd5a5e8366e3d6cf337d16e18f93e675399c57cee8fcfe6003e555c1937268ee4d4eee87870590e91328d75451308470974c927125ecaba9c2
6
+ metadata.gz: cc2c157aca7127c5d6f8815ea9324dbce2341b5862b377b77bdff553276aa58275e46661c71a7b710d2119a5ebb9e13d160f8b2f68c7c6ee9e306fb03af259af
7
+ data.tar.gz: 1d592b18cc8befb21003dea5d43970acfe8fa258d251076f593eb0f83c722f759e4b7b91abca893cad1b1096004a635d1d9b494a63cc07b78d0cc8aa4e53ca31
@@ -7,8 +7,10 @@ class BuilderGenerator < MethodistGenerator
7
7
  PATTERN_FOLDER = 'builders'.freeze
8
8
  TEMPLATE_FILE = 'builder.erb'.freeze
9
9
  TEMPLATE_SPEC_FILE = 'builder_spec.erb'.freeze
10
+ PARENT_CLASS = 'Methodist::Builder'.freeze
10
11
 
11
12
  class_option 'path', type: :string, desc: "Parent module for a new builder", default: PATTERN_FOLDER
13
+ class_option 'parent', type: :string, desc: "Parent class of generated class", default: PARENT_CLASS
12
14
 
13
15
  def generate
14
16
  template(
@@ -7,8 +7,10 @@ class ClientGenerator < MethodistGenerator
7
7
  PATTERN_FOLDER = 'clients'.freeze
8
8
  TEMPLATE_FILE = 'client.erb'.freeze
9
9
  TEMPLATE_SPEC_FILE = 'client_spec.erb'.freeze
10
+ PARENT_CLASS = 'Methodist::Client'.freeze
10
11
 
11
12
  class_option 'path', type: :string, desc: "Parent module for a new client", default: PATTERN_FOLDER
13
+ class_option 'parent', type: :string, desc: "Parent class of generated class", default: PARENT_CLASS
12
14
 
13
15
  def generate
14
16
  template(
@@ -7,9 +7,11 @@ class InteractorGenerator < MethodistGenerator
7
7
  PATTERN_FOLDER = 'interactors'.freeze
8
8
  TEMPLATE_FILE = 'interactor.erb'.freeze
9
9
  TEMPLATE_SPEC_FILE = 'interactor_spec.erb'.freeze
10
+ PARENT_CLASS = 'Methodist::Interactor'.freeze
10
11
 
11
12
  class_option 'skip-validations', type: :boolean, desc: "Skip validations in generated files", default: false
12
13
  class_option 'path', type: :string, desc: "Parent module for a new interactor", default: PATTERN_FOLDER
14
+ class_option 'parent', type: :string, desc: "Parent class of generated class", default: PARENT_CLASS
13
15
 
14
16
  def generate
15
17
  template(
@@ -2,7 +2,7 @@
2
2
  # Methodist interactors use dry-transaction.
3
3
  # Check docs here: https://github.com/dry-rb/dry-transaction
4
4
  <% end -%>
5
- class <%= name.camelcase %> < Methodist::Interactor
5
+ class <%= name.camelcase %> < <%= options['parent'] %>
6
6
  <% unless options['skip-validations'] %>
7
7
  # See here the syntax for validation http://dry-rb.org/gems/dry-validation/
8
8
  schema do
@@ -8,9 +8,11 @@ class ObserverGenerator < MethodistGenerator
8
8
  TEMPLATE_FILE = 'observer.erb'.freeze
9
9
  TEMPLATE_SPEC_FILE = 'observer_spec.erb'.freeze
10
10
  DEFAULT_PREFIX = 'config/initializers'.freeze
11
+ PARENT_CLASS = 'Methodist::Observer'.freeze
11
12
 
12
13
  class_option 'skip-validations', type: :boolean, desc: "Skip validations in generated files", default: false
13
14
  class_option 'path', type: :string, desc: "Parent module for new a interactor", default: PATTERN_FOLDER
15
+ class_option 'parent', type: :string, desc: "Parent class of generated class", default: PARENT_CLASS
14
16
 
15
17
  def generate
16
18
  template(
@@ -7,8 +7,10 @@ class ServiceGenerator < MethodistGenerator
7
7
  PATTERN_FOLDER = 'services'.freeze
8
8
  TEMPLATE_FILE = 'service.erb'.freeze
9
9
  TEMPLATE_SPEC_FILE = 'service_spec.erb'.freeze
10
+ PARENT_CLASS = 'Methodist::Service'.freeze
10
11
 
11
12
  class_option 'path', type: :string, desc: "Parent module for a new service", default: PATTERN_FOLDER
13
+ class_option 'parent', type: :string, desc: "Parent class of generated class", default: PARENT_CLASS
12
14
 
13
15
  def generate
14
16
  template(
@@ -15,7 +15,7 @@ class Methodist::Interactor < Methodist::Pattern
15
15
  attr_accessor :validation_result
16
16
 
17
17
  class << self
18
- attr_reader :input_schema
18
+ attr_reader :input_schema, :contract_class
19
19
  ##
20
20
  # Method set Dry::Validation schema for an interactor.
21
21
  #
@@ -46,6 +46,55 @@ class Methodist::Interactor < Methodist::Pattern
46
46
  end
47
47
  end
48
48
 
49
+ ##
50
+ # ATTENTION: require dry-validation >= 1.0.0
51
+ # Method set Dry::Validation::Contract contract for an interactor.
52
+ #
53
+ # Receive block for generating Dry::Validation:Contract
54
+ # result.
55
+ #
56
+ # class InteractorClass < Methodist::Interactor
57
+ # contract do
58
+ # params do
59
+ # required(:name).value(:str?)
60
+ # end
61
+ #
62
+ # rule(:name) do
63
+ # ...
64
+ # end
65
+ # end
66
+ #
67
+ # step :validate
68
+ # end
69
+ #
70
+ # InteractorClass.new.call(name: nil) #=> Failure(ValidationError)
71
+ #
72
+ # Or you can pass contract class:
73
+ # class InteractorClass < Methodist::Interactor
74
+ # contract contract_class: MyContract
75
+ #
76
+ # step :validate
77
+ # end
78
+ #
79
+ # ==== See
80
+ # http://dry-rb.org/gems/dry-validation/
81
+ #
82
+ # https://github.com/dry-rb/dry-transaction
83
+ ##
84
+ def contract(contract_class: nil, &block)
85
+ unless new_dry_validation?
86
+ raise DryValidationVersionError,
87
+ "Your depended dry-validation gem version must be gteq #{NEW_DRY_VALIDATION_V}"
88
+ end
89
+
90
+ unless block_given? || contract_class
91
+ raise ContractDefinitionError, 'You must pass block or contract_class to `contract`'
92
+ end
93
+
94
+ @contract_class = contract_class
95
+ @contract_class ||= Class.new(Dry::Validation::Contract, &block)
96
+ end
97
+
49
98
  private
50
99
 
51
100
  def dry_schema(&block)
@@ -96,8 +145,9 @@ class Methodist::Interactor < Methodist::Pattern
96
145
  def validate(input)
97
146
  input = {} unless input
98
147
  raise InputClassError, 'If you want to use custom #validate, you have to pass a hash to an interactor' unless input.is_a?(Hash)
99
- raise SchemaDefinitionError, 'You have to define a schema with #schema method' unless input_schema
100
- @validation_result = input_schema.call(input)
148
+ validator = contract_class ? contract_class : input_schema
149
+ raise ValidatorDefinitionError, 'you must define schema via #schema OR define contract via #contract' unless validator
150
+ @validation_result = validator.call(input)
101
151
  return Success(validation_result.to_h) if validation_result.success?
102
152
  Failure(failure_validation_value)
103
153
  end
@@ -108,6 +158,10 @@ class Methodist::Interactor < Methodist::Pattern
108
158
  @input_schema ||= self.class.input_schema rescue nil
109
159
  end
110
160
 
161
+ def contract_class
162
+ @contract_class ||= self.class.contract_class.new rescue nil
163
+ end
164
+
111
165
  ##
112
166
  # Method for validation input of interactor parameters.
113
167
  ##
@@ -120,6 +174,8 @@ class Methodist::Interactor < Methodist::Pattern
120
174
  }
121
175
  end
122
176
 
123
- class SchemaDefinitionError < StandardError; end
177
+ class ValidatorDefinitionError < StandardError; end
124
178
  class InputClassError < StandardError; end
179
+ class DryValidationVersionError < StandardError; end
180
+ class ContractDefinitionError < StandardError; end
125
181
  end
@@ -1,3 +1,3 @@
1
1
  module Methodist
2
- VERSION = '1.1.0'
2
+ VERSION = '1.1.1'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: methodist
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergey Nesterov