methodist 0.1.3 → 0.1.4

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: 1e1231c5c1338d3a4f5c7db47db5ab36cdd4d032f4eb97ad10e50798397e4580
4
- data.tar.gz: 9c1afa1729571899206394d775f96ba25366f5c9565b43d55fe80c9c3f7f8edf
3
+ metadata.gz: 8909b4add4a45e3fda7ea10e4cd92ab6a503cbda5e3397a0a1b912e4aeae32a8
4
+ data.tar.gz: 6c1e6d01009d2994e9c1cd924dda27e5fea0f27110d80b483d8dc1f79cef7193
5
5
  SHA512:
6
- metadata.gz: 9fcb1943a5af08b284ab7fc16c74d78949a466f2e1a8dca4f910ca8d8c2e883848137e9fbe2d6cf04ee55148b36efced176770f68dc9a0ef120f63b11c95fe26
7
- data.tar.gz: 1d09ecd42b89e86c0c7362987ee23809100e9c70e9d83fd7cac1e4bea4963493db50bb50573ab118ae150f843cebe55fd6b3d64eb4a1c73ee0fdd4b6a360d2af
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
- - __Service__: a class with collection of methods. Useful when using internal services.
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,9 @@
1
+ Description:
2
+ Generator for creating a Client class based on Methodist::Client
3
+
4
+ Example:
5
+ rails generate client Client
6
+
7
+ This will create:
8
+ app/clients/client.rb
9
+ spec/clients/client_spec.rb
@@ -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
@@ -0,0 +1,5 @@
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe <%= name.camelcase %>Client do
4
+ it 'test your client'
5
+ end
@@ -1,9 +1,9 @@
1
1
  Description:
2
- Generator for creating a Service class based on Methodist::Service
2
+ Generator for creating a Client class based on Methodist::Client
3
3
 
4
4
  Example:
5
- rails generate builder Service
5
+ rails generate client Client
6
6
 
7
7
  This will create:
8
- app/builders/service.rb
9
- spec/builders/service_spec.rb
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 service:
3
- # https://github.com/QNester/methodist/tree/master/docs/service.md
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 %>Service < Methodist::Service
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)
@@ -1,5 +1,5 @@
1
1
  require 'rails_helper'
2
2
 
3
- RSpec.describe <%= name.camelcase %>Service do
4
- it 'test your service'
3
+ RSpec.describe <%= name.camelcase %>Client do
4
+ it 'test your client'
5
5
  end
@@ -4,7 +4,7 @@ require 'methodist/pattern'
4
4
  require 'methodist/interactor'
5
5
  require 'methodist/observer'
6
6
  require 'methodist/builder'
7
- require 'methodist/service'
7
+ require 'methodist/client'
8
8
 
9
9
  module Methodist
10
10
  end
@@ -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
@@ -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
- const_set SCHEMA_CONST, Dry::Validation.Schema(&block)
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 = self.class.const_get SCHEMA_CONST rescue nil
89
- raise SchemaDefinitionError, 'You have to define a schema with #schema method' unless schema
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
  ##
@@ -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 = const_get(CONST_EXECUTION_BLOCK) rescue nil
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
- const_set(CONST_EXECUTION_BLOCK, block)
111
+ @execution_block = block
112
112
  end
113
113
 
114
114
  def observed_methods
@@ -1,27 +1,4 @@
1
- require_relative 'pattern'
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
@@ -1,3 +1,3 @@
1
1
  module Methodist
2
- VERSION = '0.1.3'
2
+ VERSION = '0.1.4'
3
3
  end
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.3
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-01-25 00:00:00.000000000 Z
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 service. Just use `rails g <pattern> <new_class_name>`. \nDocs:
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