Class: Aequitas::ContextualRuleSet

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

Instance Attribute Summary collapse

Attributes included from Equalizable

#equalizer

Instance Method Summary collapse

Methods included from Equalizable

equalize_on

Constructor Details

#initializeContextualRuleSet

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_setsObject (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

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



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

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: :maximum=>50, :allow_nil=>true, :message=>nil

  • [Symbol] (Hash)

    a customizable set of options

Returns:

  • (self)


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, 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

#add_rules_to_context(context_name, rules) ⇒ self

Define a context and append rules to it

Parameters:

  • context_name (Symbol)

    name of the context to define and append rules to

  • rules (RuleSet, Array)

    Rules to append to context_name

Returns:

  • (self)


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?

Parameters:

  • context (Symbol)

    the context to test

Raises:



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

Parameters:

  • other (ContextualRuleSet)

    the ContextualRuleSet whose rules are to be assimilated

Returns:

  • (self)


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

Parameters:

  • name (String)

    Context name for which to return a RuleSet

Returns:

  • (RuleSet)

    RuleSet for the given context



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

Returns:

  • (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_contextSymbol

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

Returns:

  • (Symbol)

    the current validation context from the stack (if valid for this model), nil if no context name is on the stack and no contexts are defined for this model, or :default if the context on the stack is invalid for this model or no context is on the stack and this model has at least one validation context



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

Parameters:

  • context (Symbol)

    the context to test

Returns:

  • (Boolean)

    true if the context is valid



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