zyra 1.2.0 → 1.4.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 +21 -13
- data/.claude/agents/architect.md +44 -0
- data/.claude/agents/ruby.md +42 -0
- data/.claude/configuration/arcanum-repo-config.json +3 -0
- data/.claude/scripts/check_ruby.sh +8 -0
- data/.claude/settings.json +10 -0
- data/.github/commit_message_template-2.0.md +32 -0
- data/.github/commit_message_template.md +10 -0
- data/.github/copilot-instructions.md +1 -0
- data/.github/pull_request_template.md +17 -0
- data/.github/zyra-usage.md +289 -0
- data/.gitignore +1 -0
- data/.rubocop.yml +2 -2
- data/.rubocop_todo.yml +2 -1
- data/AGENTS.md +121 -0
- data/CLAUDE.md +1 -0
- data/Dockerfile +2 -2
- data/Gemfile +18 -16
- data/Makefile +21 -0
- data/README.md +6 -7
- data/docs/agents/arcanum-split-issue.md +9 -0
- data/docs/agents/architecture.md +31 -0
- data/docs/agents/contributing.md +141 -0
- data/docs/agents/flow.md +5 -0
- data/docs/agents/folder-structure.md +33 -0
- data/docs/agents/issue-enhancement.md +10 -0
- data/docs/agents/issues/.gitkeep +1 -0
- data/docs/agents/plans/.gitkeep +1 -0
- data/lib/zyra/finder.rb +1 -3
- data/lib/zyra/finder_creator.rb +2 -2
- data/lib/zyra/registry.rb +4 -4
- data/lib/zyra/version.rb +1 -1
- data/scripts/bump_version.sh +62 -0
- data/spec/spec_helper.rb +7 -1
- data/zyra.gemspec +3 -3
- data/zyra.jpg +0 -0
- metadata +32 -13
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 773e9606d1f02c267ff707abca9b0661bd672c5ec19f1b67333b6a432cf9e956
|
|
4
|
+
data.tar.gz: fdf4728a97d11d49d8e99b11fe34238fed6a79ff0b888a01be389abb3f4a2d44
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1a7d6860adefa6f16fa766b9afa23b9336de6b3e0677cd569a243fde254b9594a5618d276424b74ab34249e9038a9cddfe38f3b6aa492f40a0a7b585361dda5b
|
|
7
|
+
data.tar.gz: afe60d21cb865849333e107d6480d83d23bba337a2773f7dc23874b175b19290f6ec7b457b69790c3ede7f5fcce60a746e783d6c46a3e01b63f0a9afa7ab0992
|
data/.circleci/config.yml
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
version: 2
|
|
1
|
+
version: 2.1
|
|
2
2
|
workflows:
|
|
3
|
-
version: 2
|
|
4
3
|
test-and-build:
|
|
5
4
|
jobs:
|
|
6
5
|
- test:
|
|
@@ -11,37 +10,46 @@ workflows:
|
|
|
11
10
|
filters:
|
|
12
11
|
tags:
|
|
13
12
|
only: /.*/
|
|
13
|
+
- coverage-final:
|
|
14
|
+
requires: [test]
|
|
15
|
+
filters:
|
|
16
|
+
tags:
|
|
17
|
+
only: /.*/
|
|
14
18
|
- build-and-release:
|
|
15
|
-
requires: [test, checks]
|
|
19
|
+
requires: [test, checks, coverage-final]
|
|
16
20
|
filters:
|
|
17
21
|
tags:
|
|
18
22
|
only: /\d+\.\d+\.\d+/
|
|
19
23
|
branches:
|
|
20
24
|
only:
|
|
21
|
-
-
|
|
25
|
+
- main
|
|
22
26
|
jobs:
|
|
23
27
|
test:
|
|
24
28
|
docker:
|
|
25
|
-
- image: darthjee/
|
|
29
|
+
- image: darthjee/circleci_ruby_3412:1.0.0
|
|
26
30
|
environment:
|
|
27
31
|
PROJECT: zyra
|
|
28
32
|
steps:
|
|
29
33
|
- checkout
|
|
30
|
-
- run:
|
|
31
|
-
name: Prepare Coverage Test Report
|
|
32
|
-
command: cc-test-reporter before-build
|
|
33
34
|
- run:
|
|
34
35
|
name: Bundle Install
|
|
35
36
|
command: bundle install
|
|
36
37
|
- run:
|
|
37
38
|
name: RSpec
|
|
38
|
-
command: bundle exec rspec
|
|
39
|
+
command: CI=true bundle exec rspec
|
|
40
|
+
- run:
|
|
41
|
+
name: Upload coverage to Codacy (partial)
|
|
42
|
+
command: bash <(curl -Ls https://coverage.codacy.com/get.sh) report --partial -r coverage/lcov/project.lcov
|
|
43
|
+
coverage-final:
|
|
44
|
+
docker:
|
|
45
|
+
- image: darthjee/circleci_ruby_3412:1.0.0
|
|
46
|
+
steps:
|
|
39
47
|
- run:
|
|
40
|
-
name:
|
|
41
|
-
command:
|
|
48
|
+
name: Finalize coverage on Codacy
|
|
49
|
+
command: bash <(curl -Ls https://coverage.codacy.com/get.sh) final
|
|
42
50
|
checks:
|
|
43
51
|
docker:
|
|
44
|
-
- image: darthjee/
|
|
52
|
+
- image: darthjee/circleci_ruby_3412:1.0.0
|
|
45
53
|
environment:
|
|
46
54
|
PROJECT: zyra
|
|
47
55
|
steps:
|
|
@@ -66,7 +74,7 @@ jobs:
|
|
|
66
74
|
command: check_specs
|
|
67
75
|
build-and-release:
|
|
68
76
|
docker:
|
|
69
|
-
- image: darthjee/
|
|
77
|
+
- image: darthjee/circleci_ruby_3412:1.0.0
|
|
70
78
|
environment:
|
|
71
79
|
PROJECT: zyra
|
|
72
80
|
steps:
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: architect
|
|
3
|
+
description: Zyra architect and coordinator. Use for cross-cutting tasks, multi-agent coordination, documentation, root-level files, or any task that spans more than one agent's scope.
|
|
4
|
+
tools: Read, Edit, Write, Bash, Agent
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
You are the architect and coordinator for the zyra project — a Ruby gem for idempotent seeding.
|
|
8
|
+
|
|
9
|
+
## Your scope
|
|
10
|
+
|
|
11
|
+
- `docs/agents/` — all project documentation
|
|
12
|
+
- Root-level files: `README.md`, `AGENTS.md`, `CLAUDE.md`, `Gemfile`, `zyra.gemspec`, `Rakefile`, `Makefile`, Docker files
|
|
13
|
+
- `.circleci/` and `.github/`
|
|
14
|
+
- Cross-cutting decisions and coordination of specialist agents
|
|
15
|
+
|
|
16
|
+
Everything that runs code must go through docker-compose (see the `ruby` agent for commands).
|
|
17
|
+
|
|
18
|
+
## Specialist agents
|
|
19
|
+
|
|
20
|
+
Delegate implementation, exploration, and planning work to the right agent. Never implement, explore, or plan what belongs to a specialist yourself.
|
|
21
|
+
|
|
22
|
+
| Agent | Scope |
|
|
23
|
+
|-------|-------|
|
|
24
|
+
| `ruby` | `lib/`, `spec/`, `config/` — gem source, specs and tooling config |
|
|
25
|
+
|
|
26
|
+
## How to coordinate
|
|
27
|
+
|
|
28
|
+
1. **Break it down** — identify which parts belong to which agent.
|
|
29
|
+
2. **Delegate exploration first** — dispatch the specialist before proposing an approach.
|
|
30
|
+
3. **Sequence or parallelize** — parallel if independent, sequential otherwise.
|
|
31
|
+
4. **Integrate** — verify cross-cutting concerns afterwards.
|
|
32
|
+
5. **Update docs** — reflect architectural changes in `docs/agents/`.
|
|
33
|
+
|
|
34
|
+
## Documentation (`docs/agents/`)
|
|
35
|
+
|
|
36
|
+
| File | Contents |
|
|
37
|
+
|------|----------|
|
|
38
|
+
| folder-structure.md | Top-level directory layout |
|
|
39
|
+
| architecture.md | Source layout and guidelines |
|
|
40
|
+
| contributing.md | Commit, PR and code organization rules |
|
|
41
|
+
| flow.md | Main runtime flow |
|
|
42
|
+
| plans/, issues/ | Plans and issue specs |
|
|
43
|
+
|
|
44
|
+
Keep documentation up to date after any architectural change. When an agent is added or its scope changes, update this file and `AGENTS.md`.
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: ruby
|
|
3
|
+
description: Zyra Ruby specialist. Use for any task involving Ruby code, RSpec specs, YARD docs, RuboCop, or the gem's tooling config in lib/, spec/ and config/.
|
|
4
|
+
tools: Read, Edit, Write, Bash
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
You are the ruby specialist for the zyra project — a Ruby gem for idempotent seeding (find-or-create by lookup keys).
|
|
8
|
+
|
|
9
|
+
## Your scope
|
|
10
|
+
|
|
11
|
+
You own everything inside:
|
|
12
|
+
|
|
13
|
+
- `lib/` — gem source (`zyra.rb` entry point, `zyra/` namespace)
|
|
14
|
+
- `spec/` — RSpec unit and integration specs, factories and support
|
|
15
|
+
- `config/` — check_specs, yardstick and rubycritic config
|
|
16
|
+
|
|
17
|
+
Do NOT touch `docs/agents/`, `.circleci/`, `.github/`, Docker files or root-level docs; those belong to the architect.
|
|
18
|
+
|
|
19
|
+
## Stack
|
|
20
|
+
|
|
21
|
+
- Ruby 3.3, RSpec, FactoryBot, RuboCop (+ rubocop-rspec), YARD + yardstick, Rubycritic, Jace (events), ActiveSupport
|
|
22
|
+
|
|
23
|
+
## Commands
|
|
24
|
+
|
|
25
|
+
**Everything must be run through docker-compose** — never run ruby, bundle, rspec or rubocop directly on the host.
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
docker-compose run --rm zyra /bin/bash -c 'bundle exec rspec'
|
|
29
|
+
docker-compose run --rm zyra /bin/bash -c 'rubocop'
|
|
30
|
+
docker-compose run --rm zyra /bin/bash -c 'bundle exec rake verify_measurements'
|
|
31
|
+
docker-compose run --rm circleci /bin/bash -c 'check_specs'
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Run `docker-compose run --rm zyra /bin/bash` for an interactive shell. The `circleci` service mirrors the CI image (provides `check_specs`, `check_readme.sh`, `rubycritic.sh`).
|
|
35
|
+
|
|
36
|
+
## Conventions
|
|
37
|
+
|
|
38
|
+
- Always add specs for new/changed behaviour; files without tests go in `config/check_specs.yml` with a justification.
|
|
39
|
+
- Public APIs need YARD docstrings (`@param`, `@return`, `@example`).
|
|
40
|
+
- Small single-responsibility classes, composition over inheritance, Law of Demeter.
|
|
41
|
+
- snake_case files mirroring class names; public methods before private; dependencies injected via constructor.
|
|
42
|
+
- All code, commits and docs in English.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
set -euo pipefail
|
|
3
|
+
set -x
|
|
4
|
+
|
|
5
|
+
docker-compose run --rm zyra /bin/bash -c 'bundle exec rspec'
|
|
6
|
+
docker-compose run --rm zyra /bin/bash -c 'rubocop'
|
|
7
|
+
docker-compose run --rm zyra /bin/bash -c 'bundle exec rake verify_measurements'
|
|
8
|
+
docker-compose run --rm circleci /bin/bash -c 'check_specs'
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
```text
|
|
2
|
+
<type>(<scope>): <subject> (issue #<id>)
|
|
3
|
+
|
|
4
|
+
<optional body: what was done and why, if not obvious>
|
|
5
|
+
|
|
6
|
+
Addresses-Comment: <optional: URL of the PR comment this commit addresses>
|
|
7
|
+
|
|
8
|
+
Co-Authored-By: <AI model name> <AI model email>
|
|
9
|
+
Co-Authored-By: <agent> agent <agent email>
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
Note: unlike the model line above (always the running AI model's own
|
|
13
|
+
canonical noreply address), the agent line's email is independently
|
|
14
|
+
configurable per agent via the "git.email" key (a template such as
|
|
15
|
+
"you+{agent}@example.com", with "{agent}" substituted for the actual
|
|
16
|
+
agent name, e.g. "architect" or "backend") in
|
|
17
|
+
.claude/state/arcanum-config.json (local) or
|
|
18
|
+
.claude/configuration/arcanum-repo-config.json (repo) — see
|
|
19
|
+
docs/guides/arcanum-repo-config.md. When "git.email" is unset, the agent
|
|
20
|
+
line falls back to the same address as the model line, reproducing the
|
|
21
|
+
old, single-shared-email template's output.
|
|
22
|
+
|
|
23
|
+
The model line above can be omitted entirely by setting the
|
|
24
|
+
"git.omit_model_coauthor" key to "true" in the same three locations
|
|
25
|
+
(local state, repo config, or global config — see
|
|
26
|
+
docs/guides/arcanum-repo-config.md) — the agent line is always still
|
|
27
|
+
emitted regardless of this setting.
|
|
28
|
+
|
|
29
|
+
This file's mere presence (as "commit_message_template-2.0.md", not its
|
|
30
|
+
content) is what switches the commit scripts onto this new, two-distinct-
|
|
31
|
+
email behavior — the content itself is never parsed at runtime, it is
|
|
32
|
+
purely human-facing documentation of the shape above.
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
```text
|
|
2
|
+
<type>(<scope>): <subject> (issue #<id>)
|
|
3
|
+
|
|
4
|
+
<optional body: what was done and why, if not obvious>
|
|
5
|
+
|
|
6
|
+
Addresses-Comment: <optional: URL of the PR comment this commit addresses>
|
|
7
|
+
|
|
8
|
+
Co-Authored-By: <AI model name> <AI model email>
|
|
9
|
+
Co-Authored-By: <agent> agent <AI model email>
|
|
10
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
See [AGENTS.md](../AGENTS.md) for project instructions.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
## Summary
|
|
2
|
+
|
|
3
|
+
<!-- One sentence describing what this PR does -->
|
|
4
|
+
|
|
5
|
+
## Problem
|
|
6
|
+
|
|
7
|
+
<!-- What problem does this solve? Why is this change needed? -->
|
|
8
|
+
|
|
9
|
+
## Solution
|
|
10
|
+
|
|
11
|
+
<!-- How was the problem solved? What approach was taken? -->
|
|
12
|
+
|
|
13
|
+
## Details
|
|
14
|
+
|
|
15
|
+
<!-- Optional: implementation notes, migration steps, caveats. Remove this section if not needed. -->
|
|
16
|
+
|
|
17
|
+
Fixes #
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
# `zyra` Gem — Integration Reference
|
|
2
|
+
|
|
3
|
+
> **Audience:** this document is intended for developers and GitHub Copilot in
|
|
4
|
+
> repositories that use (or plan to use) the `zyra` gem. It describes how to
|
|
5
|
+
> integrate and use the gem without having to inspect the original source code.
|
|
6
|
+
|
|
7
|
+
## What is `zyra`?
|
|
8
|
+
|
|
9
|
+
`zyra` is a Ruby gem for **idempotent database seeding** in Rails projects. It
|
|
10
|
+
ensures that certain entities exist in the database without duplicating them on
|
|
11
|
+
every `rake db:seed` run.
|
|
12
|
+
|
|
13
|
+
The basic flow is:
|
|
14
|
+
|
|
15
|
+
1. You **register** a model and define which attributes serve as lookup keys.
|
|
16
|
+
2. When `find_or_create` is called, the gem searches for the record by those keys.
|
|
17
|
+
3. If the record **does not exist**, it is created with all provided attributes.
|
|
18
|
+
4. If the record **already exists**, it is simply returned (no duplication).
|
|
19
|
+
5. Optional hooks allow extra logic to be executed at each step of the process.
|
|
20
|
+
|
|
21
|
+
---
|
|
22
|
+
|
|
23
|
+
## Installation
|
|
24
|
+
|
|
25
|
+
### Via `Gemfile` (recommended for Rails applications)
|
|
26
|
+
|
|
27
|
+
Add to your `Gemfile`:
|
|
28
|
+
|
|
29
|
+
```ruby
|
|
30
|
+
gem 'zyra'
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Then install the dependencies:
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
bundle install
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Via `gemspec` (for gems that depend on `zyra`)
|
|
40
|
+
|
|
41
|
+
```ruby
|
|
42
|
+
spec.add_dependency 'zyra', '>= 1.3.0'
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Direct installation
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
gem install zyra
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
---
|
|
52
|
+
|
|
53
|
+
## Requirements
|
|
54
|
+
|
|
55
|
+
| Requirement | Minimum version |
|
|
56
|
+
|---------------|-----------------|
|
|
57
|
+
| Ruby | 3.3.1 |
|
|
58
|
+
| ActiveSupport | 7.2.2 |
|
|
59
|
+
| jace | 0.2.0 |
|
|
60
|
+
|
|
61
|
+
`zyra` is compatible with any ORM framework that uses the `ActiveRecord`
|
|
62
|
+
interface (e.g. Rails with ActiveRecord).
|
|
63
|
+
|
|
64
|
+
---
|
|
65
|
+
|
|
66
|
+
## Initial setup
|
|
67
|
+
|
|
68
|
+
The gem requires no configuration files, environment variables, or separate
|
|
69
|
+
initializers. Simply include it in the seeding code:
|
|
70
|
+
|
|
71
|
+
```ruby
|
|
72
|
+
require 'zyra'
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
In Rails projects the gem is already loaded automatically via Bundler.
|
|
76
|
+
|
|
77
|
+
---
|
|
78
|
+
|
|
79
|
+
## Usage in Rails applications
|
|
80
|
+
|
|
81
|
+
The recommended place to use `zyra` is `db/seeds.rb`.
|
|
82
|
+
|
|
83
|
+
### Step 1 — Register the model
|
|
84
|
+
|
|
85
|
+
Call `Zyra.register` passing the model class and the attribute (or list of
|
|
86
|
+
attributes) to use as the lookup key:
|
|
87
|
+
|
|
88
|
+
```ruby
|
|
89
|
+
# db/seeds.rb
|
|
90
|
+
|
|
91
|
+
Zyra.register(User, find_by: :email)
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Multiple keys:
|
|
95
|
+
|
|
96
|
+
```ruby
|
|
97
|
+
Zyra.register(Product, find_by: %i[sku store_id])
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
Register with a custom symbolic key (useful when the same model needs more than
|
|
101
|
+
one registration):
|
|
102
|
+
|
|
103
|
+
```ruby
|
|
104
|
+
Zyra.register(User, :admin_user, find_by: :email)
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
> When no key is provided, it is automatically derived from the class name
|
|
108
|
+
> (e.g. `User` → `:user`, `Admin::User` → `:admin_user`).
|
|
109
|
+
|
|
110
|
+
### Step 2 — Find or create the record
|
|
111
|
+
|
|
112
|
+
```ruby
|
|
113
|
+
# db/seeds.rb
|
|
114
|
+
|
|
115
|
+
user = Zyra.find_or_create(
|
|
116
|
+
:user,
|
|
117
|
+
email: 'admin@example.com',
|
|
118
|
+
name: 'Administrator',
|
|
119
|
+
role: 'admin'
|
|
120
|
+
)
|
|
121
|
+
# => a persisted User instance
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
On the **first run** the user is created with all provided attributes. On
|
|
125
|
+
**subsequent runs** the existing user is found by `email` and returned
|
|
126
|
+
unchanged (unless hooks are configured).
|
|
127
|
+
|
|
128
|
+
### Step 3 — Use the block to always update certain fields
|
|
129
|
+
|
|
130
|
+
The block passed to `find_or_create` is executed both on creation and when an
|
|
131
|
+
existing record is found, making it useful to ensure certain fields are always
|
|
132
|
+
up-to-date:
|
|
133
|
+
|
|
134
|
+
```ruby
|
|
135
|
+
attributes = {
|
|
136
|
+
email: 'admin@example.com',
|
|
137
|
+
name: 'Administrator',
|
|
138
|
+
password: 'secure_password'
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
Zyra.find_or_create(:user, attributes) do |user|
|
|
142
|
+
user.update(attributes)
|
|
143
|
+
end
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
---
|
|
147
|
+
|
|
148
|
+
## Available hooks
|
|
149
|
+
|
|
150
|
+
Hooks are registered with `Zyra.on(key, event)` (or chained on the return value
|
|
151
|
+
of `register`). The four possible events are:
|
|
152
|
+
|
|
153
|
+
| Event | When it fires |
|
|
154
|
+
|-----------|-----------------------------------------------------------|
|
|
155
|
+
| `:build` | After the object is instantiated (before saving) |
|
|
156
|
+
| `:create` | After the object is saved for the first time |
|
|
157
|
+
| `:found` | When the object is found in the database |
|
|
158
|
+
| `:return` | Always, after `:build`/`:create`/`:found` (post-return) |
|
|
159
|
+
|
|
160
|
+
### Example: generate a token only on creation
|
|
161
|
+
|
|
162
|
+
```ruby
|
|
163
|
+
Zyra.register(User, find_by: :email)
|
|
164
|
+
.on(:build) do |user|
|
|
165
|
+
user.api_token = SecureRandom.hex(16)
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
Zyra.find_or_create(:user, email: 'usr@srv.com', name: 'John')
|
|
169
|
+
# api_token is generated only the first time
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### Example: force an update on every run
|
|
173
|
+
|
|
174
|
+
```ruby
|
|
175
|
+
Zyra.register(User, find_by: :email)
|
|
176
|
+
|
|
177
|
+
Zyra.on(:user, :return) do |user|
|
|
178
|
+
user.update(last_seeded_at: Time.current)
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
Zyra.find_or_create(:user, email: 'usr@srv.com')
|
|
182
|
+
# last_seeded_at is updated on every seed run
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
### Example: create associated records only on first creation
|
|
186
|
+
|
|
187
|
+
```ruby
|
|
188
|
+
Zyra.register(User, find_by: :email)
|
|
189
|
+
.on(:build) do |user|
|
|
190
|
+
user.posts.build(title: 'Welcome', body: 'First post')
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
Zyra.find_or_create(:user, email: 'usr@srv.com', name: 'John').reload
|
|
194
|
+
# The post is created only when the user is created for the first time
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
---
|
|
198
|
+
|
|
199
|
+
## Full example in `db/seeds.rb`
|
|
200
|
+
|
|
201
|
+
```ruby
|
|
202
|
+
# db/seeds.rb
|
|
203
|
+
|
|
204
|
+
# 1. Register models
|
|
205
|
+
Zyra.register(Role, find_by: :name)
|
|
206
|
+
Zyra.register(User, find_by: :email)
|
|
207
|
+
.on(:build) { |u| u.api_token = SecureRandom.hex(16) }
|
|
208
|
+
|
|
209
|
+
# 2. Create roles
|
|
210
|
+
admin_role = Zyra.find_or_create(:role, name: 'admin')
|
|
211
|
+
_user_role = Zyra.find_or_create(:role, name: 'user')
|
|
212
|
+
|
|
213
|
+
# 3. Create the admin user and always keep the name up-to-date
|
|
214
|
+
Zyra.find_or_create(
|
|
215
|
+
:user,
|
|
216
|
+
email: 'admin@example.com',
|
|
217
|
+
name: 'Admin',
|
|
218
|
+
role: admin_role
|
|
219
|
+
) do |user|
|
|
220
|
+
user.update(name: 'Admin')
|
|
221
|
+
end
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
Run with:
|
|
225
|
+
|
|
226
|
+
```bash
|
|
227
|
+
rails db:seed
|
|
228
|
+
# or, to start from scratch:
|
|
229
|
+
rails db:reset
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
---
|
|
233
|
+
|
|
234
|
+
## Best practices and conventions
|
|
235
|
+
|
|
236
|
+
1. **Register models before using them** — ideally at the top of `db/seeds.rb`
|
|
237
|
+
or in a separate file (`db/seeds/registrations.rb`) loaded at the start.
|
|
238
|
+
|
|
239
|
+
2. **Use `find_by` with unique and stable attributes** — emails, slugs, internal
|
|
240
|
+
codes. Avoid attributes that change frequently.
|
|
241
|
+
|
|
242
|
+
3. **Prefer the block for optional updates** — put in the block only what must
|
|
243
|
+
always be updated; attributes outside the block are used only on creation.
|
|
244
|
+
|
|
245
|
+
4. **Use `:build` hooks for data that should be generated only once** — tokens,
|
|
246
|
+
unique references, etc.
|
|
247
|
+
|
|
248
|
+
5. **Use `:return` hooks for data that should always be refreshed** — audit
|
|
249
|
+
timestamps, counters, etc.
|
|
250
|
+
|
|
251
|
+
6. **One `Zyra.register` per model per key** — if you need to look up the same
|
|
252
|
+
model by different attributes, provide a distinct symbolic key:
|
|
253
|
+
|
|
254
|
+
```ruby
|
|
255
|
+
Zyra.register(User, :user_by_email, find_by: :email)
|
|
256
|
+
Zyra.register(User, :user_by_name, find_by: :name)
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
7. **Avoid `Zyra.reset` in production code** — this method exists to facilitate
|
|
260
|
+
testing and clears all registered models.
|
|
261
|
+
|
|
262
|
+
---
|
|
263
|
+
|
|
264
|
+
## Relevant rake tasks
|
|
265
|
+
|
|
266
|
+
| Command | Description |
|
|
267
|
+
|-------------------|----------------------------------------------------------|
|
|
268
|
+
| `rails db:seed` | Runs `db/seeds.rb` (use `zyra` here) |
|
|
269
|
+
| `rails db:reset` | Recreates the database and runs the seeds |
|
|
270
|
+
| `rails db:setup` | Creates the database, runs migrations and seeds |
|
|
271
|
+
|
|
272
|
+
---
|
|
273
|
+
|
|
274
|
+
## Internal file reference for `darthjee/zyra`
|
|
275
|
+
|
|
276
|
+
| File | Responsibility |
|
|
277
|
+
|---------------------------------|----------------------------------------------------------|
|
|
278
|
+
| `lib/zyra.rb` | Main module; exposes `register`, `on`, `find_or_create` |
|
|
279
|
+
| `lib/zyra/registry.rb` | Keeps the map of registered models |
|
|
280
|
+
| `lib/zyra/finder_creator.rb` | Orchestrates the lookup and creation of a record |
|
|
281
|
+
| `lib/zyra/finder.rb` | Queries the database using the lookup key attributes |
|
|
282
|
+
| `lib/zyra/creator.rb` | Instantiates and persists a new record |
|
|
283
|
+
| `lib/zyra/exceptions.rb` | Gem exceptions (`NotRegistered`, etc.) |
|
|
284
|
+
| `lib/zyra/version.rb` | Version constant (`Zyra::VERSION`) |
|
|
285
|
+
|
|
286
|
+
Full YARD documentation: <https://www.rubydoc.info/gems/zyra>
|
|
287
|
+
(replace with the version installed in your project, e.g. `/gems/zyra/1.2.0`)
|
|
288
|
+
|
|
289
|
+
Repository: <https://github.com/darthjee/zyra>
|
data/.gitignore
CHANGED
data/.rubocop.yml
CHANGED
data/.rubocop_todo.yml
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# This configuration was generated by
|
|
2
2
|
# `rubocop --auto-gen-config`
|
|
3
|
-
# on
|
|
3
|
+
# on 2026-03-10 01:29:02 UTC using RuboCop version 1.85.1.
|
|
4
4
|
# The point is for the user to remove these configuration records
|
|
5
5
|
# one by one as the offenses are removed from the code base.
|
|
6
6
|
# Note that changes in the inspected code, or installation of new
|
|
@@ -10,3 +10,4 @@
|
|
|
10
10
|
# Configuration parameters: AllowSubject.
|
|
11
11
|
RSpec/MultipleMemoizedHelpers:
|
|
12
12
|
Max: 7
|
|
13
|
+
|