Module: Lotus::Validations::AttributeDefiner::ClassMethods Private

Defined in:
lib/lotus/validations/attribute_definer.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Since:

  • 0.2.2

Instance Method Summary collapse

Instance Method Details

#_attribute(name, options = {}, &block) ⇒ 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.

Define an attribute

See Also:

  • Lotus::Validations::AttributeDefiner#attribute

Since:

  • 0.3.1



267
268
269
270
271
272
273
274
275
# File 'lib/lotus/validations/attribute_definer.rb', line 267

def _attribute(name, options = {}, &block)
  if block_given?
    define_nested_attribute(name, options, &block)
    validates(name, {})
  else
    define_attribute(name, options)
    validates(name, options)
  end
end

#attribute(name, options = {}, &block) ⇒ 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.

Define an attribute

value which coerces to TrueClass

Examples:

Attributes

require 'lotus/validations'

class Person
  include Lotus::Validations

  attribute :name
end

person = Person.new(name: 'Luca', age: 32)
person.name # => "Luca"
person.age  # => raises NoMethodError because `:age` wasn't defined as attribute.

Standard coercions

require 'lotus/validations'

class Person
  include Lotus::Validations

  attribute :fav_number, type: Integer
end

person = Person.new(fav_number: '23')
person.valid?

person.fav_number # => 23

Custom coercions

require 'lotus/validations'

class FavNumber
  def initialize(number)
    @number = number
  end
end

class BirthDate
end

class Person
  include Lotus::Validations

  attribute :fav_number, type: FavNumber
  attribute :date,       type: BirthDate
end

person = Person.new(fav_number: '23', date: 'Oct 23, 2014')
person.valid?

person.fav_number # => 23
person.date       # => this raises an error, because BirthDate#initialize doesn't accept any arg

Acceptance

require 'lotus/validations'

class Signup
  include Lotus::Validations

  attribute :terms_of_service, acceptance: true
end

 = Signup.new(terms_of_service: '1')
.valid? # => true

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

Confirmation

require 'lotus/validations'

class Signup
  include Lotus::Validations

  attribute :password, confirmation: true
end

 = Signup.new(password: 'secret', password_confirmation: 'secret')
.valid? # => true

 = Signup.new(password: 'secret', password_confirmation: 'x')
.valid? # => false

Exclusion

require 'lotus/validations'

class Signup
  include Lotus::Validations

  attribute :music, exclusion: ['pop']
end

 = Signup.new(music: 'rock')
.valid? # => true

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

Format

require 'lotus/validations'

class Signup
  include Lotus::Validations

  attribute :name, format: /\A[a-zA-Z]+\z/
end

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

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

Inclusion

require 'lotus/validations'

class Signup
  include Lotus::Validations

  attribute :age, inclusion: 18..99
end

 = Signup.new(age: 32)
.valid? # => true

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

Presence

require 'lotus/validations'

class Signup
  include Lotus::Validations

  attribute :name, presence: true
end

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

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

Size

require 'lotus/validations'

class Signup
  MEGABYTE = 1024 ** 2
  include Lotus::Validations

  attribute :ssn,      size: 11    # exact match
  attribute :password, size: 8..64 # range
  attribute :avatar,   size  1..(5 * MEGABYTE)
end

signup = Signup.new(password: 'a-very-long-password')
signup.valid? # => true

signup = Signup.new(password: 'short')
signup.valid? # => false

Parameters:

  • name (#to_sym)

    the name of the attribute

  • options (Hash) (defaults to: {})

    optional set of validations

Options Hash (options):

  • :type (Class)

    the Ruby type used to coerce the value

  • :acceptance (TrueClass, FalseClass)

    requires a non-empty

  • :confirmation (TrueClass, FalseClass)

    requires the value to be confirmed twice

  • :exclusion (#include?)

    requires the value NOT be included in the given collection

  • :format (Regexp)

    requires value to match the given Regexp

  • :inclusion (#include?)

    requires the value BE included in the given collection

  • :presence (TrueClass, FalseClass)

    requires the value be included in the given collection

  • :size (Numeric, Range)

    requires value’s to be equal or included by the given validator

Raises:

  • (ArgumentError)

    if an unknown or mispelled validation is given

Since:

  • 0.2.2



257
258
259
# File 'lib/lotus/validations/attribute_definer.rb', line 257

def attribute(name, options = {}, &block)
  _attribute(name, options, &block)
end

#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.2



283
284
285
# File 'lib/lotus/validations/attribute_definer.rb', line 283

def defined_attributes
  @defined_attributes ||= Set.new(super)
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.

Parameters:

  • base (Class)

    the target class

See Also:

Since:

  • 0.2.2



56
57
58
59
# File 'lib/lotus/validations/attribute_definer.rb', line 56

def included(base)
  super
  base.defined_attributes.merge(defined_attributes)
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 modules.

Parameters:

  • base (Class)

    the target class

See Also:

Since:

  • 0.2.2



69
70
71
72
# File 'lib/lotus/validations/attribute_definer.rb', line 69

def inherited(base)
  super
  base.defined_attributes.merge(defined_attributes)
end