action_sequence 0.0.1

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: 77ee7260c00343238d0585e9719df521f9a8a68ed365c49b2c929a22ee71c98b
4
+ data.tar.gz: a121847ce23c49ca488119553f134aded0785c8f61f151d9efbdc5c107ce78f1
5
+ SHA512:
6
+ metadata.gz: 4978e0d73affe9269ad682d6fb452d8a42751d6af21e797a30b38c26a3ba3c634a924378a41cc45df772bab4c077806ea14fd11fe1ec33014ba29992d96d2711
7
+ data.tar.gz: 177a8d4a34791a06d06330fc3668170c0d5ebb9919a4d46028103f0f8bca049c666cba285393f52a2c463d30565464950bed88ef1995e8d31261666fdb47b846
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative './action_sequence/context'
4
+ require_relative './action_sequence/sequence'
5
+ require_relative './action_sequence/transaction_sequence.rb'
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionSequence
4
+ ###
5
+ # Context container to hold state for an
6
+ # Sequence of Actions
7
+ ##
8
+ class Context
9
+ attr_reader :error_message
10
+
11
+ def initialize(initial_context: {})
12
+ @context = initial_context
13
+ @error_message = nil
14
+ end
15
+
16
+ def add_to_context!(hash)
17
+ @context.merge!(hash)
18
+ end
19
+
20
+ def failed?
21
+ !error_message.nil?
22
+ end
23
+
24
+ def success?
25
+ error_message.nil?
26
+ end
27
+
28
+ def fail_context!(error_message)
29
+ @error_message = error_message
30
+ end
31
+
32
+ def fetch(key)
33
+ @context.fetch(key)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionSequence
4
+ ###
5
+ # A Sequence calls an array of actions over a shared context,
6
+ # skipping the remaining actions if the context has failed
7
+ ##
8
+ class Sequence
9
+ def initialize(actions: [], initial_context: {})
10
+ @actions = actions
11
+ @context = Context.new(initial_context: initial_context)
12
+ end
13
+
14
+ def call
15
+ @actions.each do |action|
16
+ return @context if @context.failed?
17
+
18
+ action.call(@context)
19
+ end
20
+
21
+ @context
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ActionSequence
4
+ ###
5
+ # A Sequence that calls its actions in a transaction
6
+ ##
7
+ class TransactionSequence < Sequence
8
+ def call
9
+ ::ActiveRecord::Base.transaction do
10
+ super
11
+ end
12
+ end
13
+ end
14
+ end
metadata ADDED
@@ -0,0 +1,46 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: action_sequence
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - James A. Sral
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-12-18 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: A small gem for managing sequences of actions over a shared-context
14
+ email: jamessral@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/action_sequence.rb
20
+ - lib/action_sequence/context.rb
21
+ - lib/action_sequence/sequence.rb
22
+ - lib/action_sequence/transaction_sequence.rb
23
+ homepage: https://rubygems.org/gems/action_sequence
24
+ licenses:
25
+ - MIT
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubygems_version: 3.0.3
43
+ signing_key:
44
+ specification_version: 4
45
+ summary: A small gem for managing sequences of actions over a shared-context
46
+ test_files: []