Class: Aequitas::ContextualRuleSet

Inherits:
Object
  • Object
show all
Extended by:
ValueObject, Forwardable
Includes:
Enumerable
Defined in:
lib/aequitas/contextual_rule_set.rb

Instance Attribute Summary collapse

Attributes included from ValueObject

#equalizer

Instance Method Summary collapse

Methods included from ValueObject

equalize_on

Constructor Details

#initializeContextualRuleSet

Returns 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

#rule_setsObject (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

#transformerMessageTransformer?

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.

Returns:



34
35
36
# File 'lib/aequitas/contextual_rule_set.rb', line 34

def transformer
  @transformer
end

Instance Method Details

#[](attribute_name) ⇒ Array

Retrieve Rules applicable to a given attribute name

Parameters:

  • attribute_name (Symbol)

    name of the attribute for which to retrieve applicable Rules

Returns:

  • (Array)

    list of Rules applicable to attribute_name in the default context



87
88
89
# File 'lib/aequitas/contextual_rule_set.rb', line 87

def [](attribute_name)
  context(default_context_name).fetch(attribute_name, [])
end

#add(rule_class, attribute_names, options = {}, &block) ⇒ self

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.

Parameters:

  • rule_class (Aequitas::Rule)

    Rule class, example: Aequitas::Rule::Presence

  • attribute_names (Array<Symbol>)

    Attribute names given to validation macro, example:

    :first_name, :last_name

    in validates_presence_of :first_name, :last_name

  • options (Hash) (defaults to: {})

    Options supplied to validation macro, example: :allow_nil=>true

  • [Symbol] (Hash)

    a customizable set of options

Returns:

  • (self)


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, options = {}, &block)
  context_names = extract_context_names(options)

  attribute_names.each do |attribute_name|
    rules = rule_class.rules_for(attribute_name, options, &block)

    context_names.each { |context| add_rules_to_context(context, rules) }
  end

  self
end

#concat(other) ⇒ self

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

Parameters:

  • other (ContextualRuleSet)

    the ContextualRuleSet whose rules are to be assimilated

Returns:

  • (self)


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

#context(context_name) ⇒ RuleSet

Return the RuleSet for a given context name

Parameters:

  • context_name (Symbol)

    Context name for which to return a RuleSet

Returns:

  • (RuleSet)

    RuleSet for the given context



74
75
76
# File 'lib/aequitas/contextual_rule_set.rb', line 74

def context(context_name)
  rule_sets.fetch(context_name)
end

#validate(resource, context_name = default_context_name) ⇒ ViolationSet

Delegate #validate to the appropriate RuleSet

Returns:

  • (ViolationSet)

    the collection of Violations applicable to the given resource



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