jace 0.1.2 → 1.0.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 +4 -4
- data/.circleci/config.yml +7 -8
- data/.claude/agents/architect.md +56 -0
- data/.claude/agents/docs.md +35 -0
- data/.claude/agents/ruby.md +43 -0
- data/.claude/agents/security.md +26 -0
- data/.claude/configuration/arcanum-repo-config.json +9 -0
- data/.claude/configuration/auto-fix-all.json +3 -0
- data/.claude/scripts/check_ruby.sh +7 -0
- data/.claude/settings.json +10 -0
- data/.github/commit_message_template-2.0.md +32 -0
- data/.github/commit_message_template.md +8 -0
- data/.github/copilot-instructions.md +1 -99
- data/.github/pull_request_template.md +17 -0
- data/.gitignore +1 -0
- data/AGENTS.md +140 -0
- data/CLAUDE.md +1 -0
- data/Dockerfile +3 -2
- data/Gemfile +9 -9
- data/README.md +3 -3
- data/docs/agents/arcanum-split-issue.md +9 -0
- data/docs/agents/architecture.md +46 -0
- data/docs/agents/contributing.md +207 -0
- data/docs/agents/flow.md +27 -0
- data/docs/agents/folder-structure.md +60 -0
- data/docs/agents/issue-enhancement.md +10 -0
- data/docs/agents/issues/.gitkeep +1 -0
- data/docs/agents/plans/.gitkeep +1 -0
- data/docs/guides/jace/api-reference.md +20 -0
- data/docs/guides/jace/basic-usage.md +40 -0
- data/docs/guides/jace/core-concepts.md +43 -0
- data/docs/guides/jace/execution-model.md +19 -0
- data/docs/guides/jace/handlers.md +61 -0
- data/docs/guides/jace/installation.md +23 -0
- data/docs/guides/jace/integration-pattern.md +32 -0
- data/docs/guides/jace-usage.md +24 -0
- data/lib/jace/version.rb +1 -1
- data/scripts/bump_version.sh +62 -0
- data/spec/integration/yard/jace/registry_spec.rb +1 -2
- metadata +33 -7
- data/.github/jace-usage.md +0 -241
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# Architecture
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
Jace is a small, dependency-free Ruby gem for in-process, event-driven logic. Consumers
|
|
6
|
+
build a `Jace::Registry`, register `before`/`after` handlers for named events, then
|
|
7
|
+
`trigger` an event around a block. Jace runs the `before` handlers, the block, and the
|
|
8
|
+
`after` handlers in sequence, all evaluated within a caller-supplied context object via
|
|
9
|
+
`instance_eval`. There is no persistence, queueing, or I/O — the whole library is
|
|
10
|
+
in-memory coordination of Procs/Symbols.
|
|
11
|
+
|
|
12
|
+
## Source Code Layout
|
|
13
|
+
|
|
14
|
+
All application source code lives under `lib/`.
|
|
15
|
+
|
|
16
|
+
### `lib/jace.rb`
|
|
17
|
+
|
|
18
|
+
Entry point. Declares the `Jace` module and `autoload`s each class so requiring
|
|
19
|
+
`jace` doesn't eagerly load the whole library.
|
|
20
|
+
|
|
21
|
+
### `lib/jace/registry.rb`
|
|
22
|
+
|
|
23
|
+
`Jace::Registry` — the public API. Maps event names to `Dispatcher` instances.
|
|
24
|
+
`register(event, instant = :after, &block)` adds a handler; `trigger(event, context, &block)`
|
|
25
|
+
looks up (or defaults) the dispatcher for the event and runs it against the block and context.
|
|
26
|
+
|
|
27
|
+
### `lib/jace/dispatcher.rb`
|
|
28
|
+
|
|
29
|
+
`Jace::Dispatcher` — holds the `before` and `after` handler lists for a single event.
|
|
30
|
+
`dispatch(context, &block)` delegates the actual run to `Jace::Executer.call`.
|
|
31
|
+
|
|
32
|
+
### `lib/jace/executer.rb`
|
|
33
|
+
|
|
34
|
+
`Jace::Executer` — runs one event cycle: executes the `before` handlers, calls the
|
|
35
|
+
block, then executes the `after` handlers, returning the block's result. Each raw
|
|
36
|
+
handler entry (`Symbol` or `Proc`) is wrapped into a `Jace::Handler` before being run
|
|
37
|
+
via `context.instance_eval`.
|
|
38
|
+
|
|
39
|
+
### `lib/jace/handler.rb`
|
|
40
|
+
|
|
41
|
+
`Jace::Handler` — wraps a single `Symbol` (method name) or `Proc` into a callable
|
|
42
|
+
(`#call(context)` / `#to_proc`) that evaluates within the given context.
|
|
43
|
+
|
|
44
|
+
### `lib/jace/version.rb`
|
|
45
|
+
|
|
46
|
+
Gem version constant (`Jace::VERSION`).
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
# Contributing
|
|
2
|
+
|
|
3
|
+
## Commit Guidelines
|
|
4
|
+
|
|
5
|
+
- **Atomic and Unitary:** Each commit must represent a single logical change.
|
|
6
|
+
*Example:*
|
|
7
|
+
- Good: `Add #events method to Registry`
|
|
8
|
+
- Bad: `Add #events method and refactor Dispatcher logic`
|
|
9
|
+
- **No Unrelated Changes:** Do not mix unrelated changes in the same commit.
|
|
10
|
+
- **Separate Refactoring:** Whenever possible, separate refactoring commits from new feature or bugfix commits.
|
|
11
|
+
|
|
12
|
+
## Pull Requests
|
|
13
|
+
|
|
14
|
+
- **Descriptive Summary:** Every PR must include a clear and descriptive summary of its purpose and changes.
|
|
15
|
+
- **PR Description Files:** If a description cannot be provided directly in the PR, generate a file with the PR description (e.g., `docs/agents/issues/<pr_number>_description.md`), but do not commit this file.
|
|
16
|
+
|
|
17
|
+
## Definition of Done for PRs
|
|
18
|
+
|
|
19
|
+
A PR is considered complete when:
|
|
20
|
+
|
|
21
|
+
- The stated objective has been achieved.
|
|
22
|
+
- All tests are passing.
|
|
23
|
+
- Rubocop passes without errors.
|
|
24
|
+
- Code coverage is as high as reasonably possible.
|
|
25
|
+
- YARD documentation coverage passes (`bundle exec rake verify_measurements`).
|
|
26
|
+
- Code is not overly complex:
|
|
27
|
+
- Classes and methods should have clear, focused responsibilities.
|
|
28
|
+
- If a class or method is taking on too many responsibilities, refactor to simplify.
|
|
29
|
+
- Methods should be small and do exactly one thing. If a method is growing, extract parts into private helper methods or separate classes.
|
|
30
|
+
- *Example:*
|
|
31
|
+
```ruby
|
|
32
|
+
# Good: each method does one thing
|
|
33
|
+
class Worker
|
|
34
|
+
def run
|
|
35
|
+
fetch_job
|
|
36
|
+
process_job
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
def fetch_job; end
|
|
42
|
+
def process_job; end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# Bad: method does too much
|
|
46
|
+
class Worker
|
|
47
|
+
def run
|
|
48
|
+
fetch_job
|
|
49
|
+
process_job
|
|
50
|
+
send_metrics
|
|
51
|
+
cleanup
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
```
|
|
55
|
+
- This requirement applies primarily to source code. For specs, refactor only if there is excessive duplication.
|
|
56
|
+
|
|
57
|
+
### CI Checks
|
|
58
|
+
|
|
59
|
+
This project is a single Ruby gem (no per-folder path filters in CI) — every change is checked against
|
|
60
|
+
the full suite below, taken from `.circleci/config.yml`:
|
|
61
|
+
|
|
62
|
+
| CircleCI Job | Local command(s) |
|
|
63
|
+
|--------------|-------------------|
|
|
64
|
+
| `test` | `bundle exec rspec` |
|
|
65
|
+
| `checks` | `rubocop` and `bundle exec rake verify_measurements` (YARD doc coverage via yardstick) |
|
|
66
|
+
| `build-and-release` | Not runnable locally — gem build/publish, only runs on tagged `main` after `test` and `checks` pass |
|
|
67
|
+
|
|
68
|
+
The `checks` job also runs `check_readme.sh`, `rubycritic.sh`, and `check_specs` inside the CI Docker
|
|
69
|
+
image (`darthjee/circleci_ruby_331`). These rely on scripts baked into that image and are not directly
|
|
70
|
+
runnable locally; `rubocop` and `rake verify_measurements` are the closest local equivalents, plus
|
|
71
|
+
manually confirming every new class has a corresponding spec (per `config/check_specs.yml`) and the
|
|
72
|
+
README/version docs are in sync.
|
|
73
|
+
|
|
74
|
+
If this project later grows into a multi-package/monorepo layout with path-filtered CI jobs, update this
|
|
75
|
+
table to map each top-level folder to the jobs and commands relevant to it, and include a step in issue
|
|
76
|
+
plans to identify affected folders before opening a PR.
|
|
77
|
+
|
|
78
|
+
## Code Organization
|
|
79
|
+
|
|
80
|
+
### File Responsibility: Class Declarers vs Scripts
|
|
81
|
+
|
|
82
|
+
Every source file (excluding test files) must act as a **class declarer** — it should define one or more
|
|
83
|
+
classes or modules. Files must not act as **scripts** (i.e., they must not execute logic at load time or
|
|
84
|
+
perform side effects directly).
|
|
85
|
+
|
|
86
|
+
This gem currently has no entrypoint scripts — `lib/jace.rb` only declares the `Jace` module and
|
|
87
|
+
`autoload`s its classes, so it does not execute logic at load time either. If an entrypoint script is
|
|
88
|
+
ever introduced (e.g. a CLI under `exe/`), document it here as the sole exception.
|
|
89
|
+
|
|
90
|
+
*Example:*
|
|
91
|
+
```ruby
|
|
92
|
+
# Good: class declarer — defines a class
|
|
93
|
+
class Router
|
|
94
|
+
def register(app); end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
# Bad: script — executes logic at file-load time
|
|
98
|
+
router = Router.new
|
|
99
|
+
router.register(app)
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Test files are exempt from this rule and may execute setup code freely.
|
|
103
|
+
|
|
104
|
+
### File Naming: snake_case Matching the Class Name
|
|
105
|
+
|
|
106
|
+
Files that define a class or module must use **snake_case** naming, matching the class/module name.
|
|
107
|
+
|
|
108
|
+
*Examples:*
|
|
109
|
+
|
|
110
|
+
- `registry.rb` for `class Registry`
|
|
111
|
+
- `dispatcher.rb` for `class Dispatcher`
|
|
112
|
+
- `executer.rb` for `class Executer`
|
|
113
|
+
|
|
114
|
+
This applies to both source files and their corresponding spec files, mirroring `lib/` under `spec/lib/`:
|
|
115
|
+
- `lib/jace/registry.rb` → spec: `spec/lib/jace/registry_spec.rb`
|
|
116
|
+
- `lib/jace/dispatcher.rb` → spec: `spec/lib/jace/dispatcher_spec.rb`
|
|
117
|
+
|
|
118
|
+
Non-class files (e.g. utility modules of module-level methods) use lowercase/snake_case at the author's discretion.
|
|
119
|
+
|
|
120
|
+
### Method Order: Public Before Private
|
|
121
|
+
|
|
122
|
+
Within a class, **public methods must be declared before private methods**. Private methods (after a
|
|
123
|
+
`private` keyword) serve as implementation helpers and should appear at the end of the class body.
|
|
124
|
+
|
|
125
|
+
*Example:*
|
|
126
|
+
```ruby
|
|
127
|
+
# Good: public methods first, private methods last
|
|
128
|
+
class Worker
|
|
129
|
+
def run
|
|
130
|
+
prepare
|
|
131
|
+
execute
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def status; end
|
|
135
|
+
|
|
136
|
+
private
|
|
137
|
+
|
|
138
|
+
def prepare; end
|
|
139
|
+
def execute; end
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# Bad: private methods mixed in with or before public methods
|
|
143
|
+
class Worker
|
|
144
|
+
private
|
|
145
|
+
|
|
146
|
+
def prepare; end
|
|
147
|
+
|
|
148
|
+
public
|
|
149
|
+
|
|
150
|
+
def run; end
|
|
151
|
+
end
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## Dependency Injection
|
|
155
|
+
|
|
156
|
+
Classes must receive their dependencies (data, configuration, collaborators) as constructor arguments. A
|
|
157
|
+
class must never reach out to load files, read environment variables, or fetch configuration on its own.
|
|
158
|
+
|
|
159
|
+
**The entry point (or the calling code) is the only place responsible for loading configuration** (e.g.
|
|
160
|
+
reading a YAML file, parsing options). It then passes the loaded data down to the classes that need it.
|
|
161
|
+
|
|
162
|
+
This makes every class independently testable: tests simply instantiate the class with the data they
|
|
163
|
+
need, without touching the filesystem or environment.
|
|
164
|
+
|
|
165
|
+
*Example:*
|
|
166
|
+
```ruby
|
|
167
|
+
# Good: class receives its context/data as arguments — easy to test
|
|
168
|
+
class Dispatcher
|
|
169
|
+
def initialize(before: [], after: [])
|
|
170
|
+
@before = [before].flatten.compact
|
|
171
|
+
@after = [after].flatten.compact
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
# Bad: class loads its own config — hard to test and couples to the filesystem
|
|
176
|
+
class Dispatcher
|
|
177
|
+
def initialize
|
|
178
|
+
@before = YAML.load_file('./before.yml') # ❌
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
This principle applies to all classes — including handlers and registries. If a class needs data, it
|
|
184
|
+
gets it through its constructor.
|
|
185
|
+
|
|
186
|
+
## Refactoring Guidelines
|
|
187
|
+
|
|
188
|
+
When refactoring, aim to:
|
|
189
|
+
|
|
190
|
+
- **Reduce Code Duplication:**
|
|
191
|
+
*Example:* Move repeated spec setup into a shared context or factory method.
|
|
192
|
+
```ruby
|
|
193
|
+
# Good
|
|
194
|
+
def build_context(name: 'Books')
|
|
195
|
+
SomeContext.new(name: name)
|
|
196
|
+
end
|
|
197
|
+
# In specs:
|
|
198
|
+
let(:context) { build_context(name: 'Games') }
|
|
199
|
+
|
|
200
|
+
# Bad
|
|
201
|
+
let(:context) { SomeContext.new(name: 'Games') }
|
|
202
|
+
# ...repeated setup duplicated across many spec files
|
|
203
|
+
```
|
|
204
|
+
- **Simplify Conditionals:** Prefer early returns / guard clauses over deeply nested `if`/`else`.
|
|
205
|
+
- **Extract Collaborators:** When a class starts coordinating too many concerns, extract a new
|
|
206
|
+
class instead of growing the existing one (see `Jace::Registry` → `Jace::Dispatcher` → `Jace::Executer`
|
|
207
|
+
→ `Jace::Handler` as an example of this decomposition).
|
data/docs/agents/flow.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# Flow
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
Jace runs one in-process cycle per triggered event: `before` handlers, then the main
|
|
6
|
+
block, then `after` handlers. There is no queueing or I/O — everything happens
|
|
7
|
+
synchronously in the caller's thread.
|
|
8
|
+
|
|
9
|
+
## Registration
|
|
10
|
+
|
|
11
|
+
1. `Registry#register(event, instant = :after, &block)` normalizes `event` to a Symbol
|
|
12
|
+
and finds or creates the event's `Dispatcher`.
|
|
13
|
+
2. The block is appended to that dispatcher's `before` or `after` list (chosen by
|
|
14
|
+
`instant`).
|
|
15
|
+
|
|
16
|
+
## Trigger
|
|
17
|
+
|
|
18
|
+
1. `Registry#trigger(event, context, &block)` looks up the event's dispatcher, falling
|
|
19
|
+
back to an empty `Dispatcher` when nothing was registered for it.
|
|
20
|
+
2. `Dispatcher#dispatch(context, &block)` calls
|
|
21
|
+
`Executer.call(before:, after:, context:, &block)`.
|
|
22
|
+
3. `Executer#call`:
|
|
23
|
+
- wraps each `before` entry in a `Jace::Handler` and runs it via
|
|
24
|
+
`context.instance_eval`;
|
|
25
|
+
- calls the main block (if given) and keeps its result;
|
|
26
|
+
- wraps and runs each `after` entry the same way.
|
|
27
|
+
4. The main block's return value is returned to the caller of `trigger`.
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# Folder Structure
|
|
2
|
+
|
|
3
|
+
## Project Root
|
|
4
|
+
|
|
5
|
+
| Directory / File | Description |
|
|
6
|
+
|-----------------|-------------|
|
|
7
|
+
| `lib/` | Gem source code — `Jace::Registry`, `Jace::Dispatcher`, `Jace::Executer`, `Jace::Handler`, and version info. |
|
|
8
|
+
| `spec/` | RSpec test suite — unit specs (mirroring `lib/`) and integration specs. |
|
|
9
|
+
| `config/` | Tooling config: `check_specs.yml` (spec-coverage exclusions), `yardstick.rb`/`yardstick.yml` (YARD doc-coverage), `rubycritc.rb`. |
|
|
10
|
+
| `docs/` | Project documentation, including the `agents/` folder consumed by AI coding agents, and `guides/` (usage guides, e.g. `jace-usage.md`). |
|
|
11
|
+
| `.github/` | GitHub-specific files: Copilot instructions and PR/commit templates. |
|
|
12
|
+
| `.circleci/` | CircleCI pipeline configuration (`config.yml`). |
|
|
13
|
+
| `AGENTS.md` | Shared instructions for AI coding agents (Claude, Copilot). |
|
|
14
|
+
| `CLAUDE.md` | Pointer to `AGENTS.md`. |
|
|
15
|
+
| `README.md` | Project overview, installation, and usage examples. |
|
|
16
|
+
| `jace.gemspec` | Gem specification. |
|
|
17
|
+
| `Gemfile` / `Gemfile.lock` | Bundler dependency management. |
|
|
18
|
+
| `Rakefile` | Rake tasks (tests, docs, etc.). |
|
|
19
|
+
| `Makefile` | Developer convenience commands. |
|
|
20
|
+
| `Dockerfile` / `docker-compose.yml` | Containerized dev/test environment. |
|
|
21
|
+
| `.rubocop.yml` / `.rubocop_todo.yml` | RuboCop style-check configuration. |
|
|
22
|
+
| `jace.png` | Project logo, used in `README.md`. |
|
|
23
|
+
| `LICENSE` | Project license. |
|
|
24
|
+
|
|
25
|
+
## lib/jace (gem source)
|
|
26
|
+
|
|
27
|
+
| File | Description |
|
|
28
|
+
|------|--------------|
|
|
29
|
+
| `registry.rb` | `Jace::Registry` — stores event-to-handler mappings; exposes `register` and `trigger`. |
|
|
30
|
+
| `dispatcher.rb` | `Jace::Dispatcher` — holds `before`/`after` handler lists for one event, delegates execution to `Executer`. |
|
|
31
|
+
| `executer.rb` | `Jace::Executer` — runs before handlers, the main block, then after handlers within a context. |
|
|
32
|
+
| `handler.rb` | `Jace::Handler` — wraps a `Symbol` or `Proc` into a callable evaluated via `instance_eval`. |
|
|
33
|
+
| `version.rb` | Gem version constant. |
|
|
34
|
+
|
|
35
|
+
## docs/agents
|
|
36
|
+
|
|
37
|
+
| Subdirectory / File | Description |
|
|
38
|
+
|----------------------|-------------|
|
|
39
|
+
| `architecture.md` | System architecture overview for agents. |
|
|
40
|
+
| `flow.md` | Key workflows/data flow through the system. |
|
|
41
|
+
| `folder-structure.md` | This file — top-level directory layout. |
|
|
42
|
+
| `issue-enhancement.md` | Guidance for enhancing/refining issues. |
|
|
43
|
+
| `arcanum-split-issue.md` | Concerns to weigh when splitting a broad issue into sub-issues. |
|
|
44
|
+
| `issues/` | Structured issue files consumed by the planning/fixing pipeline. |
|
|
45
|
+
| `plans/` | Implementation plans generated for issues. |
|
|
46
|
+
|
|
47
|
+
## docs/guides
|
|
48
|
+
|
|
49
|
+
| Subdirectory / File | Description |
|
|
50
|
+
|----------------------|-------------|
|
|
51
|
+
| `jace-usage.md` | Hub page for using the Jace gem; links out to the topic pages below. |
|
|
52
|
+
| `jace/` | Topic pages split out of `jace-usage.md` (installation, core concepts, basic usage, handlers, integration pattern, execution model, API reference). |
|
|
53
|
+
|
|
54
|
+
## spec
|
|
55
|
+
|
|
56
|
+
| Subdirectory | Description |
|
|
57
|
+
|---------------|-------------|
|
|
58
|
+
| `lib/` | Unit specs mirroring `lib/jace/*.rb`. |
|
|
59
|
+
| `integration/` | End-to-end specs: `readme/` (validates README examples) and `yard/` (validates YARD `@example` code). |
|
|
60
|
+
| `support/` | Shared spec helpers: `models/` (test fixtures/contexts) and `shared_examples/`. |
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# Issue Enhancement
|
|
2
|
+
|
|
3
|
+
A checklist of concerns to consider when fleshing out a vague issue idea (tagged `Idea`/`Writting`) before it reaches the `Created` stage. Not exhaustive — adjust or extend the list for this project's needs.
|
|
4
|
+
|
|
5
|
+
- **Scope boundaries** — what's explicitly in scope and what's explicitly out.
|
|
6
|
+
- **Alternative solutions** — other ways to solve the same problem, and why this one was chosen.
|
|
7
|
+
- **Edge cases** — inputs, states, or timing the happy path doesn't cover.
|
|
8
|
+
- **Backward compatibility** — whether this breaks existing behavior, data, or integrations.
|
|
9
|
+
- **Testing strategy** — how the change will be verified.
|
|
10
|
+
- **Performance & security considerations** — anything relevant to load, latency, or attack surface.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# API Reference
|
|
2
|
+
|
|
3
|
+
| Method | Signature | Description |
|
|
4
|
+
|--------|-----------|-------------|
|
|
5
|
+
| `Registry#register` | `(event, instant = :after, &block)` | Adds a handler to the named event |
|
|
6
|
+
| `Registry#trigger` | `(event, context, &block)` | Fires the event, running handlers around the block |
|
|
7
|
+
| `Registry#events` | `()` | Returns all registered event names as `Array<Symbol>` |
|
|
8
|
+
| `Registry#registry` | `()` | Returns the raw `Hash` of event → `Dispatcher` mappings |
|
|
9
|
+
|
|
10
|
+
---
|
|
11
|
+
|
|
12
|
+
## YARD Documentation
|
|
13
|
+
|
|
14
|
+
Full API docs: [https://www.rubydoc.info/gems/jace](https://www.rubydoc.info/gems/jace)
|
|
15
|
+
|
|
16
|
+
Source: [https://github.com/darthjee/jace](https://github.com/darthjee/jace)
|
|
17
|
+
|
|
18
|
+
---
|
|
19
|
+
|
|
20
|
+
[Back to hub](../jace-usage.md)
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
# Basic Usage
|
|
2
|
+
|
|
3
|
+
```ruby
|
|
4
|
+
class Order
|
|
5
|
+
attr_reader :log
|
|
6
|
+
|
|
7
|
+
def initialize
|
|
8
|
+
@log = []
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def validate
|
|
12
|
+
log << 'validated'
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def persist
|
|
16
|
+
log << 'persisted'
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def notify
|
|
20
|
+
log << 'notified'
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
registry = Jace::Registry.new
|
|
25
|
+
order = Order.new
|
|
26
|
+
|
|
27
|
+
registry.register(:save, :before) { validate }
|
|
28
|
+
registry.register(:save) { notify }
|
|
29
|
+
|
|
30
|
+
registry.trigger(:save, order) do
|
|
31
|
+
order.persist # main block uses the surrounding scope, so explicit receiver is needed
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
order.log
|
|
35
|
+
# => ['validated', 'persisted', 'notified']
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
[Back to hub](../jace-usage.md)
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# Core Concepts
|
|
2
|
+
|
|
3
|
+
### `Jace::Registry`
|
|
4
|
+
|
|
5
|
+
The central object. It stores event-to-handler mappings and exposes two public
|
|
6
|
+
methods: `register` and `trigger`.
|
|
7
|
+
|
|
8
|
+
```ruby
|
|
9
|
+
registry = Jace::Registry.new
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
### `register(event, instant = :after, &block)`
|
|
13
|
+
|
|
14
|
+
Adds a handler block for a named event. The `instant` parameter controls whether
|
|
15
|
+
the block runs before or after the main event block.
|
|
16
|
+
|
|
17
|
+
| `instant` | When the handler runs |
|
|
18
|
+
|-----------|----------------------|
|
|
19
|
+
| `:after` (default) | After the main block |
|
|
20
|
+
| `:before` | Before the main block |
|
|
21
|
+
|
|
22
|
+
```ruby
|
|
23
|
+
registry.register(:payment_processed) { send_receipt }
|
|
24
|
+
registry.register(:payment_processed, :before) { validate_payment }
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### `trigger(event, context, &block)`
|
|
28
|
+
|
|
29
|
+
Fires the named event. Jace runs the `before` handlers, then the given block,
|
|
30
|
+
then the `after` handlers. The `before` and `after` handlers are `instance_eval`'d
|
|
31
|
+
inside `context`, so bare method calls in handlers resolve against the context.
|
|
32
|
+
The main block is called normally (not `instance_eval`'d), so it uses the
|
|
33
|
+
surrounding scope's receiver.
|
|
34
|
+
|
|
35
|
+
```ruby
|
|
36
|
+
registry.trigger(:payment_processed, payment_object) do
|
|
37
|
+
charge_credit_card
|
|
38
|
+
end
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
[Back to hub](../jace-usage.md)
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# Execution Model (summary)
|
|
2
|
+
|
|
3
|
+
```
|
|
4
|
+
registry.trigger(:event, context) { main_block }
|
|
5
|
+
|
|
6
|
+
Execution order:
|
|
7
|
+
1. All :before handlers (in registration order, instance_eval'd in context)
|
|
8
|
+
2. main_block (called as-is, context is NOT the receiver)
|
|
9
|
+
3. All :after handlers (in registration order, instance_eval'd in context)
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
> **Note:** The `before` and `after` handlers are `instance_eval`'d inside the
|
|
13
|
+
> context object, so bare method calls inside them (`send_receipt`, `validate`,
|
|
14
|
+
> etc.) resolve against the context. The main block, however, is a regular
|
|
15
|
+
> `call` (not `instance_eval`), so its receiver is the surrounding scope.
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
[Back to hub](../jace-usage.md)
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# Handlers
|
|
2
|
+
|
|
3
|
+
## Handler Types
|
|
4
|
+
|
|
5
|
+
Handlers are registered as **blocks** (procs) and are `instance_eval`'d inside
|
|
6
|
+
the context object when the event fires, so bare method calls resolve against
|
|
7
|
+
the context.
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
registry.register(:shipment_sent) { send_confirmation_email }
|
|
11
|
+
registry.register(:shipment_sent, :before) { freeze_order }
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## Multiple Handlers per Event
|
|
17
|
+
|
|
18
|
+
You can register as many `before` and `after` handlers as you like for the same
|
|
19
|
+
event. They execute in registration order.
|
|
20
|
+
|
|
21
|
+
```ruby
|
|
22
|
+
registry.register(:user_created, :before) { sanitize_input }
|
|
23
|
+
registry.register(:user_created, :before) { check_duplicates }
|
|
24
|
+
registry.register(:user_created) { send_welcome_email }
|
|
25
|
+
registry.register(:user_created) { notify_admin }
|
|
26
|
+
|
|
27
|
+
registry.trigger(:user_created, user_context) do
|
|
28
|
+
persist_user
|
|
29
|
+
end
|
|
30
|
+
# Order: sanitize_input → check_duplicates → persist_user
|
|
31
|
+
# → send_welcome_email → notify_admin
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## Triggering Events Without a Main Block
|
|
37
|
+
|
|
38
|
+
The main block is optional. If omitted, only the registered handlers run:
|
|
39
|
+
|
|
40
|
+
```ruby
|
|
41
|
+
registry.trigger(:cache_invalidated, cache_context)
|
|
42
|
+
# Runs all :before handlers, then all :after handlers; no main block
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
---
|
|
46
|
+
|
|
47
|
+
## Triggering an Unregistered Event
|
|
48
|
+
|
|
49
|
+
Triggering an event that has no registered handlers is safe. The main block is
|
|
50
|
+
still executed, and no error is raised:
|
|
51
|
+
|
|
52
|
+
```ruby
|
|
53
|
+
registry.trigger(:unknown_event, some_context) do
|
|
54
|
+
do_work
|
|
55
|
+
end
|
|
56
|
+
# do_work runs normally; no handlers fire
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
---
|
|
60
|
+
|
|
61
|
+
[Back to hub](../jace-usage.md)
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Installation
|
|
2
|
+
|
|
3
|
+
Add `jace` to your `Gemfile`:
|
|
4
|
+
|
|
5
|
+
```ruby
|
|
6
|
+
gem 'jace'
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Then run:
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
bundle install
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
Or install it directly:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
gem install jace
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
[Back to hub](../jace-usage.md)
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# Typical Integration Pattern
|
|
2
|
+
|
|
3
|
+
The most common pattern is to hold a `Jace::Registry` instance inside a service
|
|
4
|
+
or module and expose `register` to callers so they can hook into lifecycle events:
|
|
5
|
+
|
|
6
|
+
```ruby
|
|
7
|
+
module PaymentService
|
|
8
|
+
REGISTRY = Jace::Registry.new
|
|
9
|
+
|
|
10
|
+
def self.on(event, instant = :after, &block)
|
|
11
|
+
REGISTRY.register(event, instant, &block)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def self.process(payment)
|
|
15
|
+
REGISTRY.trigger(:payment_processed, payment) do
|
|
16
|
+
payment.charge!
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# In an initializer or plugin:
|
|
22
|
+
PaymentService.on(:payment_processed) { send_receipt }
|
|
23
|
+
PaymentService.on(:payment_processed, :before) { log_attempt }
|
|
24
|
+
|
|
25
|
+
# Elsewhere in the application:
|
|
26
|
+
PaymentService.process(payment)
|
|
27
|
+
# Runs: log_attempt → payment.charge! → send_receipt
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
---
|
|
31
|
+
|
|
32
|
+
[Back to hub](../jace-usage.md)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Using the Jace Gem
|
|
2
|
+
|
|
3
|
+
## What is Jace?
|
|
4
|
+
|
|
5
|
+
**Jace** is a Ruby gem for event-driven development **within a single application**.
|
|
6
|
+
It is not about distributed architecture or message queues — it is about building
|
|
7
|
+
internal event-oriented logic inside a Ruby gem or application.
|
|
8
|
+
|
|
9
|
+
With Jace, you register handlers for named events and trigger those events from
|
|
10
|
+
anywhere in your codebase. When an event is triggered, Jace executes the registered
|
|
11
|
+
`before` handlers (inside the context via `instance_eval`), then the main block,
|
|
12
|
+
then the `after` handlers (also inside the context via `instance_eval`).
|
|
13
|
+
|
|
14
|
+
---
|
|
15
|
+
|
|
16
|
+
## Guide
|
|
17
|
+
|
|
18
|
+
- [Installation](jace/installation.md)
|
|
19
|
+
- [Core Concepts](jace/core-concepts.md) — `Jace::Registry`, `register`, `trigger`
|
|
20
|
+
- [Basic Usage](jace/basic-usage.md)
|
|
21
|
+
- [Handlers](jace/handlers.md) — handler types, multiple handlers per event, edge cases
|
|
22
|
+
- [Typical Integration Pattern](jace/integration-pattern.md)
|
|
23
|
+
- [Execution Model](jace/execution-model.md)
|
|
24
|
+
- [API Reference](jace/api-reference.md)
|
data/lib/jace/version.rb
CHANGED