bountiful_blocks 1.0.0 → 1.1.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: 931ac5652dcb409ecd827ff2f7adb6daf3e102b344fe8d382ac636641bff7312
4
- data.tar.gz: 7f3ad476b417b2fc042491d103943a5ccb57fbe8da3b4b1b39c0cc44617382bc
3
+ metadata.gz: 67aa1f4e399b6e73e1b336b1c5c1bb93d11dd32f661fcaaa5f9c4436285dc75d
4
+ data.tar.gz: bf6ab6419e32e4bc6cab4859654afce4c968a5e1505469a8dc7cd626ab037970
5
5
  SHA512:
6
- metadata.gz: 563b2ed6adb3987cd598edbf28affcc7b72b143867110ebe52a64a5f6604fc5f8daff13460d067c566452764cf8d625d7dc4400d5486da73a24f0f40a21febf2
7
- data.tar.gz: 5136107120ac4e8b8d8f4e7fd244d8e18594da36aa6dfa2b6eee40a00ebaf2ee15ce9b8ac9c9c4c067ae10a70aa23564a728c6cbdc3b07085f4662f617b870bd
6
+ metadata.gz: a5f002cf969382785b756b1e04e62ca870b0d941a824875619666733ce0f708b4e6574d8c24122f582669a579dad0e82452db6381dc7c216be78cd45ffb93ace
7
+ data.tar.gz: f808fb510faceaf5c1e2aee72ac641cc34e985dca9919d5b7242500f33ab0a1fc3fa52ded5494ab4aa5a9b00a0bde010da6959930409b1cd4572745955d8b4ab
data/CHANGELOG.md CHANGED
@@ -1,5 +1,37 @@
1
1
  # Changelog
2
2
 
3
+ <!--[//]: # (
4
+ ## <Release number> <Date YYYY-MM-DD>
5
+ ### Breaking changes
6
+ ### Deprecations
7
+ ### New features
8
+ ### Bug fixes
9
+ )-->
10
+
11
+ ## 1.1.1 2023-10-12
12
+
13
+ ### New features
14
+
15
+ - Added `given_blocks!` utility method.
16
+
17
+ ### Bug fixes
18
+
19
+ - The `given!` utility method was never filled during instantiation, now it works as described.
20
+ - Trying to invoke a block that was not passed now produces `NoMethodError` instead of `FrozenError`.
21
+
22
+ ## 1.1.0 2023-09-07
23
+
24
+ ### New features
25
+
26
+ - Added `raw!`, `given!`, and `call_all!` utility methods.
27
+ - Now multiblocks are always frozen after construction.
28
+ - Added RBS type signatures.
29
+
30
+ ### Bug fixes
31
+
32
+ - Fixed enforcing of block names limitations.
33
+ - Removed the `Error` class.
34
+
3
35
  ## 1.0.0 2023-05-30
4
36
 
5
37
  First release. Refer to [README.md](README.md) for the full documentation.
data/README.md CHANGED
@@ -94,6 +94,20 @@ def deliver message, &block
94
94
  end
