Module: Lotus::Validations

Defined in:
lib/lotus/validations.rb,
lib/lotus/validations/error.rb,
lib/lotus/validations/errors.rb,
lib/lotus/validations/version.rb,
lib/lotus/validations/attribute.rb,
lib/lotus/validations/coercions.rb,
lib/lotus/validations/validator.rb,
lib/lotus/validations/validation_set.rb,
lib/lotus/validations/attribute_definer.rb,
lib/lotus/validations/nested_attributes.rb,
lib/lotus/validations/blank_value_checker.rb

Overview

Lotus::Validations is a set of lightweight validations for Ruby objects.

Since:

  • 0.1.0

Defined Under Namespace

Modules: AttributeDefiner, ClassMethods, Coercions Classes: Attribute, BlankValueChecker, Error, Errors, NestedAttributes, ValidationSet, Validator

Constant Summary collapse

VERSION =

Since:

  • 0.1.0

'0.4.0'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ 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.

Override Ruby’s hook for modules.

Parameters:

  • base (Class)

    the target action

See Also:

Since:

  • 0.1.0



23
24
25
26
27
28
# File 'lib/lotus/validations.rb', line 23

def self.included(base)
  base.class_eval do
    extend ClassMethods
    include AttributeDefiner
  end
end

Instance Method Details

#each(&blk) {|attribute, value| ... } ⇒ Object

Iterates thru the defined attributes and their values

Parameters:

  • blk (Proc)

    a block

Yield Parameters:

  • attribute (Symbol)

    the name of the attribute

  • value (Object, nil)

    the value of the attribute

Since:

  • 0.2.0



265
266
267
# File 'lib/lotus/validations.rb', line 265

def each(&blk)
  to_h.each(&blk)
end

#errorsLotus::Validations::Errors

Validation errors

Examples:

Valid attributes

require 'lotus/validations'

class Signup
  include Lotus::Validations

  attribute :email, presence: true, format: /\A(.*)@(.*)\.(.*)\z/
end

 = Signup.new(email: '[email protected]')
.valid? # => true

.errors
  # => #<Lotus::Validations::Errors:0x007fd594ba9228 @errors={}>

Invalid attributes

require 'lotus/validations'

class Signup
  include Lotus::Validations

  attribute :email, presence: true, format: /\A(.*)@(.*)\.(.*)\z/
  attribute :age, size: 18..99
end

 = Signup.new(email: '', age: 17)
.valid? # => false

.errors
  # => #<Lotus::Validations::Errors:0x007fe00ced9b78
  # @errors={
  #   :email=>[
  #     #<Lotus::Validations::Error:0x007fe00cee3290 @attribute=:email, @validation=:presence, @expected=true, @actual="">,
  #     #<Lotus::Validations::Error:0x007fe00cee31f0 @attribute=:email, @validation=:format, @expected=/\A(.*)@(.*)\.(.*)\z/, @actual="">
  #   ],
  #   :age=>[
  #     #<Lotus::Validations::Error:0x007fe00cee30d8 @attribute=:age, @validation=:size, @expected=18..99, @actual=17>
  #   ]
  # }>

Invalid attributes

require 'lotus/validations'

class Post
  include Lotus::Validations

  attribute :title, presence: true
end

post = Post.new
post.invalid? # => true

post.errors
  # => #<Lotus::Validations::Errors:0x2931522b
  # @errors={
  #   :title=>[
  #     #<Lotus::Validations::Error:0x662706a7 @actual=nil, @attribute_name="title", @validation=:presence, @expected=true, @namespace=nil, @attribute="title">
  #   ]
  # }>

Returns:

See Also:

Since:

  • 0.1.0



221
222
223
# File 'lib/lotus/validations.rb', line 221

def errors
  @errors ||= Errors.new
end

#invalid?TrueClass, FalseClass

Checks if the current data doesn’t satisfies the defined validations

Returns:

  • (TrueClass, FalseClass)

    the result of the validations

Since:

  • 0.3.2



241
242
243
# File 'lib/lotus/validations.rb', line 241

def invalid?
  !valid?
end

#to_hHash

Returns a Hash with the defined attributes as symbolized keys, and their relative values.

Returns:

  • (Hash)

Since:

  • 0.1.0



275
276
277
278
279
280
# File 'lib/lotus/validations.rb', line 275

def to_h
  # TODO remove this symbolization when we'll support Ruby 2.2+ only
  Utils::Hash.new(
    @attributes
  ).deep_dup.symbolize!.to_h
end

#valid?TrueClass, FalseClass

Checks if the current data satisfies the defined validations

Returns:

  • (TrueClass, FalseClass)

    the result of the validations

Since:

  • 0.1.0



230
231
232
233
234
# File 'lib/lotus/validations.rb', line 230

def valid?
  validate

  errors.empty?
end

#validateErrors

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.

Validates the object.

Returns:

See Also:

  • Attribute#nested

Since:

  • 0.2.4



253
254
255
256
# File 'lib/lotus/validations.rb', line 253

def validate
  validator = Validator.new(defined_validations, read_attributes, errors)
  validator.validate
end