-
Notifications
You must be signed in to change notification settings - Fork 0
Added plugin registration for custom validators #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
HassanAkbar
wants to merge
6
commits into
main
Choose a base branch
from
feat/plugin-base
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7823f33
add plugin base and wire validator plugins into schematron
HassanAkbar 6f4125c
keep slash-separated plugin paths unchanged
HassanAkbar c376c76
let plugin dependency errors through untouched
HassanAkbar 1db2a3f
Merge remote-tracking branch 'origin/main' into feat/plugin-base
HassanAkbar b1555c5
narrow plugin system gaps to discovery and converters
HassanAkbar b2bc341
Give anonymous rule classes a usable issue code
HassanAkbar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Dcc | ||
| module Plugin | ||
| # Included by a plugin class so it can declare what it adds to Dcc. | ||
| # | ||
| # @example | ||
| # class MyPlugin | ||
| # include Dcc::Plugin::Base | ||
| # register_validator MyRule | ||
| # end | ||
| module Base | ||
| # @param base [Class] the including class. | ||
| def self.included(base) | ||
| base.extend(ClassMethods) | ||
| end | ||
|
|
||
| # `Profile#call` does `rule_class.new.check_on(dcc)`, so a validator | ||
| # has to be a class. An instance responds to `#check_on` and so looks | ||
| # right, but would fail deep inside validation with a message naming | ||
| # neither the plugin nor the author's line. | ||
| # | ||
| # @param entry [Object] the candidate rule. | ||
| # @raise [ArgumentError] unless entry is a class defining #check_on. | ||
| # @return [Class] the entry. | ||
| def self.rule_class!(entry) | ||
| return entry if rule_class?(entry) | ||
|
|
||
| raise ::ArgumentError, | ||
| "expected a rule class responding to #check_on, " \ | ||
| "got #{describe(entry)}" | ||
| end | ||
|
|
||
| # @param entry [Object] | ||
| # @return [Boolean] | ||
| def self.rule_class?(entry) | ||
| entry.is_a?(::Class) && entry.method_defined?(:check_on) | ||
| end | ||
| private_class_method :rule_class? | ||
|
|
||
| # @param entry [Object] | ||
| # @return [String] | ||
| def self.describe(entry) | ||
| return entry.inspect if entry.is_a?(::Class) | ||
|
|
||
| "an instance of #{entry.class}" | ||
| end | ||
| private_class_method :describe | ||
|
|
||
| # Declaration helpers available on the including class. | ||
| module ClassMethods | ||
| # Add a Schematron rule to the active validation profile. | ||
| # | ||
| # @param rule_class [Class] a rule class defining `#check_on(dcc)`. | ||
| # @raise [ArgumentError] unless rule_class is such a class. | ||
| # @return [Class] the rule class. | ||
| def register_validator(rule_class) | ||
| checked = ::Dcc::Plugin::Base.rule_class!(rule_class) | ||
| ::Dcc::Plugin.register(:validators, checked) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "spec_helper" | ||
|
|
||
| RSpec.describe Dcc::Plugin::Base do | ||
| before { Dcc::Plugin.reset! } | ||
| after { Dcc::Plugin.reset! } | ||
|
|
||
| let(:plugin_class) do | ||
| Class.new do | ||
| include Dcc::Plugin::Base | ||
| end | ||
| end | ||
|
|
||
| let(:rule) do | ||
| Class.new do | ||
| def check_on(_dcc) | ||
| [] | ||
| end | ||
| end | ||
| end | ||
|
|
||
| it "registers a validator through the class-level helper" do | ||
| plugin_class.register_validator(rule) | ||
| expect(Dcc::Plugin.all(:validators)).to include(rule) | ||
| end | ||
|
|
||
| it "returns the rule class from register_validator" do | ||
| expect(plugin_class.register_validator(rule)).to be(rule) | ||
| end | ||
|
|
||
| it "rejects an instance where a rule class was expected" do | ||
| expect { plugin_class.register_validator(rule.new) } | ||
| .to raise_error(ArgumentError, /got an instance of/) | ||
| end | ||
|
|
||
| it "rejects a class that does not define check_on" do | ||
| expect { plugin_class.register_validator(Class.new) } | ||
| .to raise_error(ArgumentError, /responding to #check_on/) | ||
| end | ||
|
|
||
| # Every real rule in the gem subclasses `Rules::Base` and defines no | ||
| # `check_on` of its own, so the guard has to see the inherited one. | ||
| describe "a rule that defines no check_on of its own" do | ||
| let(:subclass) { Class.new(rule) } | ||
|
|
||
| it "carries no check_on of its own" do | ||
| expect(subclass.instance_methods(false)).not_to include(:check_on) | ||
| end | ||
|
|
||
| it "inherits check_on from its superclass" do | ||
| expect(subclass.instance_method(:check_on).owner).to be(rule) | ||
| end | ||
|
|
||
| it "is accepted on that inherited method alone" do | ||
| plugin_class.register_validator(subclass) | ||
| expect(Dcc::Plugin.all(:validators)).to include(subclass) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Loaded by `Dcc.load_plugins("dcc-sample_plugin")` in the plugin specs. | ||
| # Stands in for a real `dcc-*` plugin gem's entry file. | ||
|
Copilot marked this conversation as resolved.
Outdated
|
||
| class DccSamplePlugin | ||
| include Dcc::Plugin::Base | ||
|
|
||
| # A plugin's own rule, declared as the file loads. | ||
| class SampleRule < ::Dcc::Validate::Schematron::Rules::Base | ||
| end | ||
|
|
||
| register_validator SampleRule | ||
| end | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.