activeduty 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 3851ef284433a3cf609e6934fee1eb357c0f1a8d97bd2db3677ef16732adc2a1
4
+ data.tar.gz: 3a24b9d50c907e610b17062ff1468fc3bde330150da2492132c255986c99a64b
5
+ SHA512:
6
+ metadata.gz: 62d7527dced5eca2869d9ad7ce69b7ecfdd1b4ecf0c3a2249f72e18790fbfca384f3ab49798ca36cdc9021650a2c7864df2be2beed4d64d63f5b18884146a8c3
7
+ data.tar.gz: 126a2cc3b05c4dbd1cc7b9688c5b96f0d863948c8de9a97378acbd429b5030d9cda087873ffe25edc057b013cdbc7c54195cdff81a0bdc638ff2de43aaa7cf22
@@ -0,0 +1,3 @@
1
+ # ActiveDuty
2
+
3
+ Service objects.
@@ -0,0 +1,19 @@
1
+ require "active_duty/version"
2
+
3
+ require "active_support"
4
+ require "active_model"
5
+
6
+ module ActiveDuty
7
+ Failure = Class.new(StandardError)
8
+
9
+ extend ActiveSupport::Autoload
10
+ autoload :Base
11
+ autoload :Job
12
+
13
+ mattr_accessor :job_queue
14
+ self.job_queue = :active_duty
15
+
16
+ mattr_accessor :job_parent_class
17
+ self.job_parent_class = 'ActiveJob::Base'
18
+
19
+ end
@@ -0,0 +1,30 @@
1
+ require 'active_duty/initializers'
2
+ require 'active_duty/run'
3
+ require 'active_duty/fail'
4
+ require 'active_duty/success'
5
+ require 'active_duty/callbacks'
6
+ require 'active_duty/context'
7
+ require 'active_duty/errors'
8
+ require 'active_duty/rollback'
9
+
10
+ module ActiveDuty
11
+ class Base
12
+ include Callbacks
13
+ include Context
14
+ include Errors
15
+ include Fail
16
+ include Initializers
17
+ include Rollback
18
+ include Run
19
+ include Success
20
+
21
+ class << self
22
+ alias_method :__new, :new
23
+ def new(*args)
24
+ e = __new(*args)
25
+ e.after_initialize!
26
+ e
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,15 @@
1
+ module ActiveDuty
2
+ module CallAsync
3
+ extend ActiveSupport::Concern
4
+
5
+ def call_async(*args)
6
+ self.class.call_async(*args)
7
+ end
8
+
9
+ module ClassMethods
10
+ def call_async(*args)
11
+ ActiveDuty.async_job.perform_async(self.name, *args)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,27 @@
1
+ module ActiveDuty
2
+ module Callbacks
3
+ extend ActiveSupport::Concern
4
+ included do
5
+ extend ActiveModel::Callbacks
6
+ define_model_callbacks :call, only: [:before, :after, :around]
7
+
8
+ def self.after_initialize(&block)
9
+ @_after_initialize_block = block
10
+ end
11
+
12
+ def self._after_initialize_block
13
+ if defined?(@_after_initialize_block)
14
+ @_after_initialize_block
15
+ else
16
+ nil
17
+ end
18
+ end
19
+
20
+ def after_initialize!
21
+ if self.class._after_initialize_block
22
+ instance_exec(self, &self.class._after_initialize_block)
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,9 @@
1
+ require 'ostruct'
2
+
3
+ module ActiveDuty
4
+ module Context
5
+ def context
6
+ @context ||= OpenStruct.new
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,15 @@
1
+ require 'ostruct'
2
+
3
+ module ActiveDuty
4
+ module Errors
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ extend ActiveModel::Naming
9
+ end
10
+
11
+ def errors
12
+ @errors ||= ActiveModel::Errors.new(self)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,14 @@
1
+ module ActiveDuty
2
+ module Fail
3
+ extend ActiveSupport::Concern
4
+
5
+ def fail!
6
+ @failure = true
7
+ raise Failure, self
8
+ end
9
+
10
+ def failure?
11
+ @failure == true || errors.any?
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,34 @@
1
+ module ActiveDuty
2
+ module Initializers
3
+ extend ActiveSupport::Concern
4
+ module ClassMethods
5
+ def init_with(*args)
6
+ kwargs = args.extract_options!
7
+
8
+ if kwargs.empty?
9
+ define_method :initialize do |*initialize_args|
10
+ args.zip(initialize_args) do |name, value|
11
+ instance_variable_set("@#{name}", value)
12
+ end
13
+ self.class.attr_reader *args
14
+ end
15
+ elsif kwargs.is_a?(Hash)
16
+ class_eval <<-METHOD, __FILE__, __LINE__ + 1
17
+ def initialize(#{kwargs.keys.map { |key| "#{key}:" }.join(', ')})
18
+ #{kwargs.map { |key, value|
19
+ "
20
+ instance_variable_set(:\"@#{value}\", #{key})
21
+ self.class.attr_reader :#{value}
22
+ "
23
+ }.join("\n")
24
+ }
25
+ end
26
+ METHOD
27
+ end
28
+
29
+ end
30
+
31
+ end
32
+
33
+ end
34
+ end
@@ -0,0 +1,11 @@
1
+ module ActiveDuty
2
+ if Object.const_defined?(ActiveDuty.job_parent_class)
3
+ class Job < Object.const_get(ActiveDuty.job_parent_class)
4
+ def perform(klass, *args)
5
+ klass.call!(args)
6
+ end
7
+ end
8
+ else
9
+ puts "#{ActiveDuty.job_parent_class} not defined. No job."
10
+ end
11
+ end
@@ -0,0 +1,24 @@
1
+ module ActiveDuty
2
+ module Rollback
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def rollback(&block)
7
+ @_rollback = block
8
+ end
9
+
10
+ def _rollback
11
+ return unless defined?(@_rollback)
12
+ @_rollback
13
+ end
14
+ end
15
+
16
+ def rollback!
17
+ if self.class._rollback
18
+ instance_eval(&self.class._rollback)
19
+ elsif self.respond_to?(:rollback)
20
+ rollback
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,39 @@
1
+ module ActiveDuty
2
+ module Run
3
+ extend ActiveSupport::Concern
4
+ def call(*args)
5
+ begin #
6
+ call!(*args)
7
+ rescue => error
8
+ rollback!
9
+ if error.is_a?(Failure)
10
+ self.errors.add(:base, error.message)
11
+ return self
12
+ end
13
+ raise error
14
+ end
15
+ self
16
+ end
17
+
18
+ def call!(*args)
19
+ run(*args)
20
+ self
21
+ end
22
+
23
+ module ClassMethods
24
+ def run(*args, &block)
25
+ define_method :run do |*args|
26
+ self.instance_exec *args, &block
27
+ end
28
+ end
29
+
30
+ def call(*args)
31
+ new(*args).call
32
+ end
33
+
34
+ def call!(*args)
35
+ new(*args).call!
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,7 @@
1
+ module ActiveDuty
2
+ module Success
3
+ def success?
4
+ !failure?
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveDuty
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activeduty
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Josh Brody
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-07-15 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: pry
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
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: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '12.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '12.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: minitest
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '5.0'
83
+ description: Service objects.
84
+ email: josh@josh.mn
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - README.md
90
+ - lib/active_duty.rb
91
+ - lib/active_duty/base.rb
92
+ - lib/active_duty/call_async.rb
93
+ - lib/active_duty/callbacks.rb
94
+ - lib/active_duty/context.rb
95
+ - lib/active_duty/errors.rb
96
+ - lib/active_duty/fail.rb
97
+ - lib/active_duty/initializers.rb
98
+ - lib/active_duty/job.rb
99
+ - lib/active_duty/rollback.rb
100
+ - lib/active_duty/run.rb
101
+ - lib/active_duty/success.rb
102
+ - lib/active_duty/version.rb
103
+ homepage: https://github.com/joshmn/activeduty
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: 2.5.0
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubygems_version: 3.1.4
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Service objects.
126
+ test_files: []