callback-collection 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 78150ace9b1f03ae373409b70582aab42fded3b8ca7cf7268c9e15fcc8eba8a6
4
+ data.tar.gz: 98fa5ae6f33bc0116f4510d58f698c5e493fd17582843f6b067888d9cf5d82db
5
+ SHA512:
6
+ metadata.gz: 9473879ba7b4f83ce62116855eff9b7dac6ea4923ed4cd43610490be669792bea01ed6ec273c8de58351b127a33efd36ecf0670da0a681d6116d6ebbc6fa9db4
7
+ data.tar.gz: f4decc447ab5a9b79a6531fdaf40d0cadec9ecfca4f0f460d85ac8c629df1ae7f4d39aaa83b8bf23f822a7d7558133a4ac99cfb278bbe8364e4a6b2aa348bf4d
data/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # Callback Collection
2
+
3
+ [![Build Status](https://github.com/nicolasva/callback-collection/actions/workflows/ci.yml/badge.svg)](https://github.com/nicolasva/callback-collection/actions/workflows/ci.yml)
4
+ [![Code Climate](https://codeclimate.com/github/nicolasva/callback-collection.svg)](https://codeclimate.com/github/nicolasva/callback-collection)
5
+ [![Gem Version](https://badge.fury.io/rb/callback-collection.svg)](https://badge.fury.io/rb/callback-collection)
6
+ [![Documentation Status](https://img.shields.io/badge/docs-RubyDoc.info-blue.svg)](https://www.rubydoc.info/gems/callback-collection)
7
+ [![Downloads](https://img.shields.io/gem/dt/callback-collection.svg?style=flat)](https://rubygems.org/gems/callback-collection)
8
+
9
+ Une petite gem Ruby permettant de définir une collection immuable de callbacks
10
+ nommés.
11
+
12
+ ## Installation
13
+
14
+ Installez la gem depuis RubyGems :
15
+
16
+ ```sh
17
+ gem install callback-collection
18
+ ```
19
+
20
+ Avec Bundler, ajoutez-la au `Gemfile` :
21
+
22
+ ```ruby
23
+ gem "callback-collection"
24
+ ```
25
+
26
+ ## Utilisation
27
+
28
+ ```ruby
29
+ require "callback_collection"
30
+
31
+ callbacks = CallbackCollection.new do |collection|
32
+ collection.success { |name| "Bienvenue, #{name} !" }
33
+ collection.failure { |error| "Erreur : #{error.message}" }
34
+ end
35
+
36
+ callbacks.respond_with(:success, "Ruby")
37
+ # => "Bienvenue, Ruby !"
38
+ ```
39
+
40
+ La collection est figée à la fin de son initialisation. Toute tentative
41
+ d'ajouter ensuite un callback lève une `FrozenError`.
42
+
43
+ ## Développement
44
+
45
+ ```sh
46
+ bundle install
47
+ bundle exec rake
48
+ ```
49
+
50
+ La tâche par défaut exécute la suite Minitest et construit la gem dans `pkg/`.
51
+ L'intégration continue GitHub Actions vérifie le projet avec Ruby 2.7 à 3.4,
52
+ ainsi qu'avec la dernière version stable, Ruby 4.0.
53
+
54
+ ## Publication
55
+
56
+ La publication sur RubyGems utilise
57
+ [Trusted Publishing](https://guides.rubygems.org/trusted-publishing/) et ne
58
+ nécessite aucune clé API dans les secrets GitHub.
59
+
60
+ Avant la première publication, créez un *Pending Trusted Publisher* dans votre
61
+ profil RubyGems avec les paramètres suivants :
62
+
63
+ - gem : `callback-collection`
64
+ - propriétaire du dépôt : `nicolasva`
65
+ - dépôt : `callback-collection`
66
+ - workflow : `release.yml`
67
+ - environnement GitHub : `release`
68
+
69
+ Publiez ensuite une version en poussant le tag correspondant à
70
+ `CallbackCollection::VERSION` :
71
+
72
+ ```sh
73
+ git tag v0.1.0
74
+ git push origin v0.1.0
75
+ ```
76
+
77
+ GitHub Actions construit et publie alors la gem. RubyDoc génère
78
+ automatiquement sa documentation, et les badges de version et de
79
+ téléchargements deviennent actifs après la première publication.
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ class CallbackCollection
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "callback_collection/version"
4
+
5
+ # Stores named callbacks during initialization, then exposes thread-safe reads.
6
+ #
7
+ # @example
8
+ # callbacks = CallbackCollection.new do |collection|
9
+ # collection.success { |value| "Received #{value}" }
10
+ # end
11
+ #
12
+ # callbacks.respond_with(:success, "data")
13
+ # # => "Received data"
14
+ class CallbackCollection
15
+ def initialize
16
+ callbacks
17
+ yield(self) if block_given?
18
+ callbacks.freeze
19
+ end
20
+
21
+ def respond_with(callback, *args, **kwargs, &block)
22
+ handler = callbacks.fetch(callback) do
23
+ raise NoMethodError, "No callback '#{callback}' is defined."
24
+ end
25
+
26
+ return handler.call(*args, &block) if kwargs.empty?
27
+
28
+ handler.call(*args, **kwargs, &block)
29
+ end
30
+
31
+ def method_missing(method_name, *args, &block)
32
+ if block
33
+ raise FrozenError, "Cannot define a callback after initialization." if callbacks.frozen?
34
+
35
+ callbacks[method_name] = block
36
+ self
37
+ else
38
+ super
39
+ end
40
+ end
41
+
42
+ def respond_to_missing?(method_name, include_private = false)
43
+ callbacks.key?(method_name) || super
44
+ end
45
+
46
+ protected
47
+
48
+ def callbacks
49
+ @callbacks ||= {}
50
+ end
51
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: callback-collection
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Nicolas Vandenbogaerde
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: minitest
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - ">="
17
+ - !ruby/object:Gem::Version
18
+ version: '5'
19
+ - - "<"
20
+ - !ruby/object:Gem::Version
21
+ version: '7'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ version: '5'
29
+ - - "<"
30
+ - !ruby/object:Gem::Version
31
+ version: '7'
32
+ - !ruby/object:Gem::Dependency
33
+ name: rake
34
+ requirement: !ruby/object:Gem::Requirement
35
+ requirements:
36
+ - - "~>"
37
+ - !ruby/object:Gem::Version
38
+ version: '13.0'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - "~>"
44
+ - !ruby/object:Gem::Version
45
+ version: '13.0'
46
+ description: Define an immutable collection of named callbacks and invoke them with
47
+ arguments.
48
+ executables: []
49
+ extensions: []
50
+ extra_rdoc_files: []
51
+ files:
52
+ - README.md
53
+ - lib/callback_collection.rb
54
+ - lib/callback_collection/version.rb
55
+ homepage: https://github.com/nicolasva/callback-collection
56
+ licenses: []
57
+ metadata:
58
+ rubygems_mfa_required: 'true'
59
+ documentation_uri: https://www.rubydoc.info/gems/callback-collection/0.1.0
60
+ source_code_uri: https://github.com/nicolasva/callback-collection
61
+ rdoc_options: []
62
+ require_paths:
63
+ - lib
64
+ required_ruby_version: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '2.7'
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ requirements: []
75
+ rubygems_version: 4.0.20
76
+ specification_version: 4
77
+ summary: A small callback collection with a concise Ruby DSL
78
+ test_files: []