Skip to content

Instantly share code, notes, and snippets.

@buildmotion
Created August 28, 2021 20:03
Show Gist options
  • Save buildmotion/e1bdce1a0ab8b6bcb3e15d36940edefc to your computer and use it in GitHub Desktop.
Save buildmotion/e1bdce1a0ab8b6bcb3e15d36940edefc to your computer and use it in GitHub Desktop.
import {RulePolicy} from './RulePolicy';
import {RuleResult} from './RuleResult';
export class CompositeRule extends RulePolicy {
hasErrors: boolean = false;
results: Array<RuleResult> = new Array<RuleResult>();
rules: Array<RulePolicy> = new Array<RulePolicy>();
constructor(name: string, message: string, isDisplayable: boolean) {
super(name, message, isDisplayable);
}
render(): RuleResult {
this.rules.sort(s => s.priority).forEach(r => this.results.push(r.execute()));
return this.processResults();
}
public hasRules(): boolean {
if (this.rules && this.rules.length > 0) {
return true;
}
return false;
}
processResults(): RuleResult {
if (this.results.filter(r => (r.isValid === false)).length > 0) {
this.isValid = false;
this.hasErrors = true;
}
return new RuleResult(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment