cuban_linx 0.0.0 → 0.1.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: defd7aaf5bf2caafed8830cb55354ffd53ecdfb8bf9c35b47a5189ff25d503bb
4
- data.tar.gz: e796eb1ae99263147ab6fb3b7518be4e84c2f2a1b1e719637a4f7fa2d7de634c
3
+ metadata.gz: 3cbd106dba60a5f0304b201a52fe8530b42280c5aabf09d498aa63985387120a
4
+ data.tar.gz: 3572c43a2429200eb48ef85149b0c795d9f018aaa7dfa4a6fa3b1d9230c04a21
5
5
  SHA512:
6
- metadata.gz: 5fbab150e1ef8b97ceb4bd71eb82111a7954b3f11d49d4d1521988a95fe797d2962bc71ae1639ca4c84bbdbfc06c312b35e0262e406ff36947136c9b05e20a8a
7
- data.tar.gz: c430433576ad922747dd28561f3f783efdf59425d4f8ca14cdb46a48861ac3b14dcaca00ca3068d00a45ab286afa5f2d0551830a467c2b5e976ddf637d18637b
6
+ metadata.gz: e15ec75a4e82d8c236f02fdf407f18b38731be6855336f264ec5ae6845c301a718a3670760da51ad8001574d39d05c7c93f2906f3e5f762413affda3f6d2b0c9
7
+ data.tar.gz: eb3e7f3b362534d8710dfdc1a07971c7bde2f8ba16cc6694cf35d03b23d458aa3d578bba2ffea9b468f82b8ca26478c84733dd6752aa9b953dd9cf953021550b
@@ -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,30 @@
1
+ module CubanLinx
2
+ class Chain
3
+ def initialize(functions, context:)
4
+ @function_chain = functions.map { |function| chainable_method(context, function) }
5
+ end
6
+
7
+ def call(**initial_args, &block)
8
+ function_chain.reduce(Payload.new(:ok, initial_args), &reducer).as_result.then do |result|
9
+ block_given? ? block.call(result) : result
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def reducer
16
+ ->(memo, function) { function.call(memo) }
17
+ end
18
+
19
+ def chainable_method(context, function)
20
+ case function
21
+ in Symbol => method_name
22
+ Collaborator.new(context, context.public_send(method_name))
23
+ in Proc => callable
24
+ Collaborator.new(context, callable)
25
+ end
26
+ end
27
+
28
+ attr_reader :function_chain
29
+ end
30
+ end
@@ -0,0 +1,42 @@
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
+ def handle(payload, result)
24
+ case result
25
+ in :ok | nil
26
+ payload.tuple
27
+ in [:ok, messages]
28
+ [:ok, payload.add(messages), payload.errors]
29
+ in [:ok, messages, errors]
30
+ [:ok, payload.add(messages), payload.add_errors(errors)]
31
+ in [:no_op, messages]
32
+ [:no_op, payload.add(messages)]
33
+ in [:no_op, messages, errors]
34
+ [:no_op, payload.add(messages), payload.add_errors(errors)]
35
+ in [:error, messages, errors]
36
+ [:error, payload.add(messages), payload.add_errors(errors)]
37
+ in [:error, errors]
38
+ [:error, payload.messages, payload.add_errors(errors)]
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,75 @@
1
+ require "set"
2
+
3
+ module CubanLinx
4
+ class Payload
5
+ def initialize(status, messages = {}, errors = {})
6
+ @status = status
7
+ @messages = MessageHash.new(messages, initial_keys: :warnings)
8
+ @errors = hash_with_sets.merge(errors)
9
+ end
10
+
11
+ def fetch(*args, &block)
12
+ if block_given?
13
+ messages.fetch(*args, &block)
14
+ else
15
+ messages.fetch(*args)
16
+ end
17
+ end
18
+
19
+ def tuple
20
+ [status, messages, errors]
21
+ end
22
+
23
+ def as_result
24
+ [status, { messages: messages, errors: errors }]
25
+ end
26
+
27
+ def add(new_messages)
28
+ messages.merge(new_messages)
29
+ end
30
+
31
+ def add_errors(error_messages)
32
+ errors.merge(error_messages)
33
+ end
34
+
35
+ def warnings
36
+ messages.fetch(:warnings)
37
+ end
38
+
39
+ def delete(key)
40
+ raise KeyError unless messages.key?(key)
41
+
42
+ messages.delete(key)
43
+ end
44
+
45
+ attr_reader :status, :messages, :errors
46
+
47
+ private
48
+
49
+ def hash_with_sets
50
+ Hash.new { |hash, key| hash[key] = Set.new }
51
+ end
52
+
53
+ class MessageHash < Hash
54
+ def initialize(messages, initial_keys: [])
55
+ super() { |hash, key| hash[key] = Set.new }
56
+ .merge!(messages)
57
+ .tap { |hash| Array(initial_keys).each { |key| hash[key] } }
58
+ end
59
+
60
+ def merge(other_hash)
61
+ super(other_hash) do |_key, val1, val2|
62
+ case [val1, val2]
63
+ in [Set => set1, Array => collection]
64
+ set1 + collection
65
+ in [Set => set1, val]
66
+ set1 << val
67
+ else
68
+ val2
69
+ end
70
+ end
71
+ end
72
+ end
73
+ private_constant :MessageHash
74
+ end
75
+ end
data/lib/cuban_linx.rb CHANGED
@@ -1 +1,4 @@
1
- require 'lib/chain_letter/call_chain'
1
+ require_relative "./cuban_linx/call_chain"
2
+ require_relative "./cuban_linx/chain"
3
+ require_relative "./cuban_linx/collaborator"
4
+ require_relative "./cuban_linx/payload"
data/lib/version.rb ADDED
@@ -0,0 +1 @@
1
+ VERSION = '0.1.2'.freeze
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cuban_linx
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.2
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-04-06 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2022-04-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '='
18
+ - !ruby/object:Gem::Version
19
+ version: 3.11.0
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '='
25
+ - !ruby/object:Gem::Version
26
+ version: 3.11.0
13
27
  description: For procedural ruby
14
28
  email: ryancmoret@gmail.com
15
29
  executables: []
@@ -17,6 +31,11 @@ extensions: []
17
31
  extra_rdoc_files: []
18
32
  files:
19
33
  - lib/cuban_linx.rb
34
+ - lib/cuban_linx/call_chain.rb
35
+ - lib/cuban_linx/chain.rb
36
+ - lib/cuban_linx/collaborator.rb
37
+ - lib/cuban_linx/payload.rb
38
+ - lib/version.rb
20
39
  homepage: https://rubygems.org/gems/cuban_linx
21
40
  licenses:
22
41
  - MIT
@@ -36,7 +55,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
36
55
  - !ruby/object:Gem::Version
37
56
  version: '0'
38
57
  requirements: []
39
- rubygems_version: 3.2.32
58
+ rubygems_version: 3.0.3
40
59
  signing_key:
41
60
  specification_version: 4
42
61
  summary: Chainable functions