rao-service 0.0.2.pre

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8a453d7bad543b288e31572b87c313a28e19597e
4
+ data.tar.gz: baadcaee6910fb4c866914000ba63209e3ceda75
5
+ SHA512:
6
+ metadata.gz: 2440d8288cd48ea63f42595931cd665234995e6d4a5e39a396bb211964e4560cabc6cf3d8022dee9cdbec55414b130de0a231c31c8f46a1026e3534a346a5870
7
+ data.tar.gz: 52aaf54967964788d15586ffd39e06deb7f22ba3da001f7d2c7bbe3439a3b9ab51af201c95d138748d2e13f060b3b33f21981d517915265dcdabdd87b47c70f3
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2018 Roberto Vasquez Angel
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # Rails Add-Ons Service
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'rao-service'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install rao-service
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,25 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Rails Add-Ons Service'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+ load 'rails/tasks/statistics.rake'
22
+
23
+
24
+
25
+ require 'bundler/gem_tasks'
@@ -0,0 +1,6 @@
1
+ module Rao
2
+ module Service
3
+ class ApplicationMailer < ActionMailer::Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,16 @@
1
+ module Rao
2
+ module Service
3
+ class NotificationMailer < ApplicationMailer
4
+ def result_email
5
+ @result = params[:result]
6
+ @sender = params[:sender]
7
+ @recipient = params[:recipient]
8
+ @environment = params[:environment]
9
+
10
+ subject = "[#{@environment}][#{@result.service.class.name}] #{@result.ok? ? 'SUCCEEDED' : 'FAILED'}"
11
+
12
+ mail(from: @sender, to: @recipient, subject: subject)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,5 @@
1
+ Execution of <%= @result.service.class.name %> <%= @result.ok? ? 'succeeded' : 'failed' %> in <%= @environment %>:
2
+
3
+ <% @result.messages.each do |message| %>
4
+ <%= message %>
5
+ <% end %>
@@ -0,0 +1,15 @@
1
+ module Rao
2
+ module Service
3
+ module Generators
4
+ class InstallGenerator < Rails::Generators::Base
5
+ desc 'Generates the initializer'
6
+
7
+ source_root File.expand_path('../templates', __FILE__)
8
+
9
+ def generate_initializer
10
+ template 'initializer.rb', 'config/initializers/rao-service.rb'
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,19 @@
1
+ Rao::Service.configure do |config|
2
+ # Set the default recipient for service result emails.
3
+ #
4
+ # Default: config.default_notification_sender = 'john.doe@domain.local'
5
+ #
6
+ config.default_notification_sender = 'john.doe@domain.local'
7
+
8
+ # Set the default recipient for service result emails.
9
+ #
10
+ # Default: config.default_notification_recipient = 'john.doe@domain.local'
11
+ #
12
+ config.default_notification_recipient = 'john.doe@domain.local'
13
+
14
+ # Set the environment that will be shown in notifications.
15
+ #
16
+ # Default: config.default_notification_recipient = ->(result) { ::Rails.env }
17
+ #
18
+ config.notification_environment = ->(result) { ::Rails.env }
19
+ end
@@ -0,0 +1,157 @@
1
+ require 'active_support/concern'
2
+ require "rao/service/result/base"
3
+ require "rao/service/messages"
4
+ require 'active_model'
5
+
6
+ module Rao
7
+ module Service
8
+ class Base
9
+ include ActiveModel::Model
10
+ extend ActiveModel::Naming
11
+
12
+ def self.attr_accessor(*args)
13
+ super
14
+ add_attribute_names(*args)
15
+ end
16
+
17
+ def self.add_attribute_names(*args)
18
+ args.each do |attr_name|
19
+ attribute_names << attr_name
20
+ end
21
+ end
22
+
23
+ def self.attribute_names
24
+ (@attr_names ||= [])
25
+ end
26
+
27
+ def self.call(*args)
28
+ new(*args).perform
29
+ end
30
+
31
+ def initialize(attributes = {}, options = {}, &block)
32
+ @options = options
33
+ @block = block
34
+ @attributes = {}
35
+ set_attributes(attributes)
36
+ initialize_result
37
+ initialize_errors
38
+ initialize_messages
39
+ after_initialize
40
+ end
41
+
42
+ private
43
+
44
+ module Attributes
45
+ def set_attributes(attributes)
46
+ attributes.each do |key, value|
47
+ send("#{key}=", value)
48
+ end
49
+ end
50
+ end
51
+
52
+ include Attributes
53
+
54
+ module Callbacks
55
+ def after_initialize; end
56
+ def before_perform; end
57
+ def after_perform; end
58
+ def before_validation; end
59
+ def after_validation; end
60
+ def after_perform; end
61
+
62
+ def perform
63
+ before_validation
64
+ return perform_result unless valid?
65
+ after_validation
66
+ before_perform
67
+ say "Performing" do
68
+ _perform
69
+ end
70
+ after_perform
71
+ perform_result
72
+ end
73
+ end
74
+
75
+ module Resultable
76
+ private
77
+
78
+ def initialize_result
79
+ @result = result_class.new(self)
80
+ end
81
+
82
+ def perform_result
83
+ copy_messages_to_result
84
+ copy_errors_to_result
85
+ @result
86
+ end
87
+
88
+ def result_class
89
+ "#{self.class.name}::Result".constantize
90
+ end
91
+ end
92
+
93
+ module Errors
94
+ private
95
+
96
+ def initialize_errors
97
+ @errors = ActiveModel::Errors.new(self)
98
+ end
99
+
100
+ def copy_errors_to_result
101
+ @result.instance_variable_set(:@errors, @errors)
102
+ end
103
+
104
+ def copy_errors_from_to(obj, key_prefix)
105
+ obj.errors.each do |key, message|
106
+ @errors.add(key_prefix, message)
107
+ end
108
+ end
109
+
110
+ def add_error_and_say(attribute, message)
111
+ @errors.add(attribute, message)
112
+ say(message)
113
+ end
114
+ end
115
+
116
+ module Autosave
117
+ extend ActiveSupport::Concern
118
+
119
+ if respond_to?(:class_methods)
120
+ class_methods do
121
+ def call!(*args)
122
+ new(*args).autosave!.perform
123
+ end
124
+ end
125
+ else
126
+ module ClassMethods
127
+ def call!(*args)
128
+ new(*args).autosave!.perform
129
+ end
130
+ end
131
+ end
132
+
133
+ def autosave?
134
+ !!@options[:autosave]
135
+ end
136
+
137
+ def autosave!
138
+ @options[:autosave] = true
139
+ self
140
+ end
141
+ end
142
+
143
+ module Internationalization
144
+ def t(key, options)
145
+ I18n.t("activemodel.#{self.class.name.underscore}#{key}", options)
146
+ end
147
+ end
148
+
149
+ include Errors
150
+ include Resultable
151
+ include Callbacks
152
+ include Rao::Service::Messages
153
+ include Autosave
154
+ include Internationalization
155
+ end
156
+ end
157
+ end
@@ -0,0 +1,16 @@
1
+ require 'active_support/core_ext/module/delegation'
2
+ require 'active_support/core_ext/module/attribute_accessors'
3
+
4
+ module Rao
5
+ module Service
6
+ module Configuration
7
+ def configure
8
+ yield self
9
+ end
10
+
11
+ mattr_accessor(:default_notification_sender) { 'john.doe@domain.local' }
12
+ mattr_accessor(:default_notification_recipient) { 'john.doe@domain.local' }
13
+ mattr_accessor(:notification_environment) { Object.const_defined?('::Rails') ? ::Rails.env : nil }
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,7 @@
1
+ module Rao
2
+ module Service
3
+ class Engine < ::Rails::Engine
4
+ isolate_namespace Rao::Service
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,18 @@
1
+ module Rao
2
+ module Service::Message
3
+ class Base
4
+ def initialize(content, options = {})
5
+ @indent_level = options.fetch(:indent_level) { 1 }
6
+ @content = content
7
+ end
8
+
9
+ def level
10
+ @indent_level
11
+ end
12
+
13
+ def to_s
14
+ @content
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,52 @@
1
+ require "rao/service/message/base"
2
+
3
+ module Rao
4
+ module Service
5
+ module Messages
6
+ private
7
+
8
+ def initialize_messages
9
+ @messages = []
10
+ end
11
+
12
+ def say(what, &block)
13
+ @indent ||= 0
14
+ if block_given?
15
+ @indent += 1
16
+ output(_message("#{output_prefix}#{(" " * @indent)}#{what}...", level: @indent))
17
+ block_result = yield
18
+ say_done
19
+ @indent -= 1
20
+ block_result
21
+ else
22
+ output(_message("#{output_prefix}#{(" " * @indent)}#{what}", level: @indent))
23
+ end
24
+ end
25
+
26
+ def say_done
27
+ say " => Done"
28
+ end
29
+
30
+ def output_prefix
31
+ "[#{self.class.name}] "
32
+ end
33
+
34
+ def output(what)
35
+ @messages << what
36
+ puts what unless silenced?
37
+ end
38
+
39
+ def silenced?
40
+ !!@options[:silence]
41
+ end
42
+
43
+ def copy_messages_to_result
44
+ @result.instance_variable_set(:@messages, @messages)
45
+ end
46
+
47
+ def _message(content, options = {})
48
+ Rao::Service::Message::Base.new(content, level: options[:level])
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,60 @@
1
+ require 'rao/service'
2
+ require 'active_model/naming'
3
+ require 'active_model/translation'
4
+
5
+ module Rao
6
+ module Service::Result
7
+ class Base
8
+ extend ActiveModel::Translation
9
+
10
+ attr_reader :messages, :errors, :service
11
+
12
+ def initialize(service)
13
+ @service = service
14
+ end
15
+
16
+ def model_name
17
+ @service.model_name
18
+ 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
+ end
59
+ end
60
+ end
@@ -0,0 +1,7 @@
1
+ require 'rao/version'
2
+
3
+ module Rao
4
+ module Service
5
+ VERSION = ::Rao::VERSION
6
+ end
7
+ end
@@ -0,0 +1,18 @@
1
+ require "rao/service/configuration"
2
+ require "rao/service/version"
3
+
4
+ module Rao
5
+ module Service
6
+ extend Configuration
7
+
8
+ class_eval do
9
+ def self.rails_present?
10
+ # Gem.loaded_specs["rails"].present?
11
+ Object.const_defined?('::Rails')
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ require "rao/service/engine" if Object.const_defined?('::Rails')
18
+ require "rao/service/base"
@@ -0,0 +1,2 @@
1
+ require "rao"
2
+ require "rao/service"
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :rao_service do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,174 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rao-service
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2.pre
5
+ platform: ruby
6
+ authors:
7
+ - Roberto Vasquez Angel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-12-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activemodel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rao
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
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'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: guard-bundler
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description:
126
+ email:
127
+ - roberto@vasquez-angel.de
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - MIT-LICENSE
133
+ - README.md
134
+ - Rakefile
135
+ - app/mailers/rao/service/application_mailer.rb
136
+ - app/mailers/rao/service/notification_mailer.rb
137
+ - app/views/rao/service/notification_mailer/result_email.text.erb
138
+ - lib/generators/rao/service/install_generator.rb
139
+ - lib/generators/rao/service/templates/initializer.rb
140
+ - lib/rao-service.rb
141
+ - lib/rao/service.rb
142
+ - lib/rao/service/base.rb
143
+ - lib/rao/service/configuration.rb
144
+ - lib/rao/service/engine.rb
145
+ - lib/rao/service/message/base.rb
146
+ - lib/rao/service/messages.rb
147
+ - lib/rao/service/result/base.rb
148
+ - lib/rao/service/version.rb
149
+ - lib/tasks/rao-service_tasks.rake
150
+ homepage: https://github.com/rao
151
+ licenses:
152
+ - MIT
153
+ metadata: {}
154
+ post_install_message:
155
+ rdoc_options: []
156
+ require_paths:
157
+ - lib
158
+ required_ruby_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ required_rubygems_version: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - ">"
166
+ - !ruby/object:Gem::Version
167
+ version: 1.3.1
168
+ requirements: []
169
+ rubyforge_project:
170
+ rubygems_version: 2.6.14
171
+ signing_key:
172
+ specification_version: 4
173
+ summary: Services for Ruby on Rails.
174
+ test_files: []