arq 0.1.2 → 0.2.1

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: ef806f025ee0a8964b1b8afeb31aff51e98f459bebb7985ca2c70ffb765de02a
4
- data.tar.gz: 06ce9066144c4e5796791054a37f4f1e5850e5b99d30aa0802f3322c178a299d
3
+ metadata.gz: 83941043c326bd9f80903aa4755c49ae3affe39584f12d3757ab8a05e0878987
4
+ data.tar.gz: d94724fe95ba5dfe444c3d2587357ec60c825a61c5a0a5743a55b8c44c2b3b05
5
5
  SHA512:
6
- metadata.gz: 5b6c103b2de9f4534f8f77659b3a50aaabb57c4dcac1f11fc229cd912cb6611821aa586bdc1b0c0a3f6e3f48c325e037200079a20bad9256004685c03670b355
7
- data.tar.gz: 46e5ece77eb839e06b6c39ec14fe6f74571f7505e60e83350bcb580038d4bdf8b17839a748d4ccf091f0b252a826fbbb8e4c032a9be9f5d39f1da6487d5919ec
6
+ metadata.gz: 418c2ba2e7f9373056b1eb52503c9af47749921c8c696bdf4dce98bcf0649684eab3f2a48742f3943aed00839c1e776178e0fc4b0932409203d7c04891fa52bf
7
+ data.tar.gz: 543ada56b97ebef9d58fd01e614583aca4a123707917bd3d11607e2645c6ae4688db1b942be279fd0fac86c0986d6d5f7d9c9c198d8ee5742efaffbcced27f88
data/lib/arq/action.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  module Arq
4
4
  # Module to extend to create an action. Exposes confing functions #call, #params, and #returns.
5
5
  module Action
6
- def call(ctx)
6
+ def call(ctx = Arq::Context.new)
7
7
  ctx = transform_input_context(ctx)
8
8
 
9
9
  generate_runnable(ctx).call
data/lib/arq/errors.rb CHANGED
@@ -22,7 +22,10 @@ module Arq
22
22
  end
23
23
  end
24
24
 
25
- # Raised internally in [Arq::Runnable#hard_fail!] to escape the current action.
26
- class FailureError < StandardError
25
+ # Raised internally in [Arq::Runnable#fail!] to escape the current action.
26
+ # FailureError extends from Exception to avoid being rescued as a StandardError.
27
+ # rubocop:disable Lint/InheritException
28
+ class FailureError < Exception
27
29
  end
30
+ # rubocop:enable Lint/InheritException
28
31
  end
data/lib/arq/runnable.rb CHANGED
@@ -16,9 +16,7 @@ module Arq
16
16
 
17
17
  validate_required_params
18
18
 
19
- import_context
20
19
  val = run_block
21
- export_context
22
20
 
23
21
  # If the block returned an array, attempt to run
24
22
  run_sequence(val) if val.is_a?(Array)
@@ -33,20 +31,22 @@ module Arq
33
31
  Arq::Runnable.new(@ctx, [], [], &block)
34
32
  end
35
33
 
36
- def fail!(message)
34
+ def fail!(message = nil)
37
35
  @ctx.fail!(message)
38
36
  end
39
37
 
40
- def fail_now!(message)
38
+ def fail_now!(message = nil)
41
39
  @ctx.fail_now!(message)
42
40
  end
43
41
 
44
42
  private
45
43
 
46
44
  def run_block
47
- instance_eval(&@block)
48
- rescue Arq::FailureError
49
- nil
45
+ with_context do
46
+ instance_eval(&@block)
47
+ rescue Arq::FailureError
48
+ nil
49
+ end
50
50
  end
51
51
 
52
52
  def run_sequence(sequence)
@@ -60,20 +60,39 @@ module Arq
60
60
  end
61
61
  end
62
62
 
63
- def import_context
63
+ # Runs the block with context parameters set as instance variables, then
64
+ # imports all context parameters and any new instance vars into the context.
65
+ # Returns the return value of the block.
66
+ def with_context
67
+ # Grab all current variables to know what is being returned
68
+ before_vars = instance_variables
69
+
70
+ # Load all context parameters into runnable instance
71
+ import_context_to_vars
72
+
73
+ # Run block
74
+ val = yield
75
+
76
+ # Grab all ctx + new vars
77
+ ctx_vars = instance_variables - before_vars
78
+
79
+ # Import instance vars into ctx
80
+ import_vars_to_context(ctx_vars)
81
+
82
+ # Return block value
83
+ val
84
+ end
85
+
86
+ def import_context_to_vars
64
87
  @ctx.each do |key, val|
65
88
  instance_variable_set(:"@#{key}", val)
66
89
  end
67
90
  end
68
91
 
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)
92
+ def import_vars_to_context(vars)
93
+ vars.each do |var|
94
+ key = var[1..].to_sym
95
+ @ctx[key] = instance_variable_get(var)
77
96
  end
78
97
  end
79
98
 
data/lib/arq/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Arq
4
- VERSION = "0.1.2"
4
+ VERSION = "0.2.1"
5
5
  end
data/lib/arq.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "arq/version"
4
- require "arq/errors"
5
- require "arq/context"
6
- require "arq/runnable"
7
- require "arq/action"
4
+ require_relative "arq/errors"
5
+ require_relative "arq/context"
6
+ require_relative "arq/runnable"
7
+ require_relative "arq/action"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arq
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kavin Phan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-09-05 00:00:00.000000000 Z
11
+ date: 2022-09-09 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: A service skeleton framework heavily inspired by LightService with the
14
14
  primary goal of being less verbose.
@@ -30,7 +30,7 @@ licenses:
30
30
  metadata:
31
31
  homepage_uri: https://github.com/kphan32/arq
32
32
  source_code_uri: https://github.com/kphan32/arq
33
- changelog_uri: https://github.com/kphan32/arq/blob/main/CODE_OF_CONDUCT.md
33
+ changelog_uri: https://github.com/kphan32/arq/blob/main/CHANGELOG.md
34
34
  documentation_uri: https://rubydoc.info/github/kphan32/arq/main
35
35
  rubygems_mfa_required: 'true'
36
36
  post_install_message: