Class: Aequitas::Rule

Inherits:
Object
  • Object
show all
Extended by:
ValueObject
Defined in:
lib/aequitas/rule.rb,
lib/aequitas/rule/guard.rb,
lib/aequitas/rule/block.rb,
lib/aequitas/rule/value.rb,
lib/aequitas/rule/method.rb,
lib/aequitas/rule/length.rb,
lib/aequitas/rule/format.rb,
lib/aequitas/rule/within.rb,
lib/aequitas/rule/absence.rb,
lib/aequitas/rule/presence.rb,
lib/aequitas/rule/acceptance.rb,
lib/aequitas/rule/format/url.rb,
lib/aequitas/rule/value/equal.rb,
lib/aequitas/rule/format/proc.rb,
lib/aequitas/rule/absence/nil.rb,
lib/aequitas/rule/value/range.rb,
lib/aequitas/rule/length/equal.rb,
lib/aequitas/rule/confirmation.rb,
lib/aequitas/rule/length/range.rb,
lib/aequitas/rule/numericalness.rb,
lib/aequitas/rule/absence/blank.rb,
lib/aequitas/rule/format/regexp.rb,
lib/aequitas/rule/primitive_type.rb,
lib/aequitas/rule/length/maximum.rb,
lib/aequitas/rule/skip_condition.rb,
lib/aequitas/rule/length/minimum.rb,
lib/aequitas/rule/value/less_than.rb,
lib/aequitas/rule/value/not_equal.rb,
lib/aequitas/rule/presence/not_nil.rb,
lib/aequitas/rule/value/greater_than.rb,
lib/aequitas/rule/presence/not_blank.rb,
lib/aequitas/rule/format/email_address.rb,
lib/aequitas/rule/primitive_type/virtus.rb,
lib/aequitas/rule/numericalness/integer.rb,
lib/aequitas/rule/value/less_than_or_equal.rb,
lib/aequitas/rule/numericalness/non_integer.rb,
lib/aequitas/rule/value/greater_than_or_equal.rb

Defined Under Namespace

Classes: Absence, Acceptance, Block, Confirmation, Format, Guard, Length, Method, Numericalness, Presence, PrimitiveType, SkipCondition, Value, Within

Instance Attribute Summary collapse

Attributes included from ValueObject

#equalizer

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ValueObject

equalize_on

Constructor Details

#initialize(attribute_name, options = {}) ⇒ Rule

Construct a validator. Capture the :if and :unless clauses when present.

TODO: remove Hash as a value for :message

(see Violation#[] in backwards.rb)

Parameters:

  • attribute_name (String, Symbol)

    The name of the attribute to validate.

  • [String, (Hash)

    a customizable set of options

  • [Symbol, (Hash)

    a customizable set of options

  • [Boolean] (Hash)

    a customizable set of options



59
60
61
62
63
64
# File 'lib/aequitas/rule.rb', line 59

def initialize(attribute_name, options = {})
  @attribute_name = attribute_name
  @custom_message = options.fetch(:message, nil)
  @guard          = options.fetch(:guard)          { Guard.new(options) }
  @skip_condition = options.fetch(:skip_condition) { SkipCondition.new(options) }
end

Instance Attribute Details

#attribute_nameObject (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.



16
17
18
# File 'lib/aequitas/rule.rb', line 16

def attribute_name
  @attribute_name
end

#custom_messageObject (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.



18
19
20
# File 'lib/aequitas/rule.rb', line 18

def custom_message
  @custom_message
end

#guardObject (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.



20
21
22
# File 'lib/aequitas/rule.rb', line 20

def guard
  @guard
end

#skip_conditionObject (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.



22
23
24
# File 'lib/aequitas/rule.rb', line 22

def skip_condition
  @skip_condition
end

Class Method Details

.rules_for(attribute_name, options, &block) ⇒ #each(Rule)

Get the validators for the given attribute_name and options

Parameters:

  • attribute_name (Symbol)

    the name of the attribute to which the returned validators will be bound

  • options (Hash)

    the options with which to configure the returned validators

Returns:

  • (#each(Rule))

    a collection of validators which collectively



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

def self.rules_for(attribute_name, options, &block)
  Array(new(attribute_name, options, &block))
end

Instance Method Details

#attribute_value(resource) ⇒ Object



105
106
107
# File 'lib/aequitas/rule.rb', line 105

def attribute_value(resource)
  resource.validation_attribute_value(attribute_name)
end

#execute?(resource) ⇒ Boolean

Determines if this validator should be run against the resource by delegating to the guard configured for this rule

Returns:

  • (Boolean)


86
87
88
# File 'lib/aequitas/rule.rb', line 86

def execute?(resource)
  guard.allow?(resource)
end

#skip?(value) ⇒ 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 the value to see if it is blank or nil, and if it is allowed. Note that allowing blank without explicitly denying nil allows nil values, since nil.blank? is true.

Parameters:

  • value (Object)

    The value to test.

Returns:

  • (Boolean)

    true if blank/nil is allowed, and the value is blank/nil.



101
102
103
# File 'lib/aequitas/rule.rb', line 101

def skip?(value)
  skip_condition.skip?(value)
end

#validate(resource) ⇒ NilClass, Violation

Validate the resource arg against this Rule

Parameters:

  • resource (Object)

    the target object to be validated

Returns:

  • (NilClass, Violation)

    NilClass if resource is valid Violation with additional info if resource is invalid



74
75
76
77
78
79
80
81
82
# File 'lib/aequitas/rule.rb', line 74

def validate(resource)
  value = attribute_value(resource)

  if skip?(value) || valid_value?(value)
    nil
  else
    new_violation(resource, value)
  end
end

#violation_dataObject

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.



120
121
122
# File 'lib/aequitas/rule.rb', line 120

def violation_data
  [ ]
end

#violation_infoObject

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.



110
111
112
# File 'lib/aequitas/rule.rb', line 110

def violation_info
  Hash[ violation_data ]
end

#violation_valuesObject

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.



115
116
117
# File 'lib/aequitas/rule.rb', line 115

def violation_values
  violation_data.map { |(_, value)| value }
end