power-emitter 0.0.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 +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +75 -0
- data/Rakefile +6 -0
- data/lib/emitter.rb +44 -0
- metadata +44 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: e8522337dac72f802f063d9fff0f26fe7b9f99448a8b1595b97347f717fda5cf
|
|
4
|
+
data.tar.gz: e50ccf9bec65f4dfbc7c8d796627f9cd88b679355394b9840ee2292e122cddaf
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 022a760a03b8c46b3ecd31fafe98e945f9b3aa91fdd2c09ce0e167fa56a459ad23a8f2fbe0776b40b933763aae2cd7d0ebc1a645d22e64a8b49a96c85eb8a95b
|
|
7
|
+
data.tar.gz: 6493245c97280ddac913ad5e4264c6d046c98da7d5b84b490fe2cc98cb8230e43a2a060d0a366cf5bea6f08932721b6a28577831772a8488b1d61afc0f288a01
|
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Max Power
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
|
13
|
+
all copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# Power::Emitter
|
|
2
|
+
|
|
3
|
+
# Emitter
|
|
4
|
+
|
|
5
|
+
A lightweight Ruby event emitter that allows you to bind, emit, and stop custom events using blocks.
|
|
6
|
+
|
|
7
|
+
## Features
|
|
8
|
+
|
|
9
|
+
- Bind callbacks to custom event names
|
|
10
|
+
- Emit events with arguments
|
|
11
|
+
- Stop listening to specific or all events
|
|
12
|
+
|
|
13
|
+
## API
|
|
14
|
+
|
|
15
|
+
`bind(event, &block)`
|
|
16
|
+
Registers a block (callback) to an event.
|
|
17
|
+
|
|
18
|
+
`emit(event, *args)`
|
|
19
|
+
Calls all callbacks for the given event, passing in arguments.
|
|
20
|
+
|
|
21
|
+
`stop(*events)`
|
|
22
|
+
- If called with arguments, stops those specific events.
|
|
23
|
+
- If called without arguments, clears all listeners.
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
```ruby
|
|
27
|
+
emitter = Emittable::Emitter.new
|
|
28
|
+
|
|
29
|
+
# Bind a listener
|
|
30
|
+
emitter.bind(:greet) { |name| puts "Hello, #{name}!" }
|
|
31
|
+
|
|
32
|
+
# Emit an event
|
|
33
|
+
emitter.emit(:greet, "World") # => prints "Hello, World!"
|
|
34
|
+
|
|
35
|
+
# Stop a specific event
|
|
36
|
+
emitter.stop(:greet)
|
|
37
|
+
|
|
38
|
+
# Stop all events
|
|
39
|
+
emitter.stop
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
## Example with Emittable
|
|
44
|
+
|
|
45
|
+
```ruby
|
|
46
|
+
class Chat
|
|
47
|
+
include Emittable
|
|
48
|
+
#include Emittable::Aliases
|
|
49
|
+
|
|
50
|
+
def initialize
|
|
51
|
+
@messages = []
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def add_message(msg)
|
|
55
|
+
emit :new_message, msg
|
|
56
|
+
@messages << msg
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def clear!
|
|
60
|
+
emit :clear_messages, @messages
|
|
61
|
+
@messages.clear
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
chat = Chat.new
|
|
66
|
+
chat.bind(:new_message) { |msg| puts "New Message: #{msg}" }
|
|
67
|
+
chat.bind(:clear_messages) { |msgs| puts "#{msgs.count} Messages deleted!" }
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
The Chat class includes Emittable, a mixin that adds event-driven capabilities by creating a private emitter instance method.
|
|
71
|
+
Internally, it uses Ruby’s Forwardable module to delegate bind, emit, and stop calls directly to the underlying Emitter instance.
|
|
72
|
+
|
|
73
|
+
## License
|
|
74
|
+
|
|
75
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/lib/emitter.rb
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
module Emittable
|
|
3
|
+
require "forwardable"
|
|
4
|
+
extend Forwardable
|
|
5
|
+
|
|
6
|
+
def_delegators :emitter, :bind, :emit, :stop
|
|
7
|
+
|
|
8
|
+
private
|
|
9
|
+
|
|
10
|
+
def emitter
|
|
11
|
+
@emitter ||= Emittable::Emitter.new
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
class Emitter
|
|
15
|
+
def initialize
|
|
16
|
+
@listeners = Hash.new { |h,k| h[k] = [] }
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def bind(event, &block)
|
|
20
|
+
@listeners[event] << block
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def emit(event, *args)
|
|
24
|
+
@listeners[event].map { it.call(*args) }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def stop(*events)
|
|
28
|
+
return @listeners.clear if events.empty?
|
|
29
|
+
events.each { @listeners.delete it }
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
module Aliases
|
|
34
|
+
BIND = %i[ on subscribe listen observe handle ].freeze
|
|
35
|
+
EMIT = %i[ fire publish broadcast notify trigger ].freeze
|
|
36
|
+
STOP = %i[ off unsubscribe drop ignore release ].freeze
|
|
37
|
+
|
|
38
|
+
def self.included(base)
|
|
39
|
+
BIND.each { base.alias_method it, :bind }
|
|
40
|
+
EMIT.each { base.alias_method it, :emit }
|
|
41
|
+
STOP.each { base.alias_method it, :stop }
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: power-emitter
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.2
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Max Power
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies: []
|
|
12
|
+
email:
|
|
13
|
+
- kevin.melchert@gmail.com
|
|
14
|
+
executables: []
|
|
15
|
+
extensions: []
|
|
16
|
+
extra_rdoc_files: []
|
|
17
|
+
files:
|
|
18
|
+
- LICENSE.txt
|
|
19
|
+
- README.md
|
|
20
|
+
- Rakefile
|
|
21
|
+
- lib/emitter.rb
|
|
22
|
+
homepage: https://github.com/max-power/power-emitter/
|
|
23
|
+
licenses:
|
|
24
|
+
- MIT
|
|
25
|
+
metadata: {}
|
|
26
|
+
rdoc_options: []
|
|
27
|
+
require_paths:
|
|
28
|
+
- lib
|
|
29
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - ">="
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: 3.4.0
|
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
35
|
+
requirements:
|
|
36
|
+
- - ">="
|
|
37
|
+
- !ruby/object:Gem::Version
|
|
38
|
+
version: '0'
|
|
39
|
+
requirements: []
|
|
40
|
+
rubygems_version: 4.0.5
|
|
41
|
+
specification_version: 4
|
|
42
|
+
summary: A lightweight Ruby event emitter that allows you to bind, emit, and stop
|
|
43
|
+
custom events using blocks.
|
|
44
|
+
test_files: []
|