rao-service 0.0.33.pre → 0.0.34.pre

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: 0532b18d29858dc9e4079de95bae18a44afd8695f6c163c779a85a01976e91d1
4
- data.tar.gz: d4e2f417436e3250e27289d2f9902a799aef4234057ee78e034090b4dc5531b5
3
+ metadata.gz: 5ea784288707fc71346e3887d04235b138708361055f90393833604dc82d4600
4
+ data.tar.gz: f29f8de334b83589c86c18e26455803ac4c2d1ee451dacfa6d31b1113cabe14f
5
5
  SHA512:
6
- metadata.gz: ac2fac3c0f1e83b7b5ad14baccf9adc880eee4449b5354d3d4164e44e8231fc3f36daf838f04d7d954440f771c92e5ec574643caf6d1baa8bbf7d3b4b9feb9cb
7
- data.tar.gz: 10c6eaf10e2bc561da0f372c7e7977d7e1b948d319ec053168def0534a5591a72afd7013e206b61b60912d157b07094210b1fffd7ac0e58c910c8bb6a1e02ebe
6
+ metadata.gz: a8d5e1c7164bed6e4b5ffa2707b8afac223fd07b66ac734cab98a0f7114dd38dd7c5e4a71c20b6c64c05aebfa37622f3279b224756f8404ad93b6f5b54e4c72e
7
+ data.tar.gz: 2ca451a3b164b95c006d8aa3a51572294d40593057f15744b64ef8e2fceb4972d71263edb2e080df4b9bb3650d24f8718a2afe4365dcc14dea5a678ed3f2d051
data/Rakefile CHANGED
@@ -14,12 +14,8 @@ RDoc::Task.new(:rdoc) do |rdoc|
14
14
  rdoc.rdoc_files.include('lib/**/*.rb')
15
15
  end
16
16
 
17
- APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
18
- load 'rails/tasks/engine.rake'
19
-
20
-
21
17
  load 'rails/tasks/statistics.rake'
22
18
 
19
+ require 'bundler/gem_tasks'
23
20
 
24
-
25
- require 'bundler/gem_tasks'
21
+ require 'rails/dummy/tasks'
@@ -0,0 +1,17 @@
1
+ module Rao
2
+ module Service
3
+ module Base::ActiveJobConcern
4
+ extend ActiveSupport::Concern
5
+
6
+ class_methods do
7
+ def call_later(attributes = {}, options = {})
8
+ Rao::Service::Job.perform_later(self.name, attributes, options)
9
+ end
10
+
11
+ def call_later!(attributes = {}, options = {})
12
+ call_later(attributes, options.merge(autosave: true))
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,30 @@
1
+ module Rao
2
+ module Service
3
+ module Base::AutosaveConcern
4
+ extend ActiveSupport::Concern
5
+
6
+ if respond_to?(:class_methods)
7
+ class_methods do
8
+ def call!(*args)
9
+ new(*args).autosave!.perform
10
+ end
11
+ end
12
+ else
13
+ module ClassMethods
14
+ def call!(*args)
15
+ new(*args).autosave!.perform
16
+ end
17
+ end
18
+ end
19
+
20
+ def autosave?
21
+ !!@options[:autosave]
22
+ end
23
+
24
+ def autosave!
25
+ @options[:autosave] = true
26
+ self
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,31 @@
1
+ module Rao
2
+ module Service
3
+ module Base::CallbacksConcern
4
+ extend ActiveSupport::Concern
5
+
6
+ def after_initialize; end
7
+ def before_perform; end
8
+ def after_perform; end
9
+ def before_validation; end
10
+ def after_validation; end
11
+ def after_perform; end
12
+
13
+ def perform(options = {})
14
+ options.reverse_merge!(validate: true)
15
+ validate = options.delete(:validate)
16
+ if validate
17
+ before_validation
18
+ return perform_result unless valid?
19
+ after_validation
20
+ end
21
+ before_perform
22
+ say "Performing" do
23
+ _perform
24
+ end
25
+ after_perform
26
+ save if autosave? && respond_to?(:save, true)
27
+ perform_result
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,34 @@
1
+ require "rao/service/message/base"
2
+
3
+ module Rao
4
+ module Service
5
+ module Base::ErrorsConcern
6
+ extend ActiveSupport::Concern
7
+
8
+ private
9
+
10
+ def initialize_errors
11
+ @errors = ActiveModel::Errors.new(self)
12
+ end
13
+
14
+ def copy_errors_to_result
15
+ @result.instance_variable_set(:@errors, @errors)
16
+ end
17
+
18
+ def copy_errors_from_to(obj, key_prefix)
19
+ obj.errors.each do |key, message|
20
+ @errors.add(key_prefix, message)
21
+ end
22
+ end
23
+
24
+ def add_error_and_say(attribute, message)
25
+ add_error(attribute, message)
26
+ say(message)
27
+ end
28
+
29
+ def add_error(attribute, message)
30
+ @errors.add(attribute, message)
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,11 @@
1
+ module Rao
2
+ module Service
3
+ module Base::I18nConcern
4
+ extend ActiveSupport::Concern
5
+
6
+ def t(key, options = {})
7
+ I18n.t("activemodel.#{self.class.name.underscore}#{key}", options)
8
+ end
9
+ end
10
+ end
11
+ end
@@ -2,7 +2,9 @@ require "rao/service/message/base"
2
2
 
