arq 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/arq/action.rb +55 -0
- data/lib/arq/context.rb +33 -0
- data/lib/arq/errors.rb +28 -0
- data/lib/arq/runnable.rb +90 -0
- data/lib/arq/version.rb +5 -0
- data/lib/arq.rb +7 -0
- metadata +54 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: a4e1c4a65e159a4c635f1a1bee0bc1b0797dbe99a01d16b9a515bc6f1a2060bd
|
4
|
+
data.tar.gz: 4dca5b5179826aa0161b8ae619407533ded39112db29bec87c8c7c744ca41e99
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 253883d41ea384035d6e9ebadc41e9c9a94f752b16eb28d0aad0f368406a7ca413ae14bb6f5c4f973f6806ca58531c70de82e20c9208a5ae951e2c7142efcef8
|
7
|
+
data.tar.gz: 741d201875b5fdeded240b28961a8b4367c224048f0e3cfcf1d90085d0269d847ccaef0e4aecde774cc4c648054a82f8b28be1b088c440b14869ce8f14e9d8c1
|
data/lib/arq/action.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Arq
|
4
|
+
# Module to extend to create an action. Exposes confing functions #call, #params, and #returns.
|
5
|
+
module Action
|
6
|
+
def call(ctx)
|
7
|
+
ctx = transform_input_context(ctx)
|
8
|
+
|
9
|
+
generate_runnable(ctx).call
|
10
|
+
|
11
|
+
ctx
|
12
|
+
end
|
13
|
+
|
14
|
+
def params(*keys)
|
15
|
+
params_list.concat(keys).compact
|
16
|
+
end
|
17
|
+
|
18
|
+
def returns(*keys)
|
19
|
+
returns_list.concat(keys).compact
|
20
|
+
end
|
21
|
+
|
22
|
+
def run(&block)
|
23
|
+
@run_block = block
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def transform_input_context(ctx)
|
29
|
+
case ctx
|
30
|
+
when Arq::Context
|
31
|
+
ctx
|
32
|
+
when Hash
|
33
|
+
Arq::Context.new(ctx)
|
34
|
+
else
|
35
|
+
raise Arq::InvalidContextParameterError
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
def generate_runnable(ctx)
|
40
|
+
Arq::Runnable.new(ctx, params_list, returns_list, &run_block)
|
41
|
+
end
|
42
|
+
|
43
|
+
def params_list
|
44
|
+
@params_list ||= []
|
45
|
+
end
|
46
|
+
|
47
|
+
def returns_list
|
48
|
+
@returns_list ||= []
|
49
|
+
end
|
50
|
+
|
51
|
+
def run_block
|
52
|
+
@run_block ||= nil
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
data/lib/arq/context.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Arq
|
4
|
+
# Light wrapper around Hash with additional properties for action results.
|
5
|
+
class Context < Hash
|
6
|
+
def initialize(params = {})
|
7
|
+
super(nil)
|
8
|
+
merge!(params)
|
9
|
+
end
|
10
|
+
|
11
|
+
def fail!(message = nil)
|
12
|
+
@failure = true
|
13
|
+
@message = message
|
14
|
+
end
|
15
|
+
|
16
|
+
def fail_now!(message = nil)
|
17
|
+
fail!(message)
|
18
|
+
raise Arq::FailureError
|
19
|
+
end
|
20
|
+
|
21
|
+
def failure?
|
22
|
+
@failure ||= false
|
23
|
+
end
|
24
|
+
|
25
|
+
def success?
|
26
|
+
!failure?
|
27
|
+
end
|
28
|
+
|
29
|
+
def message
|
30
|
+
@message ||= ""
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
data/lib/arq/errors.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Arq
|
4
|
+
# Raised when the context for an action is not a Arq::Context or a Hash.
|
5
|
+
class InvalidContextParameterError < StandardError
|
6
|
+
def initialize
|
7
|
+
super("Must be a Arq::Context or Hash")
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
# Raised when action parameters do not exist within the passed context.
|
12
|
+
class ParametersNotInContextError < StandardError
|
13
|
+
def initialize(params)
|
14
|
+
super(params.join(", "))
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
# Raised when action return keys do not exist within the passed context.
|
19
|
+
class ReturnValuesNotInContextError < StandardError
|
20
|
+
def initialize(returns)
|
21
|
+
super(returns.join(", "))
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Raised internally in [Arq::Runnable#hard_fail!] to escape the current action.
|
26
|
+
class FailureError < StandardError
|
27
|
+
end
|
28
|
+
end
|
data/lib/arq/runnable.rb
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Arq
|
4
|
+
# Represents a process to run with a given context, parameters, and returns.
|
5
|
+
# A Runnable is generated from an Action.
|
6
|
+
class Runnable
|
7
|
+
def initialize(ctx, params = [], returns = [], &block)
|
8
|
+
@ctx = ctx
|
9
|
+
@params = params
|
10
|
+
@returns = returns
|
11
|
+
@block = block
|
12
|
+
end
|
13
|
+
|
14
|
+
def call
|
15
|
+
return if @ctx.failure?
|
16
|
+
|
17
|
+
validate_required_params
|
18
|
+
|
19
|
+
import_context
|
20
|
+
val = run_block
|
21
|
+
export_context
|
22
|
+
|
23
|
+
# If the block returned an array, attempt to run
|
24
|
+
run_sequence(val) if val.is_a?(Array)
|
25
|
+
|
26
|
+
# Only validate returns if context is successful
|
27
|
+
validate_required_returns if @ctx.success?
|
28
|
+
|
29
|
+
val
|
30
|
+
end
|
31
|
+
|
32
|
+
def run(&block)
|
33
|
+
Arq::Runnable.new(@ctx, [], [], &block)
|
34
|
+
end
|
35
|
+
|
36
|
+
def fail!(message)
|
37
|
+
@ctx.fail!(message)
|
38
|
+
end
|
39
|
+
|
40
|
+
def fail_now!(message)
|
41
|
+
@ctx.fail_now!(message)
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def run_block
|
47
|
+
instance_eval(&@block)
|
48
|
+
rescue Arq::FailureError
|
49
|
+
nil
|
50
|
+
end
|
51
|
+
|
52
|
+
def run_sequence(sequence)
|
53
|
+
sequence.each do |e|
|
54
|
+
case e
|
55
|
+
when Arq::Action
|
56
|
+
e.call(@ctx)
|
57
|
+
when Arq::Runnable
|
58
|
+
e.call
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def import_context
|
64
|
+
@ctx.each do |key, val|
|
65
|
+
instance_variable_set(:"@#{key}", val)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def export_context
|
70
|
+
keys = [*@ctx.keys, *@returns]
|
71
|
+
|
72
|
+
keys.each do |key|
|
73
|
+
instance_key = :"@#{key}"
|
74
|
+
# Must check for existence since getting non-existent
|
75
|
+
# instance variables will return nil.
|
76
|
+
@ctx[key] = instance_variable_get(instance_key) if instance_variables.include?(instance_key)
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def validate_required_params
|
81
|
+
missing_params = (@params - @ctx.keys)
|
82
|
+
raise Arq::ParametersNotInContextError, missing_params unless missing_params.empty?
|
83
|
+
end
|
84
|
+
|
85
|
+
def validate_required_returns
|
86
|
+
missing_returns = (@returns - @ctx.keys)
|
87
|
+
raise Arq::ReturnValuesNotInContextError, missing_returns unless missing_returns.empty?
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
data/lib/arq/version.rb
ADDED
data/lib/arq.rb
ADDED
metadata
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: arq
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kavin Phan
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-09-05 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A service skeleton framework heavily inspired by LightService with the
|
14
|
+
primary goal of being less verbose.
|
15
|
+
email:
|
16
|
+
- kavin@kphan.tech
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/arq.rb
|
22
|
+
- lib/arq/action.rb
|
23
|
+
- lib/arq/context.rb
|
24
|
+
- lib/arq/errors.rb
|
25
|
+
- lib/arq/runnable.rb
|
26
|
+
- lib/arq/version.rb
|
27
|
+
homepage: https://github.com/kphan32/arq
|
28
|
+
licenses:
|
29
|
+
- MIT
|
30
|
+
metadata:
|
31
|
+
homepage_uri: https://github.com/kphan32/arq
|
32
|
+
source_code_uri: https://github.com/kphan32/arq
|
33
|
+
changelog_uri: https://github.com/kphan32/arq/blob/main/CODE_OF_CONDUCT.md
|
34
|
+
rubygems_mfa_required: 'true'
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: 3.0.3
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubygems_version: 3.2.32
|
51
|
+
signing_key:
|
52
|
+
specification_version: 4
|
53
|
+
summary: A simple service skeleton framework
|
54
|
+
test_files: []
|