light-services 2.0.0.rc4 → 2.0.0.rc6

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: 0cb56d628d4f4fa27faee797fb1292542a4804441b51bbad04063629ad85bf1a
4
- data.tar.gz: 30ceeeed2d98b8e78d7d1ebe8d3aaee614d6f320a7038931eae2bcceb5518a69
3
+ metadata.gz: 06c568f730742de7555fcdccd76689a7ae983dbc97880935b2587439f1707d29
4
+ data.tar.gz: 48ad82deadfae1e28300aea79fdb6b220f3c2a29e4e082419cb43743aebfda15
5
5
  SHA512:
6
- metadata.gz: '0987294172814178aeec11f380433a361e1e469806958e1c6e12dc7465b9a166af58506531ee99c8d39ec78a45a57968fbd67973b10c45ee792ac7796ca0a4bc'
7
- data.tar.gz: 63b6d21e02e68e93487f177a0096b341f55398b05a2876100e2c729eee10c33b75284e1f5a433038e9e431c60bf4296c5c477544b19938994b575e0dff4aa8b4
6
+ metadata.gz: e55a6a1b112d794368d08c4c487734613e24d8fbf1ec5d9b097f0cba22148f37325053db5568a9715ab0d1c8ba3abb22b5762657e5b838048fe6e4c11a6cb6ec
7
+ data.tar.gz: 84ffbca0ddfade25831c0b2c7597455200a98149e6a2e102923ec9298faa4d0486a97c5b06d2d2e0a974cb47a43feb84bc4748ee3bda3aec5731b5ff132bd089
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- light-services (2.0.0.rc3)
4
+ light-services (2.0.0.rc5)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "light/services/message"
3
4
  require "light/services/messages"
4
5
  require "light/services/base_with_context"
5
6
 
@@ -92,9 +93,12 @@ module Light
92
93
  end
93
94
 
94
95
  class << self
95
- # TODO: Create `run!`
96
- def run(args = {})
97
- new(args).tap(&:call)
96
+ def run(args = {}, config = {})
97
+ new(args, config).tap(&:call)
98
+ end
99
+
100
+ def run!(args = {})
101
+ run(args, raise_on_error: true)
98
102
  end
99
103
 
100
104
  def with(service_or_config = {}, config = {})
@@ -14,11 +14,15 @@ module Light
14
14
  raise Light::Services::ArgTypeError, "#{parent_service.class} - must be a subclass of Light::Services::Base"
15
15
  end
16
16
 
17
- # TODO: Create `run!`
18
17
  def run(args = {})
19
18
  @service_class.new(extend_arguments(args), @config, @parent_service).tap(&:call)
20
19
  end
21
20
 
21
+ def run!(args = {})
22
+ @config[:raise_on_error] = true
23
+ run(args)
24
+ end
25
+
22
26
  private
23
27
 
24
28
  def extend_arguments(args)
@@ -63,14 +63,12 @@ module Light
63
63
 
64
64
  def validate_name!(klass, name)
65
65
  if !@allow_redefine && all(klass).key?(name)
66
- # TODO: Update error class
67
66
  raise Light::Services::Error, "#{@item_class} with name `#{name}` already exists in service #{klass}"
68
67
  end
69
68
  end
70
69
 
71
70
  def validate_opts!(klass, name, opts)
72
71
  if opts[:before] && opts[:after]
73
- # TODO: Update error class
74
72
  raise Light::Services::Error, "You cannot specify `before` and `after` " \
75
73
  "for #{@item_class} `#{name}` in service #{klass} at the same time"
76
74
  end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This class stores errors and warnings
4
+ module Light
5
+ module Services
6
+ class Message
7
+ # Getters
8
+ attr_reader :key, :text
9
+
10
+ def initialize(key, text, opts = {})
11
+ @key = key
12
+ @text = text
13
+ @opts = opts
14
+ end
15
+
16
+ def break?
17
+ @opts[:break]
18
+ end
19
+
20
+ def rollback?
21
+ @opts[:rollback]
22
+ end
23
+
24
+ def to_s
25
+ text
26
+ end
27
+ end
28
+ end
29
+ end
@@ -10,13 +10,21 @@ module Light
10
10
  @messages = {}
11
11
  end
12
12
 
13
- def add(key, message, opts = {}, last: nil)
14
- @messages[key] ||= []
15
- @messages[key] += [*message]
13
+ def add(key, text, opts = {})
14
+ raise Light::Services::Error, "Error text can't be blank" if !text || text.blank?
16
15
 
17
- raise!(key, message)
18
- break!(opts[:break])
19
- rollback!(opts[:rollback]) if last.nil? || last
16
+ message = nil
17
+
18
+ [*text].each do |text|
19
+ message = text.is_a?(Message) ? text : Message.new(key, text, opts)
20
+
21
+ @messages[key] ||= []
22
+ @messages[key] << message
23
+ end
24
+
25
+ raise!(message)
26
+ break!(opts.key?(:break) ? opts[:break] : message.break?)
27
+ rollback!(opts.key?(:rollback) ? opts[:rollback] : message.rollback?) if !opts.key?(:last) || opts[:last]
20
28
  end
21
29
 
22
30
  def break?
@@ -26,47 +34,40 @@ module Light
26
34
  def copy_from(entity, opts = {})
27
35
  if defined?(ActiveRecord::Base) && entity.is_a?(ActiveRecord::Base)
28
36
  copy_from(entity.errors.messages, opts)
37
+ elsif entity.is_a?(Light::Services::Base)
38
+ copy_from(entity.errors, opts)
29
39
  elsif entity.respond_to?(:each)
30
40
  last_index = entity.size - 1
31
41
 
