strum-service 0.0.1 → 0.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: 23dfc7b648bc76174a823745ed28d035fa85de556dff129309c9f55e83066a81
4
- data.tar.gz: 6561b68e156b33c3d2d4d391e6a838586c8036dd32bce7bd77ba702fb54de56a
3
+ metadata.gz: f205a707f1b5fe69e7c7939c806739a54550639cb60b99be68991387574999a6
4
+ data.tar.gz: 1027394e5b3f37086596e7835d8658f62e42a48439fdf4747b2426064f8eaf70
5
5
  SHA512:
6
- metadata.gz: 02e2d61a36385cf3e76fcccbaba442681505461c082fb7b4d6821afad7a69815badbef25f4bafd6aba45f47ccc98260b6caf82037b12e3d8194286bd0f741f47
7
- data.tar.gz: d1667dfc277014273ca400abd4b1834b83c75c91d4423862ec457aa8db269ea0b4525cef9f6bc71d66b843cd60e013f6d9fdd40022c563c2f741010052ca9bb1
6
+ metadata.gz: 06632a862034f79eaca80df73925ff73e7e93c5d292942a11f735a2fa97d00422736b807ba9e6578b48a4b4613ce3e218dd243d33e493238178eb04ce4c19b01
7
+ data.tar.gz: 484ab260dd2cbc83889fc7fddc9f39ab197a52f882df3ea6ba71aea03834c889e6619b22f46821b3ea546d4e28475497d0af80800e0f634a2d26239bb61de7fe
data/.gitlab-ci.yml ADDED
@@ -0,0 +1,3 @@
1
+ include:
2
+ - project: 'strum-rb/ci-cd'
3
+ file: '/strum-rb/.gitlab-ci.yml'
data/.rubocop.yml CHANGED
@@ -12,18 +12,15 @@ Layout/IndentationConsistency:
12
12
  Enabled: true
13
13
  EnforcedStyle: indented_internal_methods
14
14
 
15
- # Two spaces, no tabs (for indentation).
16
- Layout/IndentationWidth:
17
- Enabled: true
18
-
19
15
  # Check quotes usage according to lint rule below.
20
16
  Style/StringLiterals:
21
17
  Enabled: true
22
18
  EnforcedStyle: double_quotes
23
19
 
24
- Metrics/LineLength:
25
- Max: 120
20
+ Layout/LineLength:
26
21
  IgnoredPatterns: ['\A#']
27
22
 
28
23
  Metrics/BlockLength:
