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
- #rule_sets ⇒ Object readonly private
-
#transformer ⇒ Object
MessageTransformer to use for transforming Violations on Resources instantiated from the model to which this ContextualRuleSet is bound.
Attributes included from ValueObject
Instance Method Summary collapse
-
#[](attribute_name) ⇒ Array
Retrieve Rules applicable to a given attribute name.
-
#add(rule_class, attribute_names, options = {}, &block) ⇒ self
Create a new rule of the given class for each name in
attribute_names
and add the rules to the RuleSet(s) indicated. -
#add_rules_to_context(context_name, rules) ⇒ self
Define a context and append rules to it.
-
#assert_valid_context(context_name) ⇒ Object
private
Assert that the given validation context name is valid for this contextual rule set.
-
#concat(other) ⇒ self
Assimilate all rules contained in
other
into the receiver. -
#context(context_name) ⇒ RuleSet
Return the RuleSet for a given context name.
- #context_defined?(context_name) ⇒ Boolean
-
#current_context ⇒ Symbol
private
Returns the current validation context on the stack if valid for this contextual rule set, nil if no RuleSets are defined (and no context names are on the validation context stack), or :default if the current context is invalid for this rule set or if no contexts have been defined for this contextual rule set and no context name is on the stack.
- #define_context(context_name) ⇒ Object
-
#initialize ⇒ ContextualRuleSet
constructor
A new instance of ContextualRuleSet.
-
#valid_context?(context_name) ⇒ Boolean
private
Test if the validation context name is valid for this contextual rule set.
-
#validate(resource, context_name) ⇒ Object
Delegate #validate to RuleSet.
Methods included from ValueObject
Constructor Details
#initialize ⇒ ContextualRuleSet
Returns a new instance of ContextualRuleSet.
33 34 35 36 |
# File 'lib/aequitas/contextual_rule_set.rb', line 33 def initialize @rule_sets = Hash.new define_context(:default) end |
Instance Attribute Details
#rule_sets ⇒ Object (readonly)
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.
24 25 26 |
# File 'lib/aequitas/contextual_rule_set.rb', line 24 def rule_sets @rule_sets end |
#transformer ⇒ Object
MessageTransformer to use for transforming Violations on Resources instantiated from the model to which this ContextualRuleSet is bound
21 22 23 |
# File 'lib/aequitas/contextual_rule_set.rb', line 21 def transformer @transformer end |
Instance Method Details
#[](attribute_name) ⇒ Array
Retrieve Rules applicable to a given attribute name
72 73 74 |
# File 'lib/aequitas/contextual_rule_set.rb', line 72 def [](attribute_name) context(:default).fetch(attribute_name, []) end |
#add(rule_class, attribute_names, options = {}, &block) ⇒ self
Create a new rule of the given class for each name in attribute_names
and add the rules to the RuleSet(s) indicated
94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/aequitas/contextual_rule_set.rb', line 94 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 |
#add_rules_to_context(context_name, rules) ⇒ self
Define a context and append rules to it
128 129 130 131 132 133 |
# File 'lib/aequitas/contextual_rule_set.rb', line 128 def add_rules_to_context(context_name, rules) define_context(context_name) context(context_name).concat(rules) self end |
#assert_valid_context(context_name) ⇒ Object
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.
Assert that the given validation context name
is valid for this contextual rule set
TODO: is this method actually needed?
190 191 192 193 194 195 196 |
# File 'lib/aequitas/contextual_rule_set.rb', line 190 def assert_valid_context(context_name) unless valid_context?(context_name) actual = context_name.inspect expected = rule_sets.keys.inspect raise InvalidContextError, "#{actual} is an invalid context, known contexts are #{expected}" end end |
#concat(other) ⇒ self
Assimilate all rules contained in other
into the receiver
112 113 114 115 116 117 118 |
# File 'lib/aequitas/contextual_rule_set.rb', line 112 def concat(other) other.rule_sets.each do |context_name, rule_set| add_rules_to_context(context_name, rules) end self end |
#context(context_name) ⇒ RuleSet
Return the RuleSet for a given context name
61 62 63 |
# File 'lib/aequitas/contextual_rule_set.rb', line 61 def context(context_name) rule_sets.fetch(context_name) end |
#context_defined?(context_name) ⇒ Boolean
174 175 176 |
# File 'lib/aequitas/contextual_rule_set.rb', line 174 def context_defined?(context_name) rule_sets.include?(context_name) end |
#current_context ⇒ Symbol
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.
Returns the current validation context on the stack if valid for this contextual rule set,
nil if no RuleSets are defined (and no context names are on the validation context stack),
or :default if the current context is invalid for this rule set or
if no contexts have been defined for this contextual rule set and
no context name is on the stack.
TODO: this logic behind this method is too complicated.
simplify the semantics of #current_context, #validate
152 153 154 155 |
# File 'lib/aequitas/contextual_rule_set.rb', line 152 def current_context context = Aequitas::Context.current valid_context?(context) ? context : :default end |
#define_context(context_name) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/aequitas/contextual_rule_set.rb', line 38 def define_context(context_name) rule_sets.fetch(context_name) do |context_name| rule_sets[context_name] = RuleSet.new end self end |
#valid_context?(context_name) ⇒ Boolean
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.
Test if the validation context name is valid for this contextual rule set.
A validation context name is valid if not nil and either:
1) no rule sets are defined for this contextual rule set
OR
2) there is a rule set defined for the given context name
170 171 172 |
# File 'lib/aequitas/contextual_rule_set.rb', line 170 def valid_context?(context_name) !context_name.nil? && context_defined?(context_name) end |
#validate(resource, context_name) ⇒ Object
Delegate #validate to RuleSet
49 50 51 |
# File 'lib/aequitas/contextual_rule_set.rb', line 49 def validate(resource, context_name) context(context_name).validate(resource) end |