module Shamu::Attributes::Validation

Defines an interface for entities that report validation failures with respect to their attributes.

Public Instance Methods

attribute( name, *args, **options, &block ) click to toggle source

Adds validation options to {Attributes::DSL#attribute}. Any option not recognized by one of the Attributes mixins will be used as validation arguments for the given attribute.

@overload attribute( name, build, **options ) @param (see Attributes::Assignment::DSL#attribute) @return (see Attributes::Assignment::DSL#attribute)

@example

attribute :email, presence: true

# Results in
attribute :email
validates :email, presence: true
Calls superclass method
# File lib/shamu/attributes/validation.rb, line 45
def attribute( name, *args, **options, &block )
  super

  validation_options = options.each_with_object({}) do |(key, value), opts|
    next if attribute_option_keys.include?( key )

    validator = "#{ key.to_s.camelize }Validator"
    key = "shamu/attributes/validators/#{ key }" if Shamu::Attributes::Validators.const_defined?( validator.to_sym ) # rubocop:disable Metrics/LineLength
    opts[ key ] = value
  end
  validates name, validation_options if validation_options.any?
end
valid?() click to toggle source

@return [Boolean] if the object is free from validation errors. Must

call {#validate} before checking.
# File lib/shamu/attributes/validation.rb, line 20
def valid?
  errors.blank?
end
validated?() click to toggle source

@return [Boolean] true if the object has been validated at least once.

# File lib/shamu/attributes/validation.rb, line 25
def validated?
  @validated
end