29
- ExcludedMethods: ['describe', 'context']
24
+ IgnoredMethods:
25
+ - describe
26
+ - context
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- strum-service (0.0.1)
4
+ strum-service (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/lib/strum/service.rb CHANGED
@@ -3,7 +3,8 @@
3
3
  require "strum/service/version"
4
4
 
5
5
  module Strum
6
- module Service
6
+ # Strum service
7
+ module Service # rubocop:disable Metrics/ModuleLength
7
8
  class Failure < StandardError; end
8
9
 
9
10
  def self.included(base)
@@ -12,26 +13,40 @@ module Strum
12
13
  end
13
14
  end
14
15
 
16
+ # Define coercive methods
17
+ COERCIVE_METHODS = %i[add_error add_errors any required sliced sliced_list].freeze
18
+
19
+ COERCIVE_METHODS.each do |m|
20
+ define_method("#{m}!") do |*args, **kwargs, &block|
21
+ send(m, *args, **kwargs, &block)
22
+ throw :exit unless valid?
23
+ end
24
+ end
25
+
15
26
  # Internal: Interactor class methods.
16
27
  module ClassMethods
17
- def call(_input, **args, &block)
18
- new(_input, args).execute(&block)
28
+ def call(main_input, **args, &block)
29
+ new(main_input, **args).execute(&block)
19
30
  end
20
31
  end
21
32
 
22
33
  # Instance methods
23
- def initialize(_input, **args)
34
+ def initialize(main_input, **args)
24
35
  self.strum_errors = {}
25
36
  self.service_handlers = { on: {}, success: {}, failure: {} }
26
37
  self.outputs = {}
27
- self.inputs = args.merge(default: _input)
38
+ self.inputs = args.merge(default: main_input)
28
39
  self._inputs = inputs.dup.freeze
40
+ setup_default_proc
41
+ setup_input_default_proc
29
42
  end
30
43
 
31
- def execute
32
- audit
33
- yield(self) if block_given?
34
- call if valid?
44
+ def execute(&block)
45
+ catch(:exit) do
46
+ audit
47
+ instance_exec(self, &block) if block_given?
48
+ call if valid?
49
+ end
35
50
  valid? ? valid_result : invalid_result
36
51
  end
37
52
 
@@ -60,9 +75,10 @@ module Strum
60
75
  end
61
76
 
62
77
  def method_missing(method_name, *args, &_block)
63
- super unless input.is_a?(Hash) && input[method_name.to_sym]
64
-
65
- args.count.positive? ? public_send(input[method_name.to_sym], args) : input[method_name.to_sym]
78
+ (args.count.zero? &&
79
+ (input.is_a?(Hash) && input[method_name.to_sym]) ||
80
+ inputs[method_name.to_sym]) ||
81
+ super
66
82
  end
67
83
 
68
84
  def respond_to_missing?(method_name, include_private = false)
@@ -105,16 +121,21 @@ module Strum
105
121
  end
106
122
  # rubocop: enable Style/OptionalArguments
107
123
 
124
+ # def add_error(field, value)
125
+ # if field.is_a?(Array)
126
+ # field.each do |key|
127
+ # value.is_a?(Array) ? value.each { |v| add_error(key, v) } : add_error(key, value)
128
+ # add_error(key, value.is_a?(Array) ? value.first : value)
129
+ # end
130
+ # else
131
+ # strum_errors[field] ||= []
132
+ # strum_errors[field] = strum_errors[field] + Array(value)
133
+ # end
134
+ # end
135
+
108
136
  def add_error(field, value)
109
- if field.is_a?(Array)
110
- field.each do |key|
111
- value.is_a?(Array) ? value.each { |v| add_error(key, v) } : add_error(key, value)
112
- add_error(key, value.is_a?(Array) ? value.first : value)
113
- end
114
- else
115
- strum_errors[field] ||= []
116
- strum_errors[field] = strum_errors[field] + Array(value)
117
- end
137
+ strum_errors[field] ||= []
138
+ strum_errors[field] += Array(value)
118
139
  end
119
140
 
120
141
  def add_errors(errors)
@@ -146,16 +167,14 @@ module Strum
146
167
  end
147
168
 
148
169
  def sliced_list(*keys)
149
- if input.is_a?(Array)
150
- self.input = input.map do |item|
151
- if item.is_a?(Hash)
152
- item = item.slice(*keys)
153
- else
154
- add_error(:input_subitem, :must_be_hash)
155
- end
170
+ add_error(:input, :must_be_array) && retuen unless input.is_a?(Array)
171
+
172
+ self.input = input.map do |item|
173
+ if item.is_a?(Hash)
174
+ item.slice(*keys)
175
+ else
176
+ add_error(:input_subitem, :must_be_hash)
156
177
  end
157
- else
158
- add_error(:input, :must_be_array)
159
178
  end
160
179
  end
161
180
 
@@ -165,16 +184,34 @@ module Strum
165
184
 
166
185
  private
167
186
 
187
+ def setup_default_proc
188
+ inputs.default_proc = proc { |h, k| h.key?(k.to_s) ? h[k.to_s] : nil }
189
+ end
190
+
191
+ def setup_input_default_proc
192
+ input.default_proc = proc { |h, k| h.key?(k.to_s) ? h[k.to_s] : nil } if input.is_a?(Hash)
193
+ end
194
+
168
195
  def key_to_sym(key)
169
196
  key.to_sym
170
197
  rescue StandardError
171
198
  key
172
199
  end
173
200
 
201
+ def handler_key
202
+ ((outputs.keys << nil) & service_handlers[:success].keys).first
203
+ end
204
+
205
+ def handler
206
+ service_handlers[:success][handler_key]
207
+ end
208
+
174
209
  def valid_result
175
- handler_key = ((outputs.keys << nil) & service_handlers[:success].keys).first
176
- handler = service_handlers[:success][handler_key]
177
- handler.is_a?(Proc) ? handler.call(outputs[handler_key] || outputs[:default]) : outputs[handler_key] || outputs[:default]
210
+ if handler.is_a?(Proc)
211
+ handler.call(outputs[handler_key] || outputs[:default])
212
+ else
213
+ outputs[handler_key] || outputs[:default]
214
+ end
178
215
  end
179
216
 
180
217
  def invalid_result
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Strum
4
4
  module Service
5
- VERSION = "0.0.1"
5
+ VERSION = "0.1.1"
6
6
  end
7
7
  end
@@ -10,15 +10,16 @@ Gem::Specification.new do |spec|
10
10
 
11
11
  spec.summary = "Light ruby framework"
12
12
  spec.description = "Strum Service"
13
- spec.homepage = "https://code.qpard.com/strum/strum-service"
13
+ spec.homepage = "https://gitlab.com/strum-rb/strum-service"
14
14
  spec.license = "MIT"
15
15
  spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")
16
16
 
17
17
  spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
18
 
19
19
  spec.metadata["homepage_uri"] = spec.homepage
20
- spec.metadata["source_code_uri"] = "https://code.qpard.com/strum/strum-service"
21
- spec.metadata["changelog_uri"] = "https://code.qpard.com/strum/strum-service/CHANGELOG.md"
20
+ spec.metadata["source_code_uri"] = "https://gitlab.com/strum-rb/strum-service"
21
+ spec.metadata["changelog_uri"] = "https://gitlab.com/strum-rb/strum-service/-/blob/master/CHANGELOG.md"
22
+ spec.metadata["bug_tracker_uri"] = "https://gitlab.com/strum-rb/strum-service/issues"
22
23
 
23
24
  # Specify which files should be added to the gem when it is released.
24
25
  # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: strum-service
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Serhiy Nazarov
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-12-03 00:00:00.000000000 Z
11
+ date: 2021-05-11 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Strum Service
14
14
  email:
@@ -18,6 +18,7 @@ extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
20
  - ".gitignore"
21
+ - ".gitlab-ci.yml"
21
22
  - ".rspec"
22
23
  - ".rubocop.yml"
23
24
  - ".travis.yml"
@@ -32,14 +33,15 @@ files:
32
33
  - lib/strum/service.rb
33
34
  - lib/strum/service/version.rb
34
35
  - strum-service.gemspec
35
- homepage: https://code.qpard.com/strum/strum-service
36
+ homepage: https://gitlab.com/strum-rb/strum-service
36
37
  licenses:
37
38
  - MIT
38
39
  metadata:
39
40
  allowed_push_host: https://rubygems.org
40
- homepage_uri: https://code.qpard.com/strum/strum-service
41
- source_code_uri: https://code.qpard.com/strum/strum-service
42
- changelog_uri: https://code.qpard.com/strum/strum-service/CHANGELOG.md
41
+ homepage_uri: https://gitlab.com/strum-rb/strum-service
42
+ source_code_uri: https://gitlab.com/strum-rb/strum-service
43
+ changelog_uri: https://gitlab.com/strum-rb/strum-service/-/blob/master/CHANGELOG.md
44
+ bug_tracker_uri: https://gitlab.com/strum-rb/strum-service/issues
43
45
  post_install_message:
44
46
  rdoc_options: []
45
47
  require_paths:
@@ -55,7 +57,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
57
  - !ruby/object:Gem::Version
56
58
  version: '0'
57
59
  requirements: []
58
- rubygems_version: 3.1.4
60
+ rubygems_version: 3.1.6
59
61
  signing_key:
60
62
  specification_version: 4
61
63
  summary: Light ruby framework