3
3
  module Rao
4
4
  module Service
5
- module Messages
5
+ module Base::MessagesConcern
6
+ extend ActiveSupport::Concern
7
+
6
8
  private
7
9
 
8
10
  def initialize_messages
@@ -10,25 +12,25 @@ module Rao
10
12
  end
11
13
 
12
14
  def say(what, &block)
13
- @indent ||= 0
15
+ @indent_level ||= 0
14
16
  if block_given?
15
- @indent += 1
16
- output(_message("#{output_prefix}#{(" " * @indent)}#{what}...", level: @indent))
17
+ output(_message(what, indent_level: @indent_level, prefix: prefix, suffix: "..."))
18
+ @indent_level += 1
17
19
  block_result = yield
20
+ @indent_level -= 1
18
21
  say_done
19
- @indent -= 1
20
22
  block_result
21
23
  else
22
- output(_message("#{output_prefix}#{(" " * @indent)}#{what}", level: @indent))
24
+ output(_message(what, indent_level: @indent_level, prefix: prefix))
23
25
  end
24
26
  end
25
27
 
26
28
  def say_done
27
- say " => Done"
29
+ say "=> Done"
28
30
  end
29
31
 
30
- def output_prefix
31
- "[#{self.class.name}] "
32
+ def prefix
33
+ self.class.name
32
34
  end
33
35
 
34
36
  def output(what)
@@ -45,7 +47,7 @@ module Rao
45
47
  end
46
48
 
47
49
  def _message(content, options = {})
48
- Rao::Service::Message::Base.new(content, level: options[:level])
50
+ Rao::Service::Message::Base.new(content, indent_level: options[:indent_level], prefix: options[:prefix], suffix: options[:suffix])
49
51
  end
50
52
  end
51
53
  end
@@ -0,0 +1,25 @@
1
+ require "rao/service/message/base"
2
+
3
+ module Rao
4
+ module Service
5
+ module Base::ResultConcern
6
+ extend ActiveSupport::Concern
7
+
8
+ private
9
+
10
+ def initialize_result
11
+ @result = result_class.new(self)
12
+ end
13
+
14
+ def perform_result
15
+ copy_messages_to_result
16
+ copy_errors_to_result
17
+ @result
18
+ end
19
+
20
+ def result_class
21
+ "#{self.class.name}::Result".constantize
22
+ end
23
+ end
24
+ end
25
+ end
@@ -1,11 +1,34 @@
1
1
  require 'active_support/concern'
2
+ require 'active_support/core_ext/hash/reverse_merge'
2
3
  require "rao/service/result/base"
3
- require "rao/service/messages"
4
4
  require 'active_model'
5
5
 
6
6
  module Rao
7
7
  module Service
8
8
  class Base
9
+ if Rao::Service.active_job_present?
10
+ require 'rao/service/base/active_job_concern'
11
+ include ActiveJobConcern
12
+ end
13
+
14
+ require 'rao/service/base/i18n_concern'
15
+ include I18nConcern
16
+
17
+ require 'rao/service/base/autosave_concern'
18
+ include AutosaveConcern
19
+
20
+ require 'rao/service/base/callbacks_concern'
21
+ include CallbacksConcern
22
+
23
+ require 'rao/service/base/messages_concern'
24
+ include MessagesConcern
25
+
26
+ require 'rao/service/base/errors_concern'
27
+ include ErrorsConcern
28
+
29
+ require 'rao/service/base/result_concern'
30
+ include ResultConcern
31
+
9
32
  include ActiveModel::Model
