cuban_linx 0.1.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/cuban_linx/call_chain.rb +19 -0
- data/lib/cuban_linx/chain.rb +33 -0
- data/lib/cuban_linx/collaborator.rb +44 -0
- data/lib/cuban_linx/payload.rb +91 -0
- data/lib/version.rb +1 -0
- metadata +43 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9a55ffb699b58bfcec78b14013e24d06dfdf39bdb27341fcff9bedb77586188d
|
4
|
+
data.tar.gz: 858e3b86c883f8346638366dac2083a9bb5ffe0533d1934823501c611cbbaa3e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 26040d614df6cc4519e1e6960344b4309698e6e93ee342be85a4523aa424b384d940586cf70f5ead3d628d97d94da58ad3744cb145b8ceaf6ed2459fa3995d6f
|
7
|
+
data.tar.gz: 3b268476b544a3b55803028bea772eab59cdc7d76d9209f41e9f9bd89af9b186a5812ef7d592bd580c512f20990d792166a3fc24cc983565ac6ce59db70f3af8
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module CubanLinx
|
2
|
+
module CallChain
|
3
|
+
def self.included(base)
|
4
|
+
base.extend(ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
private
|
9
|
+
|
10
|
+
def define_link(name, &function)
|
11
|
+
define_method(name) { function }
|
12
|
+
end
|
13
|
+
|
14
|
+
def execution_chain(name, functions:)
|
15
|
+
define_method(name) { Chain.new(functions, context: self) }
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module CubanLinx
|
2
|
+
class Chain
|
3
|
+
def initialize(functions, context:)
|
4
|
+
@function_chain = functions.map do |function|
|
5
|
+
chainable_method(context, function)
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(**initial_args, &block)
|
10
|
+
function_chain.reduce(Payload.new(:ok, initial_args),
|
11
|
+
&reducer).as_result.then do |result|
|
12
|
+
block_given? ? block.call(result) : result
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def reducer
|
19
|
+
->(memo, function) { function.call(memo) }
|
20
|
+
end
|
21
|
+
|
22
|
+
def chainable_method(context, function)
|
23
|
+
case function
|
24
|
+
in Symbol => method_name
|
25
|
+
Collaborator.new(context, context.public_send(method_name))
|
26
|
+
in Proc => callable
|
27
|
+
Collaborator.new(context, callable)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
attr_reader :function_chain
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module CubanLinx
|
2
|
+
class Collaborator
|
3
|
+
def initialize(context, function)
|
4
|
+
@context = context
|
5
|
+
@function = function
|
6
|
+
end
|
7
|
+
|
8
|
+
attr_reader :function, :context
|
9
|
+
|
10
|
+
def call(payload)
|
11
|
+
case payload.status
|
12
|
+
in :ok
|
13
|
+
context.instance_exec(payload, &function).then do |result|
|
14
|
+
Payload.new(*handle(payload, result))
|
15
|
+
end
|
16
|
+
in :no_op | :error
|
17
|
+
payload
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
# rubocop:disable Metrics/MethodLength
|
24
|
+
def handle(payload, result)
|
25
|
+
case result
|
26
|
+
in :ok | true | nil
|
27
|
+
payload.tuple
|
28
|
+
in [:ok, messages]
|
29
|
+
[:ok, payload.add(messages), payload.errors]
|
30
|
+
in [:ok, messages, errors]
|
31
|
+
[:ok, payload.add(messages), payload.add_errors(errors)]
|
32
|
+
in [:no_op, messages]
|
33
|
+
[:no_op, payload.add(messages)]
|
34
|
+
in [:no_op, messages, errors]
|
35
|
+
[:no_op, payload.add(messages), payload.add_errors(errors)]
|
36
|
+
in [:error, messages, errors]
|
37
|
+
[:error, payload.add(messages), payload.add_errors(errors)]
|
38
|
+
in [:error, errors]
|
39
|
+
[:error, payload.messages, payload.add_errors(errors)]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
# rubocop:enable Metrics/MethodLength
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
require "set"
|
2
|
+
|
3
|
+
module CubanLinx
|
4
|
+
class Payload
|
5
|
+
InvalidStatusError = Class.new(StandardError)
|
6
|
+
VALID_STATUSES = %i[ok no_op error].freeze
|
7
|
+
|
8
|
+
def initialize(status, messages = {}, errors = {})
|
9
|
+
@status = determine_and_validate(status)
|
10
|
+
@messages = messages.merge({ warnings: Set.new }, &merge_block)
|
11
|
+
@errors = errors
|
12
|
+
end
|
13
|
+
|
14
|
+
def fetch(*args, &block)
|
15
|
+
if block_given?
|
16
|
+
messages.fetch(*args, &block)
|
17
|
+
else
|
18
|
+
messages.fetch(*args)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def tuple
|
23
|
+
[status, messages, errors]
|
24
|
+
end
|
25
|
+
|
26
|
+
def as_result
|
27
|
+
[status, { messages: messages, errors: errors }]
|
28
|
+
end
|
29
|
+
|
30
|
+
def add(new_messages)
|
31
|
+
messages.merge(new_messages, &merge_block)
|
32
|
+
end
|
33
|
+
|
34
|
+
def add_errors(error_messages)
|
35
|
+
errors.merge(error_messages, &merge_block)
|
36
|
+
end
|
37
|
+
|
38
|
+
def add_warning(message)
|
39
|
+
add(warnings: message)
|
40
|
+
end
|
41
|
+
|
42
|
+
def warnings
|
43
|
+
messages.fetch(:warnings)
|
44
|
+
end
|
45
|
+
|
46
|
+
def delete(key)
|
47
|
+
raise KeyError unless messages.key?(key)
|
48
|
+
|
49
|
+
messages.delete(key)
|
50
|
+
end
|
51
|
+
|
52
|
+
def method_missing(method_name, *, &block)
|
53
|
+
super unless respond_to_missing?(method_name)
|
54
|
+
|
55
|
+
messages.fetch(method_name)
|
56
|
+
end
|
57
|
+
|
58
|
+
def respond_to_missing?(method_name, include_private = false)
|
59
|
+
messages.key?(method_name) || super
|
60
|
+
end
|
61
|
+
|
62
|
+
attr_reader :status, :messages, :errors
|
63
|
+
|
64
|
+
private
|
65
|
+
|
66
|
+
def determine_and_validate(status)
|
67
|
+
return status.to_sym if VALID_STATUSES.include?(status.to_sym)
|
68
|
+
|
69
|
+
raise InvalidStatusError,
|
70
|
+
"Received #{status.inspect}. "\
|
71
|
+
"Should be one of: #{VALID_STATUSES.inspect}"
|
72
|
+
end
|
73
|
+
|
74
|
+
# rubocop:disable Metrics/MethodLength
|
75
|
+
def merge_block
|
76
|
+
lambda { |_key, val1, val2|
|
77
|
+
case [val1, val2]
|
78
|
+
in [Set => set1, Array => collection]
|
79
|
+
set1 + collection
|
80
|
+
in [Set => set1, Set => set2]
|
81
|
+
set1 + set2
|
82
|
+
in [Set => set1, val]
|
83
|
+
set1 << val
|
84
|
+
else
|
85
|
+
val2
|
86
|
+
end
|
87
|
+
}
|
88
|
+
end
|
89
|
+
# rubocop:enable Metrics/MethodLength
|
90
|
+
end
|
91
|
+
end
|
data/lib/version.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
VERSION = "1.0.0".freeze
|
metadata
CHANGED
@@ -1,29 +1,57 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cuban_linx
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Moret
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-
|
11
|
+
date: 2022-05-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: faker
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.20.0
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.20.0
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: rspec
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
16
30
|
requirements:
|
17
|
-
- -
|
31
|
+
- - '='
|
18
32
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
33
|
+
version: 3.11.0
|
20
34
|
type: :development
|
21
35
|
prerelease: false
|
22
36
|
version_requirements: !ruby/object:Gem::Requirement
|
23
37
|
requirements:
|
24
|
-
- -
|
38
|
+
- - '='
|
25
39
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
40
|
+
version: 3.11.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rubcop
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.27'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.27'
|
27
55
|
description: For procedural ruby
|
28
56
|
email: ryancmoret@gmail.com
|
29
57
|
executables: []
|
@@ -31,10 +59,16 @@ extensions: []
|
|
31
59
|
extra_rdoc_files: []
|
32
60
|
files:
|
33
61
|
- lib/cuban_linx.rb
|
62
|
+
- lib/cuban_linx/call_chain.rb
|
63
|
+
- lib/cuban_linx/chain.rb
|
64
|
+
- lib/cuban_linx/collaborator.rb
|
65
|
+
- lib/cuban_linx/payload.rb
|
66
|
+
- lib/version.rb
|
34
67
|
homepage: https://rubygems.org/gems/cuban_linx
|
35
68
|
licenses:
|
36
69
|
- MIT
|
37
|
-
metadata:
|
70
|
+
metadata:
|
71
|
+
rubygems_mfa_required: 'true'
|
38
72
|
post_install_message:
|
39
73
|
rdoc_options: []
|
40
74
|
require_paths:
|
@@ -43,14 +77,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
43
77
|
requirements:
|
44
78
|
- - ">="
|
45
79
|
- !ruby/object:Gem::Version
|
46
|
-
version:
|
80
|
+
version: 3.0.0
|
47
81
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
82
|
requirements:
|
49
83
|
- - ">="
|
50
84
|
- !ruby/object:Gem::Version
|
51
85
|
version: '0'
|
52
86
|
requirements: []
|
53
|
-
rubygems_version: 3.
|
87
|
+
rubygems_version: 3.2.15
|
54
88
|
signing_key:
|
55
89
|
specification_version: 4
|
56
90
|
summary: Chainable functions
|