95
95
  ```
96
96
 
97
+ ### Names
98
+
99
+ Block names follow the same rules as method names, with the exception that they can't end in `!`, `?`, or `=`.
100
+
101
+ ### Utility methods
102
+
103
+ To avoid conflicts, all utility method names will end in either `!`, `?`, or `=`.
104
+
105
+ - `raw!` returns the value returned by the block, which can be useful to allow a block to work both as a regular block and a multiblock.
106
+ - `given?(name)` returns `true` if a block named `name` was provided.
107
+ - `given!` returns the names of all the provided blocks.
108
+ - `given_blocks!` returns all the provided blocks in a hash indexed by their names.
109
+ - `call_all!` returns a Hash that maps all block names to their results. You can provide arguments to `call_all!` and they will be forwarded in turn to all blocks. Notice that since multiblocks are frozen, `call_all!` can't cache the Hash and must create a new one on every invocation.
110
+
97
111
  ## Version numbers
98
112
 
99
113
  BountifulBlocks loosely follows [Semantic Versioning](https://semver.org/), with a hard guarantee that breaking changes to the public API will always coincide with an increase to the `MAJOR` number.
@@ -1,28 +1,61 @@
1
1
  module BountifulBlocks
2
2
  class Multiblock
3
3
  def initialize *required_blocks, &block
4
- instance_exec(&block)
4
+ @raw = instance_exec(&block)
5
5
 
6
6
  missing_blocks = required_blocks.reject { |method_name| given? method_name }
7
7
 
8
8
  raise ArgumentError, "Block required for #{missing_blocks.join ', '}" unless missing_blocks.empty?
9
+
10
+ singleton_class.class_exec do
11
+ undef_method :method_missing
12
+ undef_method :respond_to_missing?
13
+ end
14
+
15
+ given!.freeze
16
+ given_blocks!.freeze
17
+ freeze
9
18
  end
10
19
 
11
20
  def method_missing(name, *args, **kwargs, &block)
12
- super unless args.empty? && kwargs.empty?
13
- super if name.end_with? '!', '?', '='
21
+ return super unless args.empty? && kwargs.empty?
22
+ return super if name.end_with? '!', '?', '='
14
23
 
15
24
  define_singleton_method name, &block
25
+ given! << name
26
+ given_blocks![name] = block
27
+
28
+ nil
16
29
  end
17
30
 
18
31
  def respond_to_missing?(name, *args, **kwargs, &block)
19
- super if name.end_with? '!', '?', '='
32
+ return super if name.end_with? '!', '?', '='
20
33
 
21
34
  true
22
35
  end
23
36
 
24
37
  def given? name
25
- singleton_class.method_defined? name
38
+ given_blocks!.key? name
39
+ end
40
+
41
+ def given!
42
+ @given = [] unless defined?(@given)
43
+
44
+ @given
45
+ end
46
+
47
+ def given_blocks!
48
+ @given_blocks = {} unless defined?(@given_blocks)
49
+
50
+ @given_blocks
51
+ end
52
+
53
+ def raw!
54
+ @raw
55
+ end
56
+
57
+ def call_all!(*args, **kwargs, &block)
58
+ given_blocks!.transform_values { |given_block| given_block.call(*args, **kwargs, &block) }
26
59
  end
27
60
  end
28
61
  end
@@ -1,3 +1,3 @@
1
1
  module BountifulBlocks
2
- VERSION = '1.0.0'.freeze
2
+ VERSION = '1.1.1'.freeze
3
3
  end
@@ -1,8 +1,4 @@
1
1
  require_relative 'bountiful_blocks/version'
2
2
 
3
- module BountifulBlocks
4
- class Error < StandardError; end
5
- end
6
-
7
3
  require_relative 'bountiful_blocks/multiblock'
8
4
  require_relative 'bountiful_blocks/kernel_patch'
@@ -0,0 +1,3 @@
1
+ module Kernel
2
+ def Multiblock: (*Symbol required_blocks) { () -> untyped } -> BountifulBlocks::Multiblock
3
+ end
@@ -0,0 +1,9 @@
1
+ module BountifulBlocks
2
+ class Multiblock
3
+ def initialize: (*Symbol required_blocks) { () -> untyped } -> void
4
+ def given?: (Symbol name) -> bool
5
+ def given!: () -> Array[Symbol]
6
+ def raw!: () -> untyped
7
+ def call_all!: () -> Hash[Symbol, untyped]
8
+ end
9
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bountiful_blocks
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Moku S.r.l.
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2023-05-30 00:00:00.000000000 Z
12
+ date: 2023-10-12 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: A simple and idiomatic way to pass multiple blocks to a method.
15
15
  email:
@@ -28,6 +28,8 @@ files:
28
28
  - lib/bountiful_blocks/kernel_patch.rb
29
29
  - lib/bountiful_blocks/multiblock.rb
30
30
  - lib/bountiful_blocks/version.rb
31
+ - sig/lib/bountiful_blocks/kernel_patch.rbs
32
+ - sig/lib/bountiful_blocks/multiblock.rbs
31
33
  homepage: https://github.com/moku-io/bountiful_blocks
32
34
  licenses:
33
35
  - MIT
@@ -50,7 +52,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
50
52
  - !ruby/object:Gem::Version
51
53
  version: '0'
52
54
  requirements: []
53
- rubygems_version: 3.1.6
55
+ rubygems_version: 3.4.10
54
56
  signing_key:
55
57
  specification_version: 4
56
58
  summary: A simple and idiomatic way to pass multiple blocks to a method.