32
42
  entity.each_with_index do |(key, message), index|
33
- add(key, message, opts, last: index == last_index)
43
+ add(key, message, opts.merge(last: index == last_index))
34
44
  end
35
45
  else
36
- # TODO: Update error
37
- raise Light::Services::Error
46
+ raise Light::Services::Error, "Don't know how to import errors from #{entity}"
38
47
  end
39
48
  end
40
49
 
41
50
  def copy_to(entity)
42
- if defined?(ActiveRecord::Base) && entity.is_a?(ActiveRecord::Base)
43
- each do |key, message|
44
- entity.errors.add(key, message)
51
+ if defined?(ActiveRecord::Base) && entity.is_a?(ActiveRecord::Base) || entity.is_a?(Light::Services::Base)
52
+ each do |key, messages|
53
+ messages.each do |message|
54
+ entity.errors.add(key, message.to_s)
55
+ end
45
56
  end
46
57
  elsif entity.is_a?(Hash)
47
- each do |key, message|
58
+ each do |key, messages|
48
59
  entity[key] ||= []
49
- entity[key] << message
60
+ entity[key] += messages.map(&:to_s)
50
61
  end
51
62
  else
52
- # TODO: Update error
53
- raise Light::Services::Error
63
+ raise Light::Services::Error, "Don't know how to export errors to #{entity}"
54
64
  end
55
65
 
56
66
  entity
57
67
  end
58
68
 
59
- def errors_to_record(record)
60
- if !defined?(ActiveRecord::Base) || !record.is_a?(ActiveRecord::Base)
61
- # TODO: Update error
62
- raise Light::Services::Error
63
- end
64
-
65
- errors.each do |key, message|
66
- record.errors.add(key, message)
67
- end
68
-
69
- record
69
+ def to_h
70
+ @messages.to_h.map { |key, value| [key, value.map(&:to_s)] }.to_h
70
71
  end
71
72
 
72
73
  def method_missing(method, *args, &block)
@@ -89,10 +90,10 @@ module Light
89
90
  @break = true
90
91
  end
91
92
 
92
- def raise!(key, message)
93
+ def raise!(message)
93
94
  return unless @config[:raise_on_add]
94
95
 
95
- raise Light::Services::Error, "#{key.to_s.capitalize} #{message}"
96
+ raise Light::Services::Error, "#{message.key.to_s.capitalize} #{message}"
96
97
  end
97
98
 
98
99
  def rollback!(rollback)
@@ -6,7 +6,7 @@ module Light
6
6
  module Settings
7
7
  class Argument
8
8
  # Getters
9
- attr_reader :name, :default_exists, :default, :context, :optional
9
+ attr_reader :name, :default_exists, :default, :context, :optional, :arg_types_cache
10
10
 
11
11
  def initialize(name, service_class, opts = {})
12
12
  @name = name
@@ -18,6 +18,8 @@ module Light
18
18
  @default = opts.delete(:default)
19
19
  @optional = opts.delete(:optional)
20
20
 
21
+ @arg_types_cache = {}
22
+
21
23
  define_methods
22
24
  end
23
25
 
@@ -25,6 +27,8 @@ module Light
25
27
  return if !@type || [*@type].any? do |type|
26
28
  if type == :boolean
27
29
  value.is_a?(TrueClass) || value.is_a?(FalseClass)
30
+ elsif type.is_a?(Symbol)
31
+ arg_type(value) == type
28
32
  else
29
33
  value.is_a?(type)
30
34
  end
@@ -36,6 +40,19 @@ module Light
36
40
 
37
41
  private
38
42
 
43
+ def arg_type(value)
44
+ klass = value.class
45
+
46
+ @arg_types_cache[klass] ||= klass
47
+ .name
48
+ .gsub(/::/, '/')
49
+ .gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
50
+ .gsub(/([a-z\d])([A-Z])/,'\1_\2')
51
+ .tr("-", "_")
52
+ .downcase
53
+ .to_sym
54
+ end
55
+
39
56
  def define_methods
40
57
  name = @name
41
58
 
@@ -20,10 +20,6 @@ module Light
20
20
  raise Light::Services::TwoConditions, "#{service_class} `if` and `unless` cannot be specified " \
21
21
  "for the step `#{name}` at the same time"
22
22
  end
23
-
24
- if @always && (@if || @unless)
25
- raise Light::Services::Error, "#{service_class} `always` cannot be combined with `if` and `unless`"
26
- end
27
23
  end
28
24
 
29
25
  def run(instance, benchmark: false)
@@ -65,7 +61,7 @@ module Light
65
61
  when Symbol
66
62
  instance.send(condition)
67
63
  when Proc
68
- condition.call
64
+ instance.instance_exec(&condition)
69
65
  else
70
66
  raise Light::Services::Error, "#{@service_class} condition should be a Symbol or Proc " \
71
67
  "for the step `#{@name}` (currently: #{condition.class})"
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Light
4
4
  module Services
5
- VERSION = "2.0.0.rc4"
5
+ VERSION = "2.0.0.rc6"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: light-services
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.rc4
4
+ version: 2.0.0.rc6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Emelianenko
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-08-05 00:00:00.000000000 Z
11
+ date: 2021-12-20 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Powerful implementation of Service Object pattern for Ruby and Rails
14
14
  email:
@@ -41,6 +41,7 @@ files:
41
41
  - lib/light/services/collection/outputs.rb
42
42
  - lib/light/services/config.rb
43
43
  - lib/light/services/exceptions.rb
44
+ - lib/light/services/message.rb
44
45
  - lib/light/services/messages.rb
45
46
  - lib/light/services/settings/argument.rb
46
47
  - lib/light/services/settings/output.rb