10
33
  extend ActiveModel::Naming
11
34
 
@@ -56,117 +79,6 @@ module Rao
56
79
  end
57
80
 
58
81
  include Attributes
59
-
60
- module Callbacks
61
- def after_initialize; end
62
- def before_perform; end
63
- def after_perform; end
64
- def before_validation; end
65
- def after_validation; end
66
- def after_perform; end
67
-
68
- def perform(options = {})
69
- options.reverse_merge!(validate: true)
70
- validate = options.delete(:validate)
71
- if validate
72
- before_validation
73
- return perform_result unless valid?
74
- after_validation
75
- end
76
- before_perform
77
- say "Performing" do
78
- _perform
79
- end
80
- after_perform
81
- save if autosave? && respond_to?(:save, true)
82
- perform_result
83
- end
84
- end
85
-
86
- module Resultable
87
- private
88
-
89
- def initialize_result
90
- @result = result_class.new(self)
91
- end
92
-
93
- def perform_result
94
- copy_messages_to_result
95
- copy_errors_to_result
96
- @result
97
- end
98
-
99
- def result_class
100
- "#{self.class.name}::Result".constantize
101
- end
102
- end
103
-
104
- module Errors
105
- private
106
-
107
- def initialize_errors
108
- @errors = ActiveModel::Errors.new(self)
109
- end
110
-
111
- def copy_errors_to_result
112
- @result.instance_variable_set(:@errors, @errors)
113
- end
114
-
115
- def copy_errors_from_to(obj, key_prefix)
116
- obj.errors.each do |key, message|
117
- @errors.add(key_prefix, message)
118
- end
119
- end
120
-
121
- def add_error_and_say(attribute, message)
122
- add_error(attribute, message)
123
- say(message)
124
- end
125
-
126
- def add_error(attribute, message)
127
- @errors.add(attribute, message)
128
- end
129
- end
130
-
131
- module Autosave
132
- extend ActiveSupport::Concern
133
-
134
- if respond_to?(:class_methods)
135
- class_methods do
136
- def call!(*args)
137
- new(*args).autosave!.perform
138
- end
139
- end
140
- else
141
- module ClassMethods
142
- def call!(*args)
143
- new(*args).autosave!.perform
144
- end
145
- end
146
- end
147
-
148
- def autosave?
149
- !!@options[:autosave]
150
- end
151
-
152
- def autosave!
153
- @options[:autosave] = true
154
- self
155
- end
156
- end
157
-
158
- module Internationalization
159
- def t(key, options = {})
160
- I18n.t("activemodel.#{self.class.name.underscore}#{key}", options)
161
- end
162
- end
163
-
164
- include Errors
165
- include Resultable
166
- include Callbacks
167
- include Rao::Service::Messages
168
- include Autosave
169
- include Internationalization
170
82
  end
171
83
  end
172
84
  end
@@ -2,7 +2,9 @@ module Rao
2
2
  module Service::Message
3
3
  class Base
4
4
  def initialize(content, options = {})
5
- @indent_level = options.fetch(:indent_level) { 1 }
5
+ @indent_level = options.fetch(:indent_level) { 0 }
6
+ @prefix = options.fetch(:prefix) { nil }
7
+ @suffix = options.fetch(:suffix) { nil }
6
8
  @content = content
7
9
  end
8
10
 
@@ -11,7 +13,11 @@ module Rao
11
13
  end
12
14
 
13
15
  def to_s
14
- @content
16
+ "[#{@prefix}] #{(" " * @indent_level)}#{@content}#{@suffix}"
17
+ end
18
+
19
+ def content
20
+ @content.to_s
15
21
  end
16
22
  end
17
23
  end
@@ -0,0 +1,27 @@
1
+ module Rao
2
+ module Service
3
+ module Result
4
+ module Base::MailableConcern
5
+ extend ActiveSupport::Concern
6
+
7
+ def notify_now(recipient = nil)
8
+ recipient ||= default_notification_recipient
9
+ sender ||= default_notification_sender
10
+ ::Rao::Service::NotificationMailer.with(result: self, environment: notification_environment, sender: sender, recipient: recipient).result_email.deliver_now
11
+ end
12
+
13
+ def default_notification_sender
14
+ ::Rao::Service::Configuration.default_notification_sender
15
+ end
16
+
17
+ def default_notification_recipient
18
+ ::Rao::Service::Configuration.default_notification_recipient
19
+ end
20
+
21
+ def notification_environment
22
+ ::Rao::Service::Configuration.notification_environment.call(self)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,19 @@
1
+ module Rao
2
+ module Service
3
+ module Result
4
+ module Base::SucceedableConcern
5
+ extend ActiveSupport::Concern
6
+
7
+ def success?
8
+ !failed?
9
+ end
10
+
11
+ alias_method :ok?, :success?
12
+
13
+ def failed?
14
+ @errors.any?
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -6,7 +6,15 @@ module Rao
6
6
  module Service::Result
