Module: Aequitas

Defined in:
lib/aequitas.rb,
lib/aequitas/rule.rb,
lib/aequitas/macros.rb,
lib/aequitas/version.rb,
lib/aequitas/rule_set.rb,
lib/aequitas/violation.rb,
lib/aequitas/rule/guard.rb,
lib/aequitas/rule/block.rb,
lib/aequitas/exceptions.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/support/blank.rb,
lib/aequitas/class_methods.rb,
lib/aequitas/violation_set.rb,
lib/aequitas/rule/presence.rb,
lib/aequitas/violation/rule.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/violation/message.rb,
lib/aequitas/rule/length/range.rb,
lib/aequitas/rule/numericalness.rb,
lib/aequitas/rule/absence/blank.rb,
lib/aequitas/virtus_integration.rb,
lib/aequitas/rule/format/regexp.rb,
lib/aequitas/rule/primitive_type.rb,
lib/aequitas/message_transformer.rb,
lib/aequitas/rule/length/maximum.rb,
lib/aequitas/contextual_rule_set.rb,
lib/aequitas/rule/skip_condition.rb,
lib/aequitas/rule/length/minimum.rb,
lib/aequitas/support/ordered_hash.rb,
lib/aequitas/rule/value/less_than.rb,
lib/aequitas/support/value_object.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,
lib/aequitas/virtus_integration/inline_attribute_rule_extractor.rb,
lib/aequitas/virtus_integration/inline_attribute_rule_extractor/array.rb,
lib/aequitas/virtus_integration/inline_attribute_rule_extractor/string.rb,
lib/aequitas/virtus_integration/inline_attribute_rule_extractor/object.rb,
lib/aequitas/virtus_integration/inline_attribute_rule_extractor/numeric.rb,
lib/aequitas/virtus_integration/inline_attribute_rule_extractor/boolean.rb

Defined Under Namespace

Modules: ClassMethods, Macros, Undefined, ValueObject, VirtusIntegration Classes: ContextualRuleSet, InvalidContextError, MessageTransformer, OrderedHash, Rule, RuleSet, UnknownValidationFormat, Violation, ViolationSet

Constant Summary

VERSION =
"0.0.2"

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Boolean) blank?(value)

Determines whether the specified value is blank.

An object is blank if it's false, empty, or a whitespace string. For example, "", " ", nil, [], and {} are blank.

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/aequitas/support/blank.rb', line 8

def self.blank?(value)
  case value
  when ::NilClass, ::FalseClass
    true
  when ::TrueClass, ::Numeric
    false
  when ::Array, ::Hash
    value.empty?
  when ::String
    value !~ /\S/
  else
    value.nil? || (value.respond_to?(:empty?) && value.empty?)
  end
end

+ (Object) included(base)



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

def self.included(base)
  super
  base.extend ClassMethods
end

Instance Method Details

- (Symbol) default_validation_context

The name of the default validation context for this Resource.

Overriding #default_validation_context .

Returns:

  • (Symbol)

    the current validation context from the context stack (if valid for this model), or :default



63
64
65
# File 'lib/aequitas.rb', line 63

def default_validation_context
  :default
end

- (ViolationSet) errors

Returns the collection of current validation errors for this resource

Returns:

  • (ViolationSet)

    the collection of current validation errors for this resource



31
32
33
# File 'lib/aequitas.rb', line 31

def errors
  @errors ||= ViolationSet.new(self)
end

- (Boolean) valid?(context_name = default_validation_context)

Check if a resource is valid in a given context

Returns:

  • (Boolean)


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

def valid?(context_name = default_validation_context)
  validate(context_name).errors.empty?
end

- (Object) validate(context_name = default_validation_context)

Command a resource to populate its ViolationSet with any violations of its validation Rules in context_name



39
40
41
42
43
44
45
# File 'lib/aequitas.rb', line 39

def validate(context_name = default_validation_context)
  # TODO: errors.replace(validation_violations(context_name))
  errors.clear
  validation_violations(context_name).each { |v| errors.add(v) }

  self
end

- (Object) validation_attribute_value(attribute_name)

Retrieve the value of the given property name for the purpose of validation

Defaults to sending the attribute name arg to the receiver and using the resulting value as the attribute value for validation

Parameters:

  • attribute_name (Symbol)

    the name of the attribute for which to retrieve the attribute value for validation.

Returns:

  • (Object)

    the value of the attribute identified by attribute_name for the purpose of validation



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

def validation_attribute_value(attribute_name)
  __send__(attribute_name) if respond_to?(attribute_name, true)
end

- (ContextualRuleSet) validation_rules

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:



70
71
72
# File 'lib/aequitas.rb', line 70

def validation_rules
  self.class.validation_rules
end

- (Object) validation_violations(context_name = default_validation_context)

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.

Get a list of violations for the receiver without mutating it



50
51
52
# File 'lib/aequitas.rb', line 50

def validation_violations(context_name = default_validation_context)
  validation_rules.validate(self, context_name)
end