Module: Aequitas

Defined in:
lib/aequitas/virtus_integration.rb,
lib/aequitas.rb,
lib/aequitas/rule.rb,
lib/aequitas/macros.rb,
lib/aequitas/context.rb,
lib/aequitas/version.rb,
lib/aequitas/rule_set.rb,
lib/aequitas/violation.rb,
lib/aequitas/exceptions.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/class_methods.rb,
lib/aequitas/rule/presence.rb,
lib/aequitas/support/blank.rb,
lib/aequitas/violation_set.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/contextual_rule_set.rb,
lib/aequitas/message_transformer.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/support/ordered_hash.rb,
lib/aequitas/support/value_object.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,
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/object.rb,
lib/aequitas/virtus_integration/inline_attribute_rule_extractor/string.rb,
lib/aequitas/virtus_integration/inline_attribute_rule_extractor/boolean.rb,
lib/aequitas/virtus_integration/inline_attribute_rule_extractor/numeric.rb

Overview

module Virtus

class Attribute
  Object.accept_options  :required# , :prohibited, :forbidden # hmm...
  String.accept_options  :length, :format
  Decimal.accept_options :precision, :scale
  Float.accept_options   :precision, :scale
end

end

Defined Under Namespace

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

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.blank?(value) ⇒ Boolean

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

.included(base) ⇒ Object



10
11
12
13
# File 'lib/aequitas.rb', line 10

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

Instance Method Details

#default_validation_contextSymbol

The default validation context for this Resource. This Resource’s default context can be overridden by implementing #default_validation_context

Returns:

  • (Symbol)

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



58
59
60
# File 'lib/aequitas.rb', line 58

def default_validation_context
  validation_rules.current_context
end

#errorsViolationSet

Returns the collection of current validation errors for this resource.

Returns:

  • (ViolationSet)

    the collection of current validation errors for this resource



45
46
47
# File 'lib/aequitas.rb', line 45

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

#valid?(context_name = default_validation_context) ⇒ Boolean

Check if a resource is valid in a given context

Returns:

  • (Boolean)


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

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

#validate(context_name = default_validation_context) ⇒ Object

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



26
27
28
29
30
31
32
# File 'lib/aequitas.rb', line 26

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

#validation_attribute_value(attribute_name) ⇒ Object

Retrieve the value of the given property name for the purpose of validation. Default implementation is to send the attribute name arg to the receiver and use 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.



76
77
78
# File 'lib/aequitas.rb', line 76

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

#validation_rulesObject

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.



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

def validation_rules
  self.class.validation_rules
end

#validation_violations(context_name = default_validation_context) ⇒ 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.

Get a list of violations for the receiver without mutating it



37
38
39
# File 'lib/aequitas.rb', line 37

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