Module: Lotus::Validations::ClassMethods

Defined in:
lib/lotus/validations.rb

Overview

Validations DSL

Since:

  • 0.1.0

Instance Method Summary collapse

Instance Method Details

#defined_attributesArray<String>

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.

Set of user defined attributes

Returns:

  • (Array<String>)

Since:

  • 0.2.3



135
136
137
# File 'lib/lotus/validations.rb', line 135

def defined_attributes
  validations.names.map(&:to_s)
end

#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. When a module includes Lotus::Validations and it is included in a class or module, this passes the validations from the module to the base.

Examples:

require 'lotus/validations'

module NameValidations
  include Lotus::Validations

  attribute :name, presence: true
end

class Signup
  include NameValidations
end

 = Signup.new(name: '')
.valid? # => false

 = Signup.new(name: 'Luca')
.valid? # => true

Parameters:

  • base (Class)

    the target action

See Also:

Since:

  • 0.1.0



78
79
80
81
82
83
84
85
86
# File 'lib/lotus/validations.rb', line 78

def included(base)
  base.class_eval do
    include Lotus::Validations
  end

  super

  transfer_validations_to_base(base)
end

#inherited(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 class inheritance. When a class includes Lotus::Validations and it is subclassed, this passes the attributes from the superclass to the subclass.

Parameters:

  • base (Class)

    the target action

See Also:

Since:

  • 0.2.2



44
45
46
47
# File 'lib/lotus/validations.rb', line 44

def inherited(base)
  transfer_validations_to_base(base)
  super
end

#validates(name, options) ⇒ Object

Define a validation for an existing attribute

Examples:

Presence

require 'lotus/validations'

class Signup
  include Lotus::Validations

  def initialize(attributes = {})
    @name = attributes.fetch(:name)
  end

  attr_accessor :name

  validates :name, presence: true
end

 = Signup.new(name: 'Luca')
.valid? # => true

 = Signup.new(name: nil)
.valid? # => false

Parameters:

  • name (#to_sym)

    the name of the attribute

  • options (Hash)

    set of validations

See Also:

Since:

  • 0.1.0



115
116
117
# File 'lib/lotus/validations.rb', line 115

def validates(name, options)
  validations.add(name, options)
end

#validationsHash

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.

Set of user defined validations

Returns:

  • (Hash)

Since:

  • 0.2.2



125
126
127
# File 'lib/lotus/validations.rb', line 125

def validations
  @validations ||= ValidationSet.new
end