7
7
  class Base
8
8
  extend ActiveModel::Translation
9
-
9
+
10
+ if Rao::Service.rails_present?
11
+ require 'rao/service/result/base/mailable_concern'
12
+ include MailableConcern
13
+ end
14
+
15
+ require 'rao/service/result/base/succeedable_concern'
16
+ include SucceedableConcern
17
+
10
18
  attr_reader :messages, :errors, :service
11
19
 
12
20
  def initialize(service)
@@ -16,45 +24,6 @@ module Rao
16
24
  def model_name
17
25
  @service.model_name
18
26
  end
19
-
20
- module Succeedable
21
- def success?
22
- !failed?
23
- end
24
-
25
- def failed?
26
- @errors.any?
27
- end
28
-
29
- def ok?
30
- success?
31
- end
32
- end
33
-
34
- include Succeedable
35
-
36
- module Mailable
37
- def notify_now(recipient = nil)
38
- recipient ||= default_notification_recipient
39
- sender ||= default_notification_sender
40
- ::Rao::Service::NotificationMailer.with(result: self, environment: notification_environment, sender: sender, recipient: recipient).result_email.deliver_now
41
- end
42
-
43
- def default_notification_sender
44
- ::Rao::Service::Configuration.default_notification_sender
45
- end
46
-
47
- def default_notification_recipient
48
- ::Rao::Service::Configuration.default_notification_recipient
49
- end
50
-
51
- def notification_environment
52
- ::Rao::Service::Configuration.notification_environment.call(self)
53
- end
54
-
55
- end
56
-
57
- include Mailable if Rao::Service.rails_present?
58
27
  end
59
28
  end
60
29
  end
data/lib/rao/service.rb CHANGED
@@ -7,9 +7,11 @@ module Rao
7
7
 
8
8
  class_eval do
9
9
  def self.rails_present?
10
- # Gem.loaded_specs["rails"].present?
11
10
  Object.const_defined?('::Rails')
12
11
  end
12
+ def self.active_job_present?
13
+ Object.const_defined?('::ActiveJob')
14
+ end
13
15
  end
14
16
  end
15
17
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rao-service
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.33.pre
4
+ version: 0.0.34.pre
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roberto Vasquez Angel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-04 00:00:00.000000000 Z
11
+ date: 2019-11-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rails-dummy
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: sqlite3
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -136,6 +150,34 @@ dependencies:
136
150
  - - ">="
137
151
  - !ruby/object:Gem::Version
138
152
  version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rb-readline
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: bootsnap
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
139
181
  description:
140
182
  email:
141
183
  - roberto@vasquez-angel.de
@@ -155,11 +197,19 @@ files:
155
197
  - lib/rao-service.rb
156
198
  - lib/rao/service.rb
157
199
  - lib/rao/service/base.rb
200
+ - lib/rao/service/base/active_job_concern.rb
201
+ - lib/rao/service/base/autosave_concern.rb
202
+ - lib/rao/service/base/callbacks_concern.rb
203
+ - lib/rao/service/base/errors_concern.rb
204
+ - lib/rao/service/base/i18n_concern.rb
205
+ - lib/rao/service/base/messages_concern.rb
206
+ - lib/rao/service/base/result_concern.rb
158
207
  - lib/rao/service/configuration.rb
159
208
  - lib/rao/service/engine.rb
160
209
  - lib/rao/service/message/base.rb
161
- - lib/rao/service/messages.rb
162
210
  - lib/rao/service/result/base.rb
211
+ - lib/rao/service/result/base/mailable_concern.rb
212
+ - lib/rao/service/result/base/succeedable_concern.rb
163
213
  - lib/rao/service/version.rb
164
214
  - lib/tasks/rao-service_tasks.rake
165
215
  homepage: https://github.com/rao