methodist 0.1.3 → 0.1.4
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 +7 -2
- data/lib/generators/client/USAGE +9 -0
- data/lib/generators/client/client_generator.rb +28 -0
- data/lib/generators/client/templates/client.erb +10 -0
- data/lib/generators/client/templates/client_spec.erb +5 -0
- data/lib/generators/service/USAGE +4 -4
- data/lib/generators/service/templates/service.erb +3 -3
- data/lib/generators/service/templates/service_spec.erb +2 -2
- data/lib/methodist.rb +1 -1
- data/lib/methodist/client.rb +27 -0
- data/lib/methodist/interactor.rb +8 -6
- data/lib/methodist/observer.rb +4 -4
- data/lib/methodist/service.rb +1 -24
- data/lib/methodist/version.rb +1 -1
- metadata +8 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8909b4add4a45e3fda7ea10e4cd92ab6a503cbda5e3397a0a1b912e4aeae32a8
|
4
|
+
data.tar.gz: 6c1e6d01009d2994e9c1cd924dda27e5fea0f27110d80b483d8dc1f79cef7193
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8880e409b413e2e723d4a5cfb5a6dd8c74ced275b5f2be74134e47a79d7f5b76472248f35d94ccf3e8089b9a2d19b023de59a4d24e4b194a62f08d1431a3db22
|
7
|
+
data.tar.gz: 2863ebad51d939d3116f13c6075a7960201206609787b1e9c87aef7d8141608bfcac7d8ab9d18cfd38c0fede351e26b9fa04f4f30065fbee7e00fc7fed9defcb
|
data/README.md
CHANGED
@@ -6,10 +6,15 @@
|
|
6
6
|
Methodist - a gem for Ruby on Rails created to stop chaos in your buisness logic.
|
7
7
|
This gem adds generators to your rails application using some patterns:
|
8
8
|
|
9
|
-
- __Interactor__: a class for doing some complex job.
|
9
|
+
- __Interactor__: a class for doing some complex job step by step.
|
10
10
|
- __Observer__: notifies one part of an application about changes in another part of an application.
|
11
11
|
- __Builder__: is used to create an object with complex configuration (including your business logic, validation etc.)
|
12
|
-
-
|
12
|
+
- __Client__: a class with implements methods for external services (databases, APIs and etc).
|
13
|
+
For example, class called TelegramApiClient will implements methods for HTTP requests to
|
14
|
+
telegram API.
|
15
|
+
- __Service__: a class which encapsulates some business logic in semantic module.
|
16
|
+
For example, class called NotificationService - implement methods for notifications.
|
17
|
+
Or CacheService - implement methods for caching.
|
13
18
|
|
14
19
|
|
15
20
|
## Installation
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative '../methodist_generator'
|
2
|
+
|
3
|
+
class ClientGenerator < MethodistGenerator
|
4
|
+
desc 'Create client'
|
5
|
+
source_root File.expand_path('templates', __dir__)
|
6
|
+
|
7
|
+
PATTERN_FOLDER = 'clients'.freeze
|
8
|
+
TEMPLATE_FILE = 'client.erb'.freeze
|
9
|
+
TEMPLATE_SPEC_FILE = 'client_spec.erb'.freeze
|
10
|
+
|
11
|
+
class_option 'path', type: :string, desc: "Parent module for a new client", default: PATTERN_FOLDER
|
12
|
+
|
13
|
+
def generate
|
14
|
+
template(
|
15
|
+
TEMPLATE_FILE,
|
16
|
+
"#{filename_with_path}_client.rb"
|
17
|
+
)
|
18
|
+
end
|
19
|
+
|
20
|
+
def generate_spec
|
21
|
+
return unless rspec_used?
|
22
|
+
template(
|
23
|
+
TEMPLATE_SPEC_FILE,
|
24
|
+
"#{filename_with_path(prefix: 'spec')}_client_spec.rb"
|
25
|
+
)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
@@ -0,0 +1,10 @@
|
|
1
|
+
<%- unless options['clean'] -%>
|
2
|
+
# See docs to learn how to use client:
|
3
|
+
# https://github.com/QNester/methodist/tree/master/docs/client.md
|
4
|
+
<% end -%>
|
5
|
+
class <%= name.camelcase %>Client < Methodist::Client
|
6
|
+
<%- unless options['clean'] -%>
|
7
|
+
# Use the `client` method to define your client.
|
8
|
+
# Example: client Redis.new(Settings.redis.to_hash)
|
9
|
+
<%- end -%>
|
10
|
+
end
|
@@ -1,9 +1,9 @@
|
|
1
1
|
Description:
|
2
|
-
Generator for creating a
|
2
|
+
Generator for creating a Client class based on Methodist::Client
|
3
3
|
|
4
4
|
Example:
|
5
|
-
rails generate
|
5
|
+
rails generate client Client
|
6
6
|
|
7
7
|
This will create:
|
8
|
-
app/
|
9
|
-
spec/
|
8
|
+
app/clients/client.rb
|
9
|
+
spec/clients/client_spec.rb
|
@@ -1,8 +1,8 @@
|
|
1
1
|
<%- unless options['clean'] -%>
|
2
|
-
# See docs to learn how to use
|
3
|
-
# https://github.com/QNester/methodist/tree/master/docs/
|
2
|
+
# See docs to learn how to use client:
|
3
|
+
# https://github.com/QNester/methodist/tree/master/docs/client.md
|
4
4
|
<% end -%>
|
5
|
-
class <%= name.camelcase %>
|
5
|
+
class <%= name.camelcase %>Client < Methodist::Client
|
6
6
|
<%- unless options['clean'] -%>
|
7
7
|
# Use the `client` method to define your client.
|
8
8
|
# Example: client Redis.new(Settings.redis.to_hash)
|
data/lib/methodist.rb
CHANGED
@@ -0,0 +1,27 @@
|
|
1
|
+
require_relative 'pattern'
|
2
|
+
|
3
|
+
##
|
4
|
+
# == Methodist::Observer
|
5
|
+
# Base class for Methodist clients
|
6
|
+
#
|
7
|
+
#
|
8
|
+
class Methodist::Client < Methodist::Pattern
|
9
|
+
attr_reader :client
|
10
|
+
|
11
|
+
class << self
|
12
|
+
attr_reader :base_client
|
13
|
+
|
14
|
+
def client(base_client)
|
15
|
+
@base_client = base_client
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
##
|
20
|
+
# Instance method use to get defined client
|
21
|
+
##
|
22
|
+
def client
|
23
|
+
@client ||= self.class.base_client
|
24
|
+
end
|
25
|
+
|
26
|
+
class ResponseError < StandardError; end
|
27
|
+
end
|
data/lib/methodist/interactor.rb
CHANGED
@@ -13,9 +13,8 @@ class Methodist::Interactor < Methodist::Pattern
|
|
13
13
|
|
14
14
|
attr_accessor :validation_result
|
15
15
|
|
16
|
-
SCHEMA_CONST = 'SCHEMA'
|
17
|
-
|
18
16
|
class << self
|
17
|
+
attr_reader :input_schema
|
19
18
|
##
|
20
19
|
# Method set Dry::Validation schema for an interactor.
|
21
20
|
#
|
@@ -40,7 +39,7 @@ class Methodist::Interactor < Methodist::Pattern
|
|
40
39
|
##
|
41
40
|
def schema(&block)
|
42
41
|
if block_given?
|
43
|
-
|
42
|
+
@input_schema = Dry::Validation.Schema(&block)
|
44
43
|
else
|
45
44
|
raise SchemaDefinitionError, 'You must pass block to `schema`'
|
46
45
|
end
|
@@ -85,15 +84,18 @@ class Methodist::Interactor < Methodist::Pattern
|
|
85
84
|
def validate(input)
|
86
85
|
input = {} unless input
|
87
86
|
raise InputClassError, 'If you want to use custom #validate, you have to pass a hash to an interactor' unless input.is_a?(Hash)
|
88
|
-
schema
|
89
|
-
|
90
|
-
@validation_result = schema.call(input)
|
87
|
+
raise SchemaDefinitionError, 'You have to define a schema with #schema method' unless input_schema
|
88
|
+
@validation_result = input_schema.call(input)
|
91
89
|
return Success(validation_result.to_h) if validation_result.success?
|
92
90
|
Failure(failure_validation_value)
|
93
91
|
end
|
94
92
|
|
95
93
|
private
|
96
94
|
|
95
|
+
def input_schema
|
96
|
+
@input_schema ||= self.class.input_schema rescue nil
|
97
|
+
end
|
98
|
+
|
97
99
|
##
|
98
100
|
# Method for validation input of interactor parameters.
|
99
101
|
##
|
data/lib/methodist/observer.rb
CHANGED
@@ -4,9 +4,9 @@
|
|
4
4
|
#
|
5
5
|
#
|
6
6
|
class Methodist::Observer < Methodist::Pattern
|
7
|
-
CONST_EXECUTION_BLOCK = 'EXEC_BLOCK'.freeze
|
8
|
-
|
9
7
|
class << self
|
8
|
+
attr_reader :execution_block
|
9
|
+
|
10
10
|
##
|
11
11
|
# Subscribe to the instance method of the klass to observe
|
12
12
|
#
|
@@ -99,7 +99,7 @@ class Methodist::Observer < Methodist::Pattern
|
|
99
99
|
# +ExecuteBlockWasNotDefined+ - when no block was passed to the execute method in the observer class
|
100
100
|
##
|
101
101
|
def trigger!(klass, method_name, result, *args)
|
102
|
-
block =
|
102
|
+
block = execution_block rescue nil
|
103
103
|
raise ExecuteBlockWasNotDefined, "You must define execute block in your #{self.name}" unless block
|
104
104
|
block.call(klass, method_name, result, *args)
|
105
105
|
end
|
@@ -108,7 +108,7 @@ class Methodist::Observer < Methodist::Pattern
|
|
108
108
|
# Method for passing execution block for execution after observed method
|
109
109
|
##
|
110
110
|
def execute(&block)
|
111
|
-
|
111
|
+
@execution_block = block
|
112
112
|
end
|
113
113
|
|
114
114
|
def observed_methods
|
data/lib/methodist/service.rb
CHANGED
@@ -1,27 +1,4 @@
|
|
1
|
-
require_relative '
|
1
|
+
require_relative 'client'
|
2
2
|
|
3
3
|
class Methodist::Service < Methodist::Pattern
|
4
|
-
CONST_CLIENT = 'CLIENT'
|
5
|
-
|
6
|
-
class << self
|
7
|
-
##
|
8
|
-
# Define client for service
|
9
|
-
#
|
10
|
-
# ==== Parameters
|
11
|
-
# * +client_instance+ [Instance of client klass] - Instance of client class.
|
12
|
-
# It could be different clients like redis, elastic, internal api etc.
|
13
|
-
##
|
14
|
-
def client(client_instance)
|
15
|
-
const_set(CONST_CLIENT, client_instance)
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
##
|
20
|
-
# Instance method use to get defined client
|
21
|
-
##
|
22
|
-
def client
|
23
|
-
@client ||= self.class.const_get(CONST_CLIENT) #rescue nil
|
24
|
-
end
|
25
|
-
|
26
|
-
class ResponseError < StandardError; end
|
27
4
|
end
|
data/lib/methodist/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: methodist
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergey Nesterov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-02-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -110,7 +110,7 @@ dependencies:
|
|
110
110
|
version: '2.9'
|
111
111
|
description: "Methodist - a gem for Ruby on Rails created to stop chaos in your business
|
112
112
|
logic. This gem adds generators to your rails application using some patterns: interactor,
|
113
|
-
builder, observer and
|
113
|
+
builder, observer and client. Just use `rails g <pattern> <new_class_name>`. \nDocs:
|
114
114
|
https://github.com/QNester/methodist/wiki"
|
115
115
|
email:
|
116
116
|
- qnesterr@gmail.com
|
@@ -125,6 +125,10 @@ files:
|
|
125
125
|
- lib/generators/builder/builder_generator.rb
|
126
126
|
- lib/generators/builder/templates/builder.erb
|
127
127
|
- lib/generators/builder/templates/builder_spec.erb
|
128
|
+
- lib/generators/client/USAGE
|
129
|
+
- lib/generators/client/client_generator.rb
|
130
|
+
- lib/generators/client/templates/client.erb
|
131
|
+
- lib/generators/client/templates/client_spec.erb
|
128
132
|
- lib/generators/interactor/USAGE
|
129
133
|
- lib/generators/interactor/interactor_generator.rb
|
130
134
|
- lib/generators/interactor/templates/interactor.erb
|
@@ -139,6 +143,7 @@ files:
|
|
139
143
|
- lib/generators/service/templates/service_spec.erb
|
140
144
|
- lib/methodist.rb
|
141
145
|
- lib/methodist/builder.rb
|
146
|
+
- lib/methodist/client.rb
|
142
147
|
- lib/methodist/interactor.rb
|
143
148
|
- lib/methodist/observer.rb
|
144
149
|
- lib/methodist/pattern.rb
|