Class: Aequitas::Rule

Inherits:
Object
  • Object
show all
Extended by:
ValueObject
Defined in:
lib/aequitas/rule.rb,
lib/aequitas/rule/block.rb,
lib/aequitas/rule/guard.rb,
lib/aequitas/rule/value.rb,
lib/aequitas/rule/format.rb,
lib/aequitas/rule/length.rb,
lib/aequitas/rule/method.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/absence/nil.rb,
lib/aequitas/rule/format/proc.rb,
lib/aequitas/rule/value/equal.rb,
lib/aequitas/rule/value/range.rb,
lib/aequitas/rule/confirmation.rb,
lib/aequitas/rule/length/equal.rb,
lib/aequitas/rule/length/range.rb,
lib/aequitas/rule/absence/blank.rb,
lib/aequitas/rule/format/regexp.rb,
lib/aequitas/rule/numericalness.rb,
lib/aequitas/rule/length/maximum.rb,
lib/aequitas/rule/length/minimum.rb,
lib/aequitas/rule/primitive_type.rb,
lib/aequitas/rule/skip_condition.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/presence/not_blank.rb,
lib/aequitas/rule/value/greater_than.rb,
lib/aequitas/rule/format/email_address.rb,
lib/aequitas/rule/numericalness/integer.rb,
lib/aequitas/rule/primitive_type/virtus.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



103
104
105
# File 'lib/aequitas/rule.rb', line 103

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)


84
85
86
# File 'lib/aequitas/rule.rb', line 84

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.



99
100
101
# File 'lib/aequitas/rule.rb', line 99

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
# File 'lib/aequitas/rule.rb', line 74

def validate(resource)
  if valid?(resource)
    nil
  else
    Violation.new(resource, custom_message, self)
  end
end

#violation_data(resource) ⇒ 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.



118
119
120
# File 'lib/aequitas/rule.rb', line 118

def violation_data(resource)
  [ ]
end

#violation_info(resource) ⇒ 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.



108
109
110
# File 'lib/aequitas/rule.rb', line 108

def violation_info(resource)
  Hash[ violation_data(resource) ]
end

#violation_values(resource) ⇒ 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.



113
114
115
# File 'lib/aequitas/rule.rb', line 113

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