lab42_monad 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 5936231b5f9f69a72bcb91fdc9b72b49461eb37dff6ec3e3e617ea922d0c9528
4
+ data.tar.gz: 286720a05832279efc3f313a89c5faac47c048a65fed017d89cde65c4b4be8a5
5
+ SHA512:
6
+ metadata.gz: 5d1d88f207260529a13a9241ea1fefc9ed0eac6929fe8bc21d478595f16f4397fe96926c5814f2a24451fb6d8af8e4e1212cc5d41c6f22c2fbdd41a0da0cbdc9
7
+ data.tar.gz: 2304f3595aae584568aa3ec5d625cd36681972feeb2695154fae7329447654b03aa39dfb618ff3cc2eae62da09977a6bb856b00ca8737014aafa34436dc967f3
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lab42
4
+ module Monad
5
+ class ContractViolation < RuntimeError; end
6
+ end
7
+ end
8
+ # SPDX-License-Identifier: Apache-2.0
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'contract_violation'
4
+ module Lab42
5
+ module Monad
6
+ class Interact
7
+ def run(interactor)
8
+ result = _run_stdin(interactor)
9
+
10
+ if stdout
11
+ _interact(result)
12
+ else
13
+ result
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ attr_reader :args, :kwds, :stdin, :stdout
20
+
21
+ def initialize(args:, kwds:, stdin:, stdout:)
22
+ @args = args
23
+ @kwds = kwds.delete_if { [:stdin, :stdout].include?(_1) }
24
+ # WANT:
25
+ @stdin = stdin
26
+ @stdout = stdout
27
+ end
28
+
29
+ def _interact(result)
30
+ case result
31
+ in [:stdout, r]
32
+ $stdout.puts r
33
+ [:ok, r]
34
+ in [:stderr, r]
35
+ $stderr.puts r
36
+ [:error, r]
37
+ else
38
+ raise ContractViolation, "result is not of the required format [:stdout|:stderr, value] but:\n\n#{result.inspect}"
39
+ end
40
+ end
41
+
42
+ def _run_stdin(interactor)
43
+ case stdin
44
+ when true
45
+ interactor.($stdin.readlines(chomp: true), *args, **kwds)
46
+ when :lazy
47
+ interactor.($stdin.each_line(chomp: true), *args, **kwds)
48
+ else
49
+ interactor.(*args, **kwds)
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ # SPDX-License-Identifier: Apache-2.0
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'monad/interact'
4
+ module Lab42
5
+ module Monad
6
+ VERSION = "0.1.2"
7
+ def self.interact(interactor, *args, **kwds)
8
+ { stdin: true, stdout: true }.merge(kwds) => { stdin:, stdout: }
9
+ Interact
10
+ .new(args:, kwds:, stdin:, stdout:)
11
+ .run(interactor)
12
+ end
13
+ end
14
+ end
15
+ # SPDX-License-Identifier: Apache-2.0
metadata ADDED
@@ -0,0 +1,45 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lab42_monad
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - Robert Dober
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2022-09-04 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Implements only some simple monadic patterns right now, à la `IO.interact`
14
+ email: robert.dober@gmail.com
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - lib/lab42/monad.rb
20
+ - lib/lab42/monad/contract_violation.rb
21
+ - lib/lab42/monad/interact.rb
22
+ homepage: https://github.com/robertdober/lab42_monad
23
+ licenses:
24
+ - Apache-2.0
25
+ metadata: {}
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: 3.1.0
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ version: '0'
40
+ requirements: []
41
+ rubygems_version: 3.3.7
42
+ signing_key:
43
+ specification_version: 4
44
+ summary: Simple Monadic Behaviors for Ruby
45
+ test_files: []