mercy 1.5.0 → 2.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 +5 -5
- data/.circleci/config.yml +89 -9
- data/.claude/agents/architect.md +48 -0
- data/.claude/agents/infra.md +36 -0
- data/.claude/agents/ruby.md +45 -0
- data/.claude/configuration/arcanum-repo-config.json +3 -0
- data/.claude/scripts/check_infra.sh +6 -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 +10 -0
- data/.github/copilot-instructions.md +1 -0
- data/.github/pull_request_template.md +17 -0
- data/.gitignore +8 -0
- data/.rubocop.yml +39 -0
- data/.rubocop_todo.yml +60 -0
- data/AGENTS.md +58 -0
- data/CLAUDE.md +1 -0
- data/Dockerfile +21 -0
- data/Gemfile +26 -1
- data/README.md +13 -4
- data/Rakefile +11 -3
- data/config/check_specs.yml +7 -0
- data/config/rubycritc.rb +12 -0
- data/config/yardstick.rb +13 -0
- data/config/yardstick.yml +41 -0
- data/docker-compose.yml +14 -9
- data/docs/agents/arcanum-split-issue.md +9 -0
- data/docs/agents/architecture.md +61 -0
- data/docs/agents/contributing.md +195 -0
- data/docs/agents/flow.md +5 -0
- data/docs/agents/folder-structure.md +26 -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/arstotzka/type_cast_ext.rb +52 -2
- data/lib/mercy/class_methods.rb +77 -7
- data/lib/mercy/concern.rb +105 -1
- data/lib/mercy/period_parser.rb +46 -3
- data/lib/mercy/report/active_record.rb +20 -16
- data/lib/mercy/report/error.rb +6 -4
- data/lib/mercy/report/multiple.rb +26 -22
- data/lib/mercy/report/range.rb +6 -4
- data/lib/mercy/report.rb +38 -7
- data/lib/mercy/report_builder.rb +2 -0
- data/lib/mercy/report_config.rb +5 -0
- data/lib/mercy/status.rb +7 -1
- data/lib/mercy/status_builder.rb +2 -0
- data/lib/mercy/version.rb +3 -1
- data/lib/mercy.rb +2 -0
- data/mercy.gemspec +19 -25
- data/scripts/bump_version.sh +62 -0
- data/scripts/check_readme.sh +6 -0
- data/scripts/rubycritic.sh +10 -0
- data/spec/dummy/.ruby-version +1 -0
- data/spec/dummy/Rakefile +10 -0
- data/spec/dummy/app/channels/application_cable/channel.rb +6 -0
- data/spec/dummy/app/channels/application_cable/connection.rb +6 -0
- data/spec/dummy/app/controllers/application_controller.rb +4 -0
- data/spec/dummy/app/controllers/concerns/.keep +0 -0
- data/spec/dummy/app/controllers/document_report_controller.rb +34 -0
- data/spec/dummy/app/controllers/example_report_controller.rb +27 -0
- data/spec/dummy/app/helpers/application_helper.rb +4 -0
- data/spec/dummy/app/jobs/application_job.rb +4 -0
- data/spec/dummy/app/mailers/application_mailer.rb +6 -0
- data/spec/dummy/app/models/application_record.rb +5 -0
- data/spec/dummy/app/models/concerns/.keep +0 -0
- data/spec/dummy/app/models/document.rb +9 -0
- data/spec/dummy/app/models/dummy_report.rb +6 -0
- data/spec/dummy/app/models/mercy/report/doc_type_error.rb +20 -0
- data/spec/dummy/app/models/mercy/report/dummy1.rb +19 -0
- data/spec/dummy/app/models/mercy/report/dummy_report.rb +11 -0
- data/spec/dummy/app/models/mercy/report/multiple/dummy.rb +28 -0
- data/spec/dummy/app/models/mercy/report/multiple/successes.rb +28 -0
- data/spec/dummy/app/models/mercy/report/success.rb +18 -0
- data/spec/dummy/app/views/layouts/application.html.erb +15 -0
- data/spec/dummy/app/views/layouts/mailer.html.erb +13 -0
- data/spec/dummy/app/views/layouts/mailer.text.erb +1 -0
- data/spec/dummy/bin/bundle +5 -0
- data/spec/dummy/bin/rails +6 -0
- data/spec/dummy/bin/rake +6 -0
- data/spec/dummy/bin/setup +38 -0
- data/spec/dummy/bin/update +33 -0
- data/spec/dummy/bin/yarn +11 -0
- data/spec/dummy/config/application.rb +24 -0
- data/spec/dummy/config/boot.rb +7 -0
- data/spec/dummy/config/cable.yml +10 -0
- data/spec/dummy/config/database.yml +20 -0
- data/spec/dummy/config/environment.rb +7 -0
- data/spec/dummy/config/environments/development.rb +55 -0
- data/spec/dummy/config/environments/production.rb +95 -0
- data/spec/dummy/config/environments/test.rb +52 -0
- data/spec/dummy/config/initializers/application_controller_renderer.rb +10 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +9 -0
- data/spec/dummy/config/initializers/content_security_policy.rb +27 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +7 -0
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +6 -0
- data/spec/dummy/config/initializers/inflections.rb +18 -0
- data/spec/dummy/config/initializers/mime_types.rb +6 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +16 -0
- data/spec/dummy/config/locales/en.yml +33 -0
- data/spec/dummy/config/puma.rb +36 -0
- data/spec/dummy/config/routes.rb +16 -0
- data/spec/dummy/config/spring.rb +8 -0
- data/spec/dummy/config/storage.yml +34 -0
- data/spec/dummy/config.ru +7 -0
- data/spec/dummy/db/migrate/20190508170435_create_documents.rb +13 -0
- data/spec/dummy/db/schema.rb +27 -0
- data/spec/dummy/log/.keep +0 -0
- data/spec/dummy/package.json +5 -0
- data/spec/dummy/public/404.html +67 -0
- data/spec/dummy/public/422.html +67 -0
- data/spec/dummy/public/500.html +66 -0
- data/spec/dummy/public/apple-touch-icon-precomposed.png +0 -0
- data/spec/dummy/public/apple-touch-icon.png +0 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/storage/.keep +0 -0
- data/spec/dummy/tmp/.keep +0 -0
- data/spec/dummy/tmp/storage/.keep +0 -0
- data/spec/integration/controller/error_report_controller_spec.rb +196 -0
- data/spec/integration/controller/multiple_report_spec.rb +86 -0
- data/spec/integration/controller/range_report_spec.rb +194 -0
- data/spec/integration/yard/arstotzka/type_cast_spec.rb +43 -0
- data/spec/integration/yard/controller/example_report_controller_spec.rb +95 -0
- data/spec/integration/yard/mercy/period_parser_spec.rb +21 -0
- data/spec/lib/arstotzka/type_cast_spec.rb +10 -9
- data/spec/lib/mercy/period_parser_spec.rb +10 -9
- data/spec/lib/mercy/report/error_spec.rb +93 -70
- data/spec/lib/mercy/report/multiple_spec.rb +9 -57
- data/spec/lib/mercy/report/range_spec.rb +70 -41
- data/spec/lib/mercy/report_builder_spec.rb +34 -17
- data/spec/lib/mercy/report_config_spec.rb +48 -0
- data/spec/lib/mercy/report_spec.rb +5 -15
- data/spec/lib/mercy/status_builder_spec.rb +33 -22
- data/spec/lib/mercy/status_spec.rb +69 -49
- data/spec/spec_helper.rb +18 -7
- data/spec/support/fixture_helpers.rb +4 -2
- data/spec/support/models/arstotzka/type_cast/dummy.rb +9 -0
- data/spec/support/shared_contexts/document_setup.rb +31 -0
- data/spec/support/shared_examples/time_parser.rb +2 -0
- metadata +123 -109
- data/spec/lib/mercy/report/report_config_spec.rb +0 -39
- data/spec/support/models/document.rb +0 -6
- data/spec/support/report/dummy.rb +0 -15
- data/spec/support/schema.rb +0 -11
data/config/yardstick.rb
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'yardstick/rake/measurement'
|
|
4
|
+
require 'yardstick/rake/verify'
|
|
5
|
+
require 'yaml'
|
|
6
|
+
|
|
7
|
+
options = YAML.load_file('config/yardstick.yml')
|
|
8
|
+
|
|
9
|
+
Yardstick::Rake::Measurement.new(:yardstick_measure, options) do |measurement|
|
|
10
|
+
measurement.output = 'measurement/report.txt'
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
Yardstick::Rake::Verify.new(:verify_measurements, options)
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
threshold: 48.4
|
|
2
|
+
require_exact_threshold: false
|
|
3
|
+
rules:
|
|
4
|
+
ApiTag::Presence:
|
|
5
|
+
enabled: true
|
|
6
|
+
exclude: []
|
|
7
|
+
ApiTag::Inclusion:
|
|
8
|
+
enabled: true
|
|
9
|
+
exclude: []
|
|
10
|
+
ApiTag::ProtectedMethod:
|
|
11
|
+
enabled: true
|
|
12
|
+
exclude: []
|
|
13
|
+
ApiTag::PrivateMethod:
|
|
14
|
+
enabled: true
|
|
15
|
+
exclude: []
|
|
16
|
+
ExampleTag:
|
|
17
|
+
enabled: true
|
|
18
|
+
exclude:
|
|
19
|
+
- Mercy::Report#json
|
|
20
|
+
- Mercy::Report#initialize
|
|
21
|
+
ReturnTag:
|
|
22
|
+
enabled: true
|
|
23
|
+
exclude:
|
|
24
|
+
- Mercy::PeriodParser#period
|
|
25
|
+
- Mercy::Report#json
|
|
26
|
+
Summary::Presence:
|
|
27
|
+
enabled: true
|
|
28
|
+
exclude:
|
|
29
|
+
- Mercy::PeriodParser#initialize
|
|
30
|
+
- Mercy::PeriodParser#period
|
|
31
|
+
- Mercy::Report#initialize
|
|
32
|
+
- Mercy::Report#json
|
|
33
|
+
Summary::Length:
|
|
34
|
+
enabled: true
|
|
35
|
+
exclude: []
|
|
36
|
+
Summary::Delimiter:
|
|
37
|
+
enabled: true
|
|
38
|
+
exclude: []
|
|
39
|
+
Summary::SingleLine:
|
|
40
|
+
enabled: true
|
|
41
|
+
exclude: []
|
data/docker-compose.yml
CHANGED
|
@@ -1,18 +1,23 @@
|
|
|
1
|
-
version: '
|
|
1
|
+
version: '3'
|
|
2
2
|
services:
|
|
3
3
|
base: &base
|
|
4
|
-
image:
|
|
5
|
-
working_dir: /home/app/
|
|
4
|
+
image: mercy
|
|
5
|
+
working_dir: /home/app/app
|
|
6
6
|
volumes:
|
|
7
|
-
- .:/home/app/
|
|
8
|
-
- mercy_gems_2_4_0:/usr/local/bundle
|
|
7
|
+
- .:/home/app/app
|
|
9
8
|
|
|
10
|
-
|
|
9
|
+
base_build:
|
|
10
|
+
<<: *base
|
|
11
|
+
build: .
|
|
12
|
+
command: echo done
|
|
11
13
|
|
|
12
14
|
mercy:
|
|
13
15
|
<<: *base
|
|
14
16
|
container_name: mercy
|
|
15
|
-
|
|
17
|
+
depends_on: [base_build]
|
|
18
|
+
command: /bin/bash -c 'rspec'
|
|
16
19
|
|
|
17
|
-
|
|
18
|
-
|
|
20
|
+
test_all:
|
|
21
|
+
<<: *base
|
|
22
|
+
depends_on: [base_build]
|
|
23
|
+
command: /bin/bash -c 'rspec && yard && rake yardstick_measure && rake verify_measurements'
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
# Arcanum Split Issue
|
|
2
|
+
|
|
3
|
+
A checklist of concerns to consider when splitting a broad issue into sub-issues via `/arcanum-split-issue`. Not exhaustive — adjust or extend the list for this project's needs.
|
|
4
|
+
|
|
5
|
+
- **Sub-issue granularity** — is each sub-issue independently workable, or does it still depend on another sub-issue landing first?
|
|
6
|
+
- **Standalone clarity** — does each sub-issue stand on its own, without requiring the reader to have the parent issue open to understand it?
|
|
7
|
+
- **Shared contracts** — interfaces, schemas, or config keys touched by more than one sub-issue, and who owns getting them right first.
|
|
8
|
+
- **Sequencing** — is there a natural order sub-issues should be implemented/merged in, or can they proceed in parallel?
|
|
9
|
+
- **Responsible agents** — which specialist agent(s) each sub-issue is likely to fall to.
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# Architecture
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
Mercy is a Rails controller concern for health checks. A controller does
|
|
6
|
+
`include Mercy`, declares reports at class level with `status_report`, and
|
|
7
|
+
renders them in an action with `render_status(bucket)`.
|
|
8
|
+
|
|
9
|
+
Declared reports are stored as configurations grouped into **buckets** (`on:`,
|
|
10
|
+
default `:default`). On each request the bucket's configurations are turned into
|
|
11
|
+
report instances (merging whitelisted request params such as `period` and
|
|
12
|
+
`threshold`), aggregated into a `Status`, and rendered as JSON with HTTP status
|
|
13
|
+
`:ok` or `:error`.
|
|
14
|
+
|
|
15
|
+
Report options are read through [Arstotzka](https://github.com/darthjee/arstotzka)
|
|
16
|
+
`expose`, with custom type casts (`period`, `infinity_float`).
|
|
17
|
+
|
|
18
|
+
## Source Code Layout
|
|
19
|
+
|
|
20
|
+
All source code lives under `lib/`.
|
|
21
|
+
|
|
22
|
+
### `lib/mercy.rb`
|
|
23
|
+
|
|
24
|
+
Entry point: requires dependencies (`darthjee-core_ext`, `darthjee-active_ext`,
|
|
25
|
+
`arstotzka`) and all Mercy components.
|
|
26
|
+
|
|
27
|
+
### `lib/mercy/`
|
|
28
|
+
|
|
29
|
+
- `concern.rb` — `Mercy` as an `ActiveSupport::Concern`; defines the instance
|
|
30
|
+
method `render_status`.
|
|
31
|
+
- `class_methods.rb` — `status_report` DSL and the per-controller `status_builder`.
|
|
32
|
+
- `status_builder.rb` — groups report keys into buckets and builds a `Status`
|
|
33
|
+
for a bucket.
|
|
34
|
+
- `report_builder.rb` — stores a `ReportConfig` per report key and builds reports.
|
|
35
|
+
- `report_config.rb` — resolves the report class from `type:` (symbol or class,
|
|
36
|
+
default `:error`) and slices request params to the class's `ALLOWED_PARAMETERS`.
|
|
37
|
+
- `status.rb` — aggregates reports; status is `:error` if any report errors.
|
|
38
|
+
- `report.rb` — base `Report` class (default options, `status`, `as_json`).
|
|
39
|
+
- `period_parser.rb` — parses strings like `3.days` / `1hours` into seconds.
|
|
40
|
+
- `version.rb` — gem version.
|
|
41
|
+
|
|
42
|
+
### `lib/mercy/report/`
|
|
43
|
+
|
|
44
|
+
- `active_record.rb` — base for ActiveRecord-backed reports: `clazz`,
|
|
45
|
+
`base_scope`, `period`, and chained scope resolution (`'active.with_error'`).
|
|
46
|
+
- `error.rb` — `Report::Error`: percentage of records in `scope` within the
|
|
47
|
+
period vs. `threshold`; outputs offending ids.
|
|
48
|
+
- `range.rb` — `Report::Range`: count of records in `scope` must be within
|
|
49
|
+
`minimum..maximum`.
|
|
50
|
+
- `multiple.rb` — `Report::Multiple` mixin: builds and aggregates sub-reports.
|
|
51
|
+
|
|
52
|
+
### `lib/arstotzka/`
|
|
53
|
+
|
|
54
|
+
- `type_cast_ext.rb` — adds `to_period` and `to_infinity_float` casts to
|
|
55
|
+
`Arstotzka::TypeCast`.
|
|
56
|
+
|
|
57
|
+
## Adding a new report type
|
|
58
|
+
|
|
59
|
+
Subclass `Mercy::Report` (or `Report::ActiveRecord`), define
|
|
60
|
+
`ALLOWED_PARAMETERS`, `DEFAULT_OPTION`, `error?` and `as_json`. It can then be
|
|
61
|
+
used via `type: :your_type` (resolved under `Mercy::Report`) or `type: YourClass`.
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
# Contributing
|
|
2
|
+
|
|
3
|
+
## Commit Guidelines
|
|
4
|
+
|
|
5
|
+
- **Atomic and Unitary:** Each commit must represent a single logical change.
|
|
6
|
+
*Example:*
|
|
7
|
+
- Good: `Add minimum option to Mercy::Report::Range`
|
|
8
|
+
- Bad: `Add minimum option to Range and refactor StatusBuilder`
|
|
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
|
+
- Linting passes without errors.
|
|
24
|
+
- Code coverage is as high as reasonably possible.
|
|
25
|
+
- Public API is documented with YARD and the yardstick measurement passes.
|
|
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
|
+
|
|
32
|
+
```ruby
|
|
33
|
+
# Good: each method does one thing
|
|
34
|
+
class Worker
|
|
35
|
+
def fetch_job; end
|
|
36
|
+
|
|
37
|
+
def process_job(job); end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Bad: method does too much
|
|
41
|
+
class Worker
|
|
42
|
+
def run
|
|
43
|
+
fetch_job
|
|
44
|
+
process_job
|
|
45
|
+
send_metrics
|
|
46
|
+
cleanup
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
- This requirement applies primarily to source code. For specs, refactor only if there is excessive duplication.
|
|
52
|
+
|
|
53
|
+
### CI Checks
|
|
54
|
+
|
|
55
|
+
Before a PR is considered complete, all CI checks relevant to the modified parts of the project must pass locally.
|
|
56
|
+
|
|
57
|
+
CircleCI (`.circleci/config.yml`) runs a single `build` job for the whole repository:
|
|
58
|
+
|
|
59
|
+
| Modified folder | CircleCI job | Local commands |
|
|
60
|
+
| --- | --- | --- |
|
|
61
|
+
| any (`lib/`, `spec/`, `config/`, root files) | `build` | `bundle exec rspec` && `rubocop` && `bundle exec rake verify_measurements` && `scripts/check_readme.sh` |
|
|
62
|
+
| `lib/` | `build` (rubycritic step) | `bundle exec rubycritic --format console --branch origin/master -t 0 <changed lib files>` |
|
|
63
|
+
|
|
64
|
+
Inside Docker, `docker-compose run test_all` runs rspec, yard and the yardstick checks.
|
|
65
|
+
|
|
66
|
+
`scripts/check_readme.sh` verifies that the README links the YARD documentation for the current version in `lib/mercy/version.rb`; bump both together.
|
|
67
|
+
|
|
68
|
+
If a new folder or CI job is added in the future, its corresponding test and check commands must be included here before merging changes to that folder.
|
|
69
|
+
|
|
70
|
+
This same process must be followed when **planning how to resolve an issue**: include a final step in the plan that identifies the affected folders and lists the CI commands to run before opening a PR.
|
|
71
|
+
|
|
72
|
+
## Code Organization
|
|
73
|
+
|
|
74
|
+
### File Responsibility: Class Declarers vs Scripts
|
|
75
|
+
|
|
76
|
+
Every source file under `lib/` must act as a **class declarer** — it should define one or more classes or modules. Files must not act as **scripts** (i.e., they must not execute logic at require time or perform side effects beyond requiring dependencies and defining constants).
|
|
77
|
+
|
|
78
|
+
This is a library: there are no executable entrypoints. `lib/mercy.rb` is the gem's load entrypoint and only requires dependencies and components. Inside the test suite, `spec/dummy/` is a Rails application whose boot files (`config/`, `bin/`) are exempt.
|
|
79
|
+
|
|
80
|
+
*Example:*
|
|
81
|
+
|
|
82
|
+
```ruby
|
|
83
|
+
# Good: class declarer — defines a class
|
|
84
|
+
module Mercy
|
|
85
|
+
class PeriodParser
|
|
86
|
+
def to_seconds; end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Bad: script — executes logic at require time
|
|
91
|
+
builder = Mercy::StatusBuilder.new
|
|
92
|
+
builder.add_report_config(:failures, clazz: Document)
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Test files are exempt from this rule and may require modules and execute setup code freely.
|
|
96
|
+
|
|
97
|
+
### File Naming: snake_case Matching the Constant
|
|
98
|
+
|
|
99
|
+
Files must use **snake_case** naming matching the class/module they define, with the directory path mirroring the namespace.
|
|
100
|
+
|
|
101
|
+
*Examples:*
|
|
102
|
+
|
|
103
|
+
- `lib/mercy/status_builder.rb` for `Mercy::StatusBuilder`
|
|
104
|
+
- `lib/mercy/report_config.rb` for `Mercy::ReportConfig`
|
|
105
|
+
- `lib/mercy/report/range.rb` for `Mercy::Report::Range`
|
|
106
|
+
|
|
107
|
+
This applies to both source files and their corresponding spec files:
|
|
108
|
+
|
|
109
|
+
- `lib/mercy/status_builder.rb` → spec: `spec/lib/mercy/status_builder_spec.rb`
|
|
110
|
+
- `lib/mercy/report/range.rb` → spec: `spec/lib/mercy/report/range_spec.rb`
|
|
111
|
+
|
|
112
|
+
Every Ruby file starts with `# frozen_string_literal: true`.
|
|
113
|
+
|
|
114
|
+
### Method Order: Public Before Private
|
|
115
|
+
|
|
116
|
+
Within a class, **public methods must be declared before private methods**. Private methods serve as implementation helpers and should appear at the end of the class body, after a single `private` keyword.
|
|
117
|
+
|
|
118
|
+
*Example:*
|
|
119
|
+
|
|
120
|
+
```ruby
|
|
121
|
+
# Good: public methods first, private methods last
|
|
122
|
+
class Worker
|
|
123
|
+
def run
|
|
124
|
+
prepare
|
|
125
|
+
execute
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def status; end
|
|
129
|
+
|
|
130
|
+
private
|
|
131
|
+
|
|
132
|
+
def prepare; end
|
|
133
|
+
|
|
134
|
+
def execute; end
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# Bad: private methods mixed in with or before public methods
|
|
138
|
+
class Worker
|
|
139
|
+
private def prepare; end
|
|
140
|
+
|
|
141
|
+
def run; end
|
|
142
|
+
|
|
143
|
+
private
|
|
144
|
+
|
|
145
|
+
def execute; end
|
|
146
|
+
end
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
## Dependency Injection
|
|
150
|
+
|
|
151
|
+
Classes must receive their dependencies (data, configuration, collaborators) as constructor arguments. A class must never reach out to load files, read environment variables, or fetch configuration on its own.
|
|
152
|
+
|
|
153
|
+
**The caller is the only place responsible for loading configuration.** In Mercy, configuration comes from the host controller (`status_report` options) and request params, and is passed down through `StatusBuilder` → `ReportBuilder` → `ReportConfig` → report instances.
|
|
154
|
+
|
|
155
|
+
This makes every class independently testable: tests simply instantiate the class with the data they need, without touching the filesystem or environment.
|
|
156
|
+
|
|
157
|
+
*Example:*
|
|
158
|
+
|
|
159
|
+
```ruby
|
|
160
|
+
# Good: class receives data as an argument — easy to test
|
|
161
|
+
class Mercy::ReportConfig
|
|
162
|
+
def initialize(config)
|
|
163
|
+
@config = config
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# Bad: class loads its own config — hard to test and couples to the filesystem
|
|
168
|
+
class Mercy::ReportConfig
|
|
169
|
+
def config
|
|
170
|
+
YAML.load_file('config/mercy.yml') # ❌
|
|
171
|
+
end
|
|
172
|
+
end
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
This principle applies to all classes — including helpers and builders. If a class needs data, it gets it through its constructor.
|
|
176
|
+
|
|
177
|
+
## Refactoring Guidelines
|
|
178
|
+
|
|
179
|
+
When refactoring, aim to:
|
|
180
|
+
|
|
181
|
+
- **Reduce Code Duplication:**
|
|
182
|
+
*Example:* Move repeated setup code in specs to `let`, shared contexts or helpers under `spec/support/`.
|
|
183
|
+
|
|
184
|
+
```ruby
|
|
185
|
+
# Good
|
|
186
|
+
shared_context 'with documents' do
|
|
187
|
+
let(:documents) { create_documents(status: :error, count: 3) }
|
|
188
|
+
end
|
|
189
|
+
# In specs:
|
|
190
|
+
include_context 'with documents'
|
|
191
|
+
|
|
192
|
+
# Bad
|
|
193
|
+
let(:documents) { 3.times.map { Document.create(status: :error) } }
|
|
194
|
+
# ...repeated in many spec files
|
|
195
|
+
```
|
data/docs/agents/flow.md
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# Folder Structure
|
|
2
|
+
|
|
3
|
+
## Project Root
|
|
4
|
+
|
|
5
|
+
| Directory / File | Description |
|
|
6
|
+
|-----------------|-------------|
|
|
7
|
+
| `lib/` | Gem source code (`Mercy` module, reports, builders, Arstotzka type-cast extension). |
|
|
8
|
+
| `spec/` | RSpec test suite: unit specs, integration specs, a dummy Rails app and shared support code. |
|
|
9
|
+
| `config/` | Configuration for code-quality tooling (yardstick thresholds, rubycritic task). |
|
|
10
|
+
| `scripts/` | CI helper scripts (README version check, rubycritic run). |
|
|
11
|
+
| `docs/agents/` | Agent-facing documentation: architecture, flow, issues and plans. |
|
|
12
|
+
| `.circleci/` | CircleCI pipeline configuration. |
|
|
13
|
+
| `.github/` | PR/commit message templates and Copilot instructions. |
|
|
14
|
+
| `mercy.gemspec` | Gem specification: version, runtime and development dependencies. |
|
|
15
|
+
| `Rakefile` | Rake tasks (`spec`, yardstick measurement/verification). |
|
|
16
|
+
| `Dockerfile` / `docker-compose.yml` | Containerized dev/test environment (`mercy` runs rspec, `test_all` runs rspec + yard + yardstick). |
|
|
17
|
+
| `.rubocop.yml` / `.rubocop_todo.yml` | RuboCop configuration and pending offenses. |
|
|
18
|
+
|
|
19
|
+
## spec/
|
|
20
|
+
|
|
21
|
+
| Subdirectory | Description |
|
|
22
|
+
|--------------|-------------|
|
|
23
|
+
| `lib/` | Unit specs mirroring `lib/` (`mercy/`, `arstotzka/`). |
|
|
24
|
+
| `integration/` | Integration specs: `controller/` (concern inside the dummy Rails app) and `yard/` (examples from YARD docs). |
|
|
25
|
+
| `dummy/` | Minimal Rails application used to exercise the controller concern. |
|
|
26
|
+
| `support/` | Shared contexts/examples, test models and fixture helpers. |
|
|
@@ -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
|
+
|
|
@@ -1,13 +1,63 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module Arstotzka
|
|
4
|
+
# @api public
|
|
5
|
+
#
|
|
6
|
+
# Extension of Arstotzka::TypeCast
|
|
7
|
+
#
|
|
8
|
+
# @see https://www.rubydoc.info/gems/arstotzka/Arstotzka/TypeCast
|
|
9
|
+
# Arstotzka::Typecast
|
|
2
10
|
module TypeCast
|
|
11
|
+
# Converts value to period
|
|
12
|
+
#
|
|
13
|
+
# @param value [String] string to be converted
|
|
14
|
+
#
|
|
15
|
+
# @return [Integer] parsed period
|
|
16
|
+
#
|
|
17
|
+
# @example
|
|
18
|
+
# module Arstotzka
|
|
19
|
+
# module TypeCast
|
|
20
|
+
# class Dummy
|
|
21
|
+
# extend TypeCast
|
|
22
|
+
# end
|
|
23
|
+
# end
|
|
24
|
+
# end
|
|
25
|
+
#
|
|
26
|
+
# Arstotzka::TypeCast::Dummy.to_period('2days')
|
|
27
|
+
# # returns 2.days (2 * 24 * 3600 seconds)
|
|
3
28
|
def to_period(value)
|
|
4
29
|
Mercy::PeriodParser.parse(value)
|
|
5
30
|
end
|
|
6
31
|
|
|
32
|
+
# Converts value to float
|
|
33
|
+
#
|
|
34
|
+
# Accepts +"inf"+ and +"-inf"+ return +/- +Float::INFINITY+
|
|
35
|
+
#
|
|
36
|
+
# @param value [String] string to be converted
|
|
37
|
+
#
|
|
38
|
+
# @return [Float] Converted float
|
|
39
|
+
#
|
|
40
|
+
# @example
|
|
41
|
+
# module Arstotzka
|
|
42
|
+
# module TypeCast
|
|
43
|
+
# class Dummy
|
|
44
|
+
# extend TypeCast
|
|
45
|
+
# end
|
|
46
|
+
# end
|
|
47
|
+
# end
|
|
48
|
+
#
|
|
49
|
+
# Arstotzka::TypeCast::Dummy.to_infinity_float('-0.75')
|
|
50
|
+
# # returns -0.75
|
|
51
|
+
#
|
|
52
|
+
# Arstotzka::TypeCast::Dummy.to_infinity_float('inf')
|
|
53
|
+
# # returns Float::INFINITY
|
|
54
|
+
#
|
|
55
|
+
# Arstotzka::TypeCast::Dummy.to_infinity_float('-inf')
|
|
56
|
+
# # returns -Float::INFINITY
|
|
7
57
|
def to_infinity_float(value)
|
|
8
58
|
return value if value.is_a?(Numeric)
|
|
9
|
-
return -Float::INFINITY if value
|
|
10
|
-
return Float::INFINITY if value
|
|
59
|
+
return -Float::INFINITY if value =~ /-inf/
|
|
60
|
+
return Float::INFINITY if value =~ /inf/
|
|
11
61
|
|
|
12
62
|
value.to_f
|
|
13
63
|
end
|
data/lib/mercy/class_methods.rb
CHANGED
|
@@ -1,15 +1,85 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
module Mercy
|
|
4
|
+
# @api public
|
|
5
|
+
#
|
|
6
|
+
# module exposing methods to register reports
|
|
2
7
|
module ClassMethods
|
|
8
|
+
# register a report
|
|
9
|
+
#
|
|
10
|
+
# @param id [String,Symbol] report identifier
|
|
11
|
+
# @param options [Hash] report options
|
|
12
|
+
# (see the {Report report} type for more options)
|
|
13
|
+
# @option options type [Symbol,Class] type of the report.
|
|
14
|
+
# When a +Class+ is given, this will be used as report class.
|
|
15
|
+
# When a +Symbol+ is given, cleass will be found as a class
|
|
16
|
+
# defined in {Mercy::Report Mercy::Report}
|
|
17
|
+
#
|
|
18
|
+
# @return [Array<Symbol>] Existing reports for configuration
|
|
19
|
+
#
|
|
20
|
+
# @example
|
|
21
|
+
# class Document < ActiveRecord::Base
|
|
22
|
+
# scope :with_error, -> { where(status: :error) }
|
|
23
|
+
# scope :type_a, -> { where(doc_type: :a) }
|
|
24
|
+
# scope :type_b, -> { where(doc_type: :b) }
|
|
25
|
+
# end
|
|
26
|
+
#
|
|
27
|
+
# class ExampleReportController < ApplicationController
|
|
28
|
+
# include Mercy
|
|
29
|
+
#
|
|
30
|
+
# status_report :errors_a, base_scope: :type_a,
|
|
31
|
+
# scope: :with_error,
|
|
32
|
+
# clazz: Document,
|
|
33
|
+
# external_key: :external_id
|
|
34
|
+
# status_report :errors_b, base_scope: :type_b,
|
|
35
|
+
# scope: :with_error,
|
|
36
|
+
# clazz: Document,
|
|
37
|
+
# external_key: :external_id
|
|
38
|
+
#
|
|
39
|
+
# def status
|
|
40
|
+
# render_status
|
|
41
|
+
# end
|
|
42
|
+
# end
|
|
43
|
+
#
|
|
44
|
+
# Rails.application.routes.draw do
|
|
45
|
+
# resources :example_report, only: [] do
|
|
46
|
+
# get :status, on: :collection
|
|
47
|
+
# end
|
|
48
|
+
# end
|
|
49
|
+
#
|
|
50
|
+
# 49.times { Document.create(doc_type: :a) }
|
|
51
|
+
# 3.times { Document.create(doc_type: :b) }
|
|
52
|
+
# Document.with_error.create(doc_type: :a, external_id: 10)
|
|
53
|
+
# Document.with_error.create(doc_type: :b, external_id: 20)
|
|
54
|
+
#
|
|
55
|
+
# get '/example_reports/status'
|
|
56
|
+
# #returns http_status: 500
|
|
57
|
+
# # json:
|
|
58
|
+
# # {
|
|
59
|
+
# # "status":"error",
|
|
60
|
+
# # "errors_a":{
|
|
61
|
+
# # "ids":[10],
|
|
62
|
+
# # "percentage":0.02,
|
|
63
|
+
# # "status":"ok"
|
|
64
|
+
# # },
|
|
65
|
+
# # "errors_b":{
|
|
66
|
+
# # "ids":[20],
|
|
67
|
+
# # "percentage":0.25,
|
|
68
|
+
# # "status":"error"
|
|
69
|
+
# # }
|
|
70
|
+
# # }
|
|
71
|
+
def status_report(id, **options)
|
|
72
|
+
options[:id] = id
|
|
3
73
|
|
|
4
|
-
|
|
5
|
-
id = attr_names.first
|
|
6
|
-
options = {
|
|
7
|
-
id: id
|
|
8
|
-
}.merge(attr_names.extract_options!)
|
|
9
|
-
|
|
10
|
-
self.status_builder.add_report_config(id, options)
|
|
74
|
+
status_builder.add_report_config(id, options)
|
|
11
75
|
end
|
|
12
76
|
|
|
77
|
+
# @private
|
|
78
|
+
# @api private
|
|
79
|
+
#
|
|
80
|
+
# Status report builder for class
|
|
81
|
+
#
|
|
82
|
+
# @return [Mercy::StatusBuilder]
|
|
13
83
|
def status_builder
|
|
14
84
|
@status_builder ||= Mercy::StatusBuilder.new
|
|
15
85
|
end
|
data/lib/mercy/concern.rb
CHANGED
|
@@ -1,8 +1,112 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# @api public
|
|
4
|
+
#
|
|
5
|
+
# Concern exposing methods needed to build reports
|
|
1
6
|
module Mercy
|
|
2
7
|
extend ActiveSupport::Concern
|
|
3
8
|
|
|
9
|
+
# Render the status
|
|
10
|
+
#
|
|
11
|
+
# @param key [:Symbol] key of the configured report
|
|
12
|
+
#
|
|
13
|
+
# @return [String] Json rendered
|
|
14
|
+
#
|
|
15
|
+
# @example (see Mercy::ClassMethods#status_report)
|
|
16
|
+
#
|
|
17
|
+
# @example
|
|
18
|
+
# class Document < ActiveRecord::Base
|
|
19
|
+
# scope :with_success, -> { where(status: :success) }
|
|
20
|
+
# end
|
|
21
|
+
#
|
|
22
|
+
# module Mercy
|
|
23
|
+
# class Report
|
|
24
|
+
# class Success < Mercy::Report::Range
|
|
25
|
+
# ALLOWED_PARAMETERS = %i[period minimum maximum].freeze
|
|
26
|
+
# DEFAULT_OPTION = {
|
|
27
|
+
# clazz: Document
|
|
28
|
+
# }.freeze
|
|
29
|
+
#
|
|
30
|
+
# expose :doc_type, case: :snake
|
|
31
|
+
#
|
|
32
|
+
# def base
|
|
33
|
+
# super.with_success.where(doc_type: doc_type)
|
|
34
|
+
# end
|
|
35
|
+
# end
|
|
36
|
+
# end
|
|
37
|
+
# end
|
|
38
|
+
#
|
|
39
|
+
# module Mercy
|
|
40
|
+
# class Report
|
|
41
|
+
# module Multiple
|
|
42
|
+
# class Successes < Mercy::Report
|
|
43
|
+
# include Mercy::Report::Multiple
|
|
44
|
+
# DEFAULT_OPTION = {
|
|
45
|
+
# doc_type: %i[a b]
|
|
46
|
+
# }.freeze
|
|
47
|
+
# expose :doc_type, case: :snake
|
|
48
|
+
#
|
|
49
|
+
# def reports_ids
|
|
50
|
+
# [doc_type].flatten
|
|
51
|
+
# end
|
|
52
|
+
#
|
|
53
|
+
# def sub_report_class
|
|
54
|
+
# Mercy::Report::Success
|
|
55
|
+
# end
|
|
56
|
+
#
|
|
57
|
+
# def key
|
|
58
|
+
# :doc_type
|
|
59
|
+
# end
|
|
60
|
+
# end
|
|
61
|
+
# end
|
|
62
|
+
# end
|
|
63
|
+
# end
|
|
64
|
+
#
|
|
65
|
+
# class ExampleReportController < ApplicationController
|
|
66
|
+
# include Mercy
|
|
67
|
+
#
|
|
68
|
+
# status_report :successes, on: :successes,
|
|
69
|
+
# clazz: Document,
|
|
70
|
+
# type: Mercy::Report::Multiple::Successes,
|
|
71
|
+
# minimum: 10
|
|
72
|
+
#
|
|
73
|
+
# def status
|
|
74
|
+
# render_status
|
|
75
|
+
# end
|
|
76
|
+
#
|
|
77
|
+
# def count_status
|
|
78
|
+
# render_status(:successes)
|
|
79
|
+
# end
|
|
80
|
+
# end
|
|
81
|
+
#
|
|
82
|
+
# Rails.application.routes.draw do
|
|
83
|
+
# resources :example_report, only: [] do
|
|
84
|
+
# get :count_status, on: :collection
|
|
85
|
+
# end
|
|
86
|
+
# end
|
|
87
|
+
#
|
|
88
|
+
# 10.times { Document.with_success.create(doc_type: :a) }
|
|
89
|
+
# 2.times { Document.with_success.create(doc_type: :b) }
|
|
90
|
+
#
|
|
91
|
+
# get '/example_reports/count_status'
|
|
92
|
+
# #returns http_status: 500
|
|
93
|
+
# # json:
|
|
94
|
+
# # {
|
|
95
|
+
# # 'status' => 'error',
|
|
96
|
+
# # 'successes' => {
|
|
97
|
+
# # 'status' => 'error',
|
|
98
|
+
# # 'a' => {
|
|
99
|
+
# # 'count' => 10,
|
|
100
|
+
# # 'status' => 'ok'
|
|
101
|
+
# # },
|
|
102
|
+
# # 'b' => {
|
|
103
|
+
# # 'count' => 2,
|
|
104
|
+
# # 'status' => 'error'
|
|
105
|
+
# # }
|
|
106
|
+
# # }
|
|
4
107
|
def render_status(key = :default)
|
|
5
108
|
status = self.class.status_builder.build(key, params)
|
|
6
|
-
|
|
109
|
+
status_hash = status.as_json.stringify_keys.to_deep_hash
|
|
110
|
+
render json: status_hash, status: status.http_status
|
|
7
111
|
end
|
|
8
112
|
end
|