Class: Aequitas::ContextualRuleSet
- Inherits:
-
Object
- Object
- Aequitas::ContextualRuleSet
- Extended by:
- ValueObject, Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/aequitas/contextual_rule_set.rb
Instance Attribute Summary (collapse)
-
- (Object) rule_sets
readonly
Clear all named context rule sets.
-
- (MessageTransformer?) transformer
MessageTransformer to use for transforming Violations on Resources.
Attributes included from ValueObject
Instance Method Summary (collapse)
-
- (Array) [](attribute_name)
Retrieve Rules applicable to a given attribute name.
-
- (self) add(rule_class, attribute_names, options = {}, &block)
private
Create a new rule of the given class for each name in attribute_names and add the rules to the RuleSet(s) indicated.
-
- (self) concat(other)
private
Assimilate all rules contained in other into the receiver.
-
- (RuleSet) context(context_name)
Return the RuleSet for a given context name.
-
- (ContextualRuleSet) initialize
constructor
A new instance of ContextualRuleSet.
-
- (ViolationSet) validate(resource, context_name = default_context_name)
Delegate #validate to the appropriate RuleSet.
Methods included from ValueObject
Constructor Details
- (ContextualRuleSet) initialize
A new instance of ContextualRuleSet
50 51 52 53 |
# File 'lib/aequitas/contextual_rule_set.rb', line 50 def initialize @rule_sets = Hash.new define_context(default_context_name) end |
Instance Attribute Details
- (Object) rule_sets (readonly)
Clear all named context rule sets
41 42 43 |
# File 'lib/aequitas/contextual_rule_set.rb', line 41 def rule_sets @rule_sets end |
- (MessageTransformer?) transformer
MessageTransformer to use for transforming Violations on Resources
When set, overrides Violation.default_transformer when transforming violations on instances of the class to which this ContextualRuleSet is attached.
TODO: rework the default transformer lookup strategy
It's wonky that violations get a transformer from the ContextualRuleSet
of the class of the instance to which they apply (too much coupling).
I'm currently leaning towards Violations looking up a transformer from
the Rule that produced them.
That said, I don't know if it's better for a Violation to look up its
transformer from their Rule, or from the instance they are attached to.
34 35 36 |
# File 'lib/aequitas/contextual_rule_set.rb', line 34 def transformer @transformer end |
Instance Method Details
- (Array) [](attribute_name)
Retrieve Rules applicable to a given attribute name
87 88 89 |
# File 'lib/aequitas/contextual_rule_set.rb', line 87 def [](attribute_name) context(default_context_name).fetch(attribute_name, []) end |
- (self) add(rule_class, attribute_names, options = {}, &block)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Create a new rule of the given class for each name in attribute_names and add the rules to the RuleSet(s) indicated
TODO: push responsibility for the array of attribute_names out to methods
in Aequitas::Macros.
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/aequitas/contextual_rule_set.rb', line 114 def add(rule_class, attribute_names, = {}, &block) context_names = extract_context_names() attribute_names.each do |attribute_name| rules = rule_class.rules_for(attribute_name, , &block) context_names.each { |context| add_rules_to_context(context, rules) } end self end |
- (self) concat(other)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Assimilate all rules contained in other into the receiver
134 135 136 137 138 139 140 |
# File 'lib/aequitas/contextual_rule_set.rb', line 134 def concat(other) other.rule_sets.each do |context_name, rule_set| add_rules_to_context(context_name, rule_set) end self end |
- (RuleSet) context(context_name)
Return the RuleSet for a given context name
74 75 76 |
# File 'lib/aequitas/contextual_rule_set.rb', line 74 def context(context_name) rule_sets.fetch(context_name) end |
- (ViolationSet) validate(resource, context_name = default_context_name)
Delegate #validate to the appropriate RuleSet
61 62 63 |
# File 'lib/aequitas/contextual_rule_set.rb', line 61 def validate(resource, context_name = default_context_name) context(context_name).validate(resource) end |