r_signals 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 +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +11 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/LICENSE.txt +21 -0
- data/README.md +82 -0
- data/Rakefile +12 -0
- data/lib/r_signals/dependency_context/base.rb +65 -0
- data/lib/r_signals/dependency_context/signal.rb +56 -0
- data/lib/r_signals/dependency_context.rb +4 -0
- data/lib/r_signals/event_dispatchers/base.rb +49 -0
- data/lib/r_signals/event_dispatchers/flag_dispatcher.rb +34 -0
- data/lib/r_signals/event_dispatchers.rb +10 -0
- data/lib/r_signals/r_node_.rb +67 -0
- data/lib/r_signals/r_signalable_.rb +61 -0
- data/lib/r_signals/signalizer.rb +60 -0
- data/lib/r_signals/util.rb +10 -0
- data/lib/r_signals/version.rb +5 -0
- data/lib/r_signals.rb +17 -0
- data/sig/r_signals.rbs +4 -0
- metadata +67 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 622ce0ab4dc783351f84fd930367140fb61c7231bc101ac50e39adb256f41b2b
|
4
|
+
data.tar.gz: ebc2063fc0de0e056495af3d167e5b14a6b771032cc8f8e507c69d5a4836d51f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: de670d1c8d0180f7c509f39a77f5e02f4d752794f5146911c044c63e2f43ece63a26e78a986a0149a1fd618bdc96653ff1e531ceb090cdc1f9d5cddc949e72a9
|
7
|
+
data.tar.gz: a4bca09d2737dca53652c4f33d04516ad30c0f27340a32e76654288c84e91041c2e96757c248121ec415ed4e24389721596371c122dec0b584682952bd1aa1e4
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at kingsley@peoplebox.ai. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2024 Unegbu kingsley
|
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,82 @@
|
|
1
|
+
# RSignals
|
2
|
+
|
3
|
+
RSignals is a reactive signal gem for ruby. Create reactivity with your variables.
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Install the gem and add to the application's Gemfile by executing:
|
9
|
+
|
10
|
+
```bash
|
11
|
+
bundle add r_signals
|
12
|
+
```
|
13
|
+
|
14
|
+
## Usage
|
15
|
+
|
16
|
+
### First include the module
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
class Klass
|
20
|
+
include RSignals::Signalizer
|
21
|
+
|
22
|
+
signalize count: 0, shop: -> { count + 2 }
|
23
|
+
end
|
24
|
+
```
|
25
|
+
|
26
|
+
### Then we use it
|
27
|
+
|
28
|
+
```ruby
|
29
|
+
instance = Klass.new
|
30
|
+
|
31
|
+
puts instance.count # 0
|
32
|
+
|
33
|
+
instance.count 2
|
34
|
+
|
35
|
+
puts instance.count # 2
|
36
|
+
|
37
|
+
puts instance.shop # 4
|
38
|
+
|
39
|
+
```
|
40
|
+
|
41
|
+
This instance methods are reactive and cached. from the above example, `shop` depends on `count` but will only call `count` if their is a change to `count`.
|
42
|
+
|
43
|
+
|
44
|
+
### Class based
|
45
|
+
```ruby
|
46
|
+
class Klass
|
47
|
+
include RSignals::Signalizer
|
48
|
+
|
49
|
+
signalize :class, count: 0, shop: -> { count + 2 }
|
50
|
+
end
|
51
|
+
|
52
|
+
|
53
|
+
puts Klass.count # 0
|
54
|
+
|
55
|
+
Klass.count 2
|
56
|
+
|
57
|
+
puts Klass.count # 2
|
58
|
+
|
59
|
+
puts Klass.shop # 4
|
60
|
+
```
|
61
|
+
|
62
|
+
## Development
|
63
|
+
|
64
|
+
N:B Files with name ending with `_` should be ignored. they are currently not part of the repository. They are waiting to be deleted.
|
65
|
+
|
66
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
67
|
+
|
68
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
## Contributing
|
73
|
+
|
74
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/urchmaney/r_signals. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/urchmaney/r_signals/blob/main/CODE_OF_CONDUCT.md).
|
75
|
+
|
76
|
+
## License
|
77
|
+
|
78
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
79
|
+
|
80
|
+
## Code of Conduct
|
81
|
+
|
82
|
+
Everyone interacting in the RSignals project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/urchmaney/r_signals/blob/main/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "set"
|
4
|
+
|
5
|
+
module RSignals
|
6
|
+
module DependencyContext
|
7
|
+
#
|
8
|
+
# Base class for all dependency context to inherit from
|
9
|
+
#
|
10
|
+
class Base
|
11
|
+
@collection_set = Set.new
|
12
|
+
@collection_stack = []
|
13
|
+
|
14
|
+
class << self
|
15
|
+
attr_reader :collection_set, :collection_stack
|
16
|
+
end
|
17
|
+
|
18
|
+
attr_reader :mark_dirty, :dependencies
|
19
|
+
|
20
|
+
def initialize(owner = nil)
|
21
|
+
@owner = owner
|
22
|
+
@dependencies = Set.new
|
23
|
+
@event = EventDispatchers::FlagDispatcher.new
|
24
|
+
@mark_dirty = -> { @event.raise }
|
25
|
+
end
|
26
|
+
|
27
|
+
# protected
|
28
|
+
|
29
|
+
def invoke; end
|
30
|
+
|
31
|
+
def start_collecting
|
32
|
+
if Base.collection_set.include?(self)
|
33
|
+
throw "A circular dependency has occured between signals.
|
34
|
+
This can occur if signal reference each other in a loop."
|
35
|
+
end
|
36
|
+
|
37
|
+
Base.collection_set.add(self)
|
38
|
+
Base.collection_stack.push(self)
|
39
|
+
end
|
40
|
+
|
41
|
+
def finish_collecting
|
42
|
+
Base.collection_set.delete self
|
43
|
+
throw "collectStart/collectEnd was called out of order." if Base.collection_stack.pop != self
|
44
|
+
end
|
45
|
+
|
46
|
+
def clear_dependencies
|
47
|
+
@dependencies.each { |dep| dep.unsubscribe(@mark_dirty) }
|
48
|
+
@dependencies.clear
|
49
|
+
end
|
50
|
+
|
51
|
+
def collect
|
52
|
+
signal = Base.collection_stack[-1]
|
53
|
+
return unless signal
|
54
|
+
|
55
|
+
signal.dependencies.add(@event.subscribable)
|
56
|
+
@event.subscribe(signal.mark_dirty)
|
57
|
+
end
|
58
|
+
|
59
|
+
def dispose
|
60
|
+
clear_dependencies
|
61
|
+
@event.clear
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RSignals
|
4
|
+
module DependencyContext
|
5
|
+
#
|
6
|
+
# Signal Context
|
7
|
+
#
|
8
|
+
class Signal < Base
|
9
|
+
def initialize(owner = nil, initial = nil)
|
10
|
+
super(owner)
|
11
|
+
@initial = initial
|
12
|
+
return unless @initial
|
13
|
+
|
14
|
+
@current = @initial
|
15
|
+
@mark_dirty.call
|
16
|
+
@last = @initial unless RSignals.reactive? @initial
|
17
|
+
end
|
18
|
+
|
19
|
+
def signal(val = nil)
|
20
|
+
return value if val.nil?
|
21
|
+
|
22
|
+
self.value = val
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def value
|
28
|
+
if (RSignals.reactive? @current) && @event.raised?
|
29
|
+
clear_dependencies
|
30
|
+
start_collecting
|
31
|
+
execute_reactive_value
|
32
|
+
finish_collecting
|
33
|
+
end
|
34
|
+
@event.reset
|
35
|
+
collect
|
36
|
+
@last
|
37
|
+
end
|
38
|
+
|
39
|
+
def execute_reactive_value
|
40
|
+
@last = @owner.instance_exec(&@current)
|
41
|
+
rescue StandardError
|
42
|
+
p $ERROR_INFO
|
43
|
+
end
|
44
|
+
|
45
|
+
def value=(val)
|
46
|
+
val = @initial if val == :default
|
47
|
+
|
48
|
+
@current = val
|
49
|
+
clear_dependencies
|
50
|
+
@last = val unless RSignals.reactive? val
|
51
|
+
|
52
|
+
@mark_dirty.call
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RSignals
|
4
|
+
module EventDispatchers
|
5
|
+
# The base abstract class for all event dispatching class
|
6
|
+
class Base
|
7
|
+
attr_reader :subscribable
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@subscribers = Set.new
|
11
|
+
@subscribable = Subscribable.new self
|
12
|
+
end
|
13
|
+
|
14
|
+
def subscribe(handler)
|
15
|
+
@subscribers.add(handler)
|
16
|
+
-> { unsubscribe(handler) }
|
17
|
+
end
|
18
|
+
|
19
|
+
def unsubscribe(handler)
|
20
|
+
@subscribers.delete handler
|
21
|
+
end
|
22
|
+
|
23
|
+
def clear
|
24
|
+
@subscribers.clear
|
25
|
+
end
|
26
|
+
|
27
|
+
def notify_subscribers
|
28
|
+
@subscribers.to_a.map(&:call)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
#
|
33
|
+
# A safe way to subscribe and unsubscribe to events
|
34
|
+
#
|
35
|
+
class Subscribable
|
36
|
+
def initialize(dispatcher)
|
37
|
+
@dispatcher = dispatcher
|
38
|
+
end
|
39
|
+
|
40
|
+
def subscribe(handler)
|
41
|
+
@dispatcher.subscribe(handler)
|
42
|
+
end
|
43
|
+
|
44
|
+
def unsubscribe(handler)
|
45
|
+
@dispatcher.unsubscribe(handler)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RSignals
|
4
|
+
module EventDispatchers
|
5
|
+
# Flag dispatcher that has a boolean value
|
6
|
+
class FlagDispatcher < Base
|
7
|
+
def initialize
|
8
|
+
super
|
9
|
+
@value = false
|
10
|
+
end
|
11
|
+
|
12
|
+
def raise
|
13
|
+
return if @value
|
14
|
+
|
15
|
+
@value = true
|
16
|
+
notify_subscribers
|
17
|
+
end
|
18
|
+
|
19
|
+
def raised?
|
20
|
+
@value
|
21
|
+
end
|
22
|
+
|
23
|
+
def subscribe(handler)
|
24
|
+
unsub = super
|
25
|
+
handler.call if @value
|
26
|
+
unsub
|
27
|
+
end
|
28
|
+
|
29
|
+
def reset
|
30
|
+
@value = false
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# This file is currently not used in the repository. Just waiting to be deleted
|
4
|
+
|
5
|
+
require "set"
|
6
|
+
|
7
|
+
module RSignals
|
8
|
+
# Class definition to create node
|
9
|
+
class RNode
|
10
|
+
attr_reader :previous, :value_type, :dependent_nodes, :binding
|
11
|
+
|
12
|
+
def initialize(val, binding, &block)
|
13
|
+
@value = val
|
14
|
+
@previous = nil
|
15
|
+
@value_type = val.class
|
16
|
+
@block = block
|
17
|
+
@dependent_nodes = Set.new
|
18
|
+
@clean = !val.nil?
|
19
|
+
@binding = binding
|
20
|
+
end
|
21
|
+
|
22
|
+
def value
|
23
|
+
return @value if @block.nil? || clean?
|
24
|
+
|
25
|
+
run_block
|
26
|
+
end
|
27
|
+
|
28
|
+
def value=(new_val)
|
29
|
+
raise TypeMismatchError.new new_val, value_type if new_val.class != value_type
|
30
|
+
|
31
|
+
store_new_value new_val
|
32
|
+
mark_dependents_dirty
|
33
|
+
end
|
34
|
+
|
35
|
+
def r
|
36
|
+
RSignals::ParameterHelpers.calling_node = self
|
37
|
+
RSignals::ParameterHelpers
|
38
|
+
end
|
39
|
+
|
40
|
+
def add_dependent_node(node)
|
41
|
+
@dependent_nodes.add(node)
|
42
|
+
end
|
43
|
+
|
44
|
+
def clean?
|
45
|
+
@clean
|
46
|
+
end
|
47
|
+
|
48
|
+
def mark_dirty
|
49
|
+
@clean = false
|
50
|
+
mark_dependents_dirty
|
51
|
+
end
|
52
|
+
|
53
|
+
def mark_dependents_dirty
|
54
|
+
@dependent_nodes.each(&:mark_dirty)
|
55
|
+
end
|
56
|
+
|
57
|
+
def run_block
|
58
|
+
result = binding.instance_exec r, &@block
|
59
|
+
store_new_value result
|
60
|
+
end
|
61
|
+
|
62
|
+
def store_new_value(new_val)
|
63
|
+
@previous = @value
|
64
|
+
@value = new_val
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# This file is currently not used in the repository. Just waiting to be deleted
|
4
|
+
|
5
|
+
module RSignals
|
6
|
+
# Parameter Helpers
|
7
|
+
# This class contains dynamic methods for signals
|
8
|
+
# This is what we use in
|
9
|
+
# create_r_signal("name") do |r|
|
10
|
+
#
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
# r --> is this class
|
14
|
+
class ParameterHelpers
|
15
|
+
class << self
|
16
|
+
attr_accessor :calling_node
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# Included module to that bring signal behaviour
|
21
|
+
module RSignalable
|
22
|
+
def self.included(base)
|
23
|
+
base.extend ClassMethods
|
24
|
+
end
|
25
|
+
|
26
|
+
# Module class methods
|
27
|
+
module ClassMethods
|
28
|
+
def create_r_signal(name, val = nil, &block)
|
29
|
+
node = RNode.new val, self, &block
|
30
|
+
register_signal name, node
|
31
|
+
create_r_node_method name, node
|
32
|
+
create_r_node_previous_method name, node
|
33
|
+
end
|
34
|
+
|
35
|
+
def create_r_node_method(name, node)
|
36
|
+
define_singleton_method(name) do |*args|
|
37
|
+
return node.value if args.length <= 0
|
38
|
+
|
39
|
+
node.value = args[0]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def create_r_node_previous_method(name, node)
|
44
|
+
define_singleton_method("#{name}_previous") do
|
45
|
+
node.previous
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def register_signal(signal_name, node)
|
50
|
+
RSignals::ParameterHelpers.define_singleton_method(signal_name) do
|
51
|
+
node.add_dependent_node(RSignals::ParameterHelpers.calling_node)
|
52
|
+
node.value
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def full_signal_name(signal_name)
|
57
|
+
"#{name}.#{signal_name}"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RSignals
|
4
|
+
# module to be included to allow signalizing
|
5
|
+
module Signalizer
|
6
|
+
def self.included(base)
|
7
|
+
base.extend ClassMethods
|
8
|
+
base.extend self
|
9
|
+
end
|
10
|
+
|
11
|
+
def signal_instance(instance, key, value)
|
12
|
+
instance_key = "@_signal_#{key}"
|
13
|
+
node = instance.instance_variable_get(instance_key)
|
14
|
+
return node if node
|
15
|
+
|
16
|
+
instance.instance_variable_set(instance_key, RSignals::DependencyContext::Signal.new(self, value))
|
17
|
+
instance.instance_variable_get(instance_key)
|
18
|
+
end
|
19
|
+
|
20
|
+
# module containing class methods that will be added to class that extends Signaler
|
21
|
+
module ClassMethods
|
22
|
+
def signalize(*args)
|
23
|
+
to, hash = verify_args(*args)
|
24
|
+
if to == :class
|
25
|
+
define_signal_singleton_methods hash
|
26
|
+
else
|
27
|
+
define_signal_methods hash
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def verify_args(*args)
|
34
|
+
unless (args.length == 1 && args[0].is_a?(Hash)) || (
|
35
|
+
args.length == 2 && args[0].is_a?(Symbol) && args[1].is_a?(Hash))
|
36
|
+
throw "Wrong parameter structure."
|
37
|
+
end
|
38
|
+
args.length == 2 ? args : [:instance, *args]
|
39
|
+
end
|
40
|
+
|
41
|
+
def define_signal_methods(hash)
|
42
|
+
hash.each do |key, val|
|
43
|
+
define_method key do |value = nil|
|
44
|
+
node = signal_instance(self, key, val)
|
45
|
+
node.signal value
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def define_signal_singleton_methods(hash)
|
51
|
+
hash.each do |key, val|
|
52
|
+
define_singleton_method key do |value = nil|
|
53
|
+
node = signal_instance(self, key, val)
|
54
|
+
node.signal value
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
data/lib/r_signals.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "r_signals/version"
|
4
|
+
require_relative "r_signals/util"
|
5
|
+
require_relative "r_signals/event_dispatchers"
|
6
|
+
require_relative "r_signals/dependency_context"
|
7
|
+
require_relative "r_signals/signalizer"
|
8
|
+
|
9
|
+
# Parent Container Module
|
10
|
+
module RSignals
|
11
|
+
# Type Mismatch exception class
|
12
|
+
class TypeMismatchError < StandardError
|
13
|
+
def initialize(value, type)
|
14
|
+
super("TYPE MISMATCH: type of '#{value}' does not match default type : #{type}")
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
data/sig/r_signals.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: r_signals
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Unegbu Kingsley
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-04-01 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: RSignals is a reactive signal gem for ruby. Create reactivity with your
|
14
|
+
variables.
|
15
|
+
email:
|
16
|
+
- kingsobino@gmail.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- ".rspec"
|
22
|
+
- ".rubocop.yml"
|
23
|
+
- CHANGELOG.md
|
24
|
+
- CODE_OF_CONDUCT.md
|
25
|
+
- LICENSE.txt
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- lib/r_signals.rb
|
29
|
+
- lib/r_signals/dependency_context.rb
|
30
|
+
- lib/r_signals/dependency_context/base.rb
|
31
|
+
- lib/r_signals/dependency_context/signal.rb
|
32
|
+
- lib/r_signals/event_dispatchers.rb
|
33
|
+
- lib/r_signals/event_dispatchers/base.rb
|
34
|
+
- lib/r_signals/event_dispatchers/flag_dispatcher.rb
|
35
|
+
- lib/r_signals/r_node_.rb
|
36
|
+
- lib/r_signals/r_signalable_.rb
|
37
|
+
- lib/r_signals/signalizer.rb
|
38
|
+
- lib/r_signals/util.rb
|
39
|
+
- lib/r_signals/version.rb
|
40
|
+
- sig/r_signals.rbs
|
41
|
+
homepage: https://github.com/Urchmaney/r_signals
|
42
|
+
licenses:
|
43
|
+
- MIT
|
44
|
+
metadata:
|
45
|
+
homepage_uri: https://github.com/Urchmaney/r_signals
|
46
|
+
source_code_uri: https://github.com/Urchmaney/r_signals
|
47
|
+
changelog_uri: https://github.com/Urchmaney/r_signals/blob/main/CHANGELOG.md
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: 3.0.0
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
requirements: []
|
63
|
+
rubygems_version: 3.5.11
|
64
|
+
signing_key:
|
65
|
+
specification_version: 4
|
66
|
+
summary: RSignals is a reactive signal gem for ruby. Create reactivity with your variables.
|
67
|
+
test_files: []
|