Module: Aequitas::Macros

Included in:
ClassMethods
Defined in:
lib/aequitas/macros.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extract_options(arguments) ⇒ Object



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

def self.extract_options(arguments)
  arguments.last.kind_of?(Hash) ? arguments.pop : {}
end

Instance Method Details

#validates_absence_of(*attribute_names) ⇒ Object

Note:

dm-core’s support lib adds the #blank? method to many classes,

Validates that the specified attribute is “blank” via the attribute’s #blank? method.

Examples:

Usage
require 'virtus'
require 'aequitas'

class Page
  include Virtus
  include Aequitas

  attribute :unwanted_attribute, String
  attribute :another_unwanted, String
  attribute :yet_again, String

  validates_absence_of :unwanted_attribute
  validates_absence_of :another_unwanted, :yet_again

  # a call to #validate will return false unless
  # all three attributes are blank
end

See Also:

  • (dm-core) for more information.


49
50
51
52
# File 'lib/aequitas/macros.rb', line 49

def validates_absence_of(*attribute_names)
  options = Macros.extract_options(attribute_names)
  validation_rules.add(Rule::Absence::Blank, attribute_names, options)
end

#validates_acceptance_of(*attribute_names) ⇒ Object

Validates that the attributes’s value is in the set of accepted values.

Examples:

Usage

require 'virtus'
require 'aequitas'

class Page
  include Virtus
  include Aequitas

  attribute :license_agreement_accepted, String
  attribute :terms_accepted, String

  validates_acceptance_of :license_agreement, :accept => "1"
  validates_acceptance_of :terms_accepted, :allow_nil => false

  # a call to valid? will return false unless:
  # license_agreement is nil or "1"
  # and
  # terms_accepted is one of ["1", 1, "true", true, "t"]
end

Parameters:

  • [Boolean] (Hash)

    a customizable set of options

  • [Array] (Hash)

    a customizable set of options



83
84
85
86
# File 'lib/aequitas/macros.rb', line 83

def validates_acceptance_of(*attribute_names)
  options = Macros.extract_options(attribute_names)
  validation_rules.add(Rule::Acceptance, attribute_names, options)
end

#validates_confirmation_of(*attribute_names) ⇒ Object

Validates that the given attribute is confirmed by another attribute. A common use case scenario is when you require a user to confirm their password, for which you use both password and password_confirmation attributes.

Examples:

Usage

require 'virtus'
require 'aequitas'

class Page
  include Virtus
  include Aequitas

  attribute :password, String
  attribute :email, String

  attr_accessor :password_confirmation
  attr_accessor :email_repeated

  validates_confirmation_of :password
  validates_confirmation_of :email, :confirm => :email_repeated

  # a call to valid? will return false unless:
  # password == password_confirmation
  # and
  # email == email_repeated
end

Parameters:

  • [Boolean] (Hash)

    a customizable set of options

  • [Symbol] (Hash)

    a customizable set of options



125
126
127
128
# File 'lib/aequitas/macros.rb', line 125

def validates_confirmation_of(*attribute_names)
  options = Macros.extract_options(attribute_names)
  validation_rules.add(Rule::Confirmation, attribute_names, options)
end

#validates_format_of(*attribute_names) ⇒ Object

Validates that the attribute is in the specified format. You may use the :as (or :with, it’s an alias) option to specify the pre-defined format that you want to validate against. You may also specify your own format via a Proc or Regexp passed to the the :as or :with options.

Examples:

Usage

require 'virtus'
require 'aequitas'

class Page
  include Virtus
  include Aequitas

  attribute :email, String
  attribute :zip_code, String

  validates_format_of :email, :as => :email_address
  validates_format_of :zip_code, :with => /^\d{5}$/

  # a call to valid? will return false unless:
  # email is formatted like an email address
  # and
  # zip_code is a string of 5 digits
end

Parameters:

  • [Boolean] (Hash)

    a customizable set of options

  • [Format, (Hash)

    a customizable set of options



171
172
173
174
# File 'lib/aequitas/macros.rb', line 171

def validates_format_of(*attribute_names)
  options = Macros.extract_options(attribute_names)
  validation_rules.add(Rule::Format, attribute_names, options)
end

#validates_length_of(*attribute_names) ⇒ Object

Validates that the length of the attribute is equal to, less than, greater than or within a certain range (depending upon the options you specify).

Examples:

Usage

require 'virtus'
require 'aequitas'

class Page
  include Virtus
  include Aequitas

  attribute :high,       Integer
  attribute :low,        Integer
  attribute :just_right, Integer

  validates_length_of :high, :min => 100000000000
  validates_length_of :low, :equals => 0
  validates_length_of :just_right, :within => 1..10

  # a call to valid? will return false unless:
  # high is greater than or equal to 100000000000
  # low is equal to 0
  # just_right is between 1 and 10 (inclusive of both 1 and 10)
end

Parameters:

  • [Boolean] (Hash)

    a customizable set of options

  • [Range] (Hash)

    a customizable set of options



235
236
237
238
# File 'lib/aequitas/macros.rb', line 235

def validates_length_of(*attribute_names)
  options = Macros.extract_options(attribute_names)
  validation_rules.add(Rule::Length, attribute_names, options)
end

#validates_numericalness_of(*attribute_names) ⇒ Object

Validate whether a field is numeric.

Parameters:

  • [Boolean] (Hash)

    a customizable set of options

  • [String] (Hash)

    a customizable set of options

  • [Numeric] (Hash)

    a customizable set of options



280
281
282
283
284
# File 'lib/aequitas/macros.rb', line 280

def validates_numericalness_of(*attribute_names)
  options = Macros.extract_options(attribute_names)
  validation_rules.add(Rule::Value,         attribute_names, options)
  validation_rules.add(Rule::Numericalness, attribute_names, options)
end

#validates_presence_of(*attribute_names) ⇒ Object

Note:

dm-core’s support lib adds the blank? method to many classes,

Validates that the specified attribute is present.

For most property types “being present” is the same as being “not blank” as determined by the attribute’s #blank? method. However, in the case of Boolean, “being present” means not nil; i.e. true or false.

Examples:

Usage

require 'virtus'
require 'aequitas'

class Page
  include Virtus
  include Aequitas

  attribute :required_attribute, String
  attribute :another_required,   String
  attribute :yet_again,          String

  validates_presence_of :required_attribute
  validates_presence_of :another_required, :yet_again

  # a call to valid? will return false unless
  # all three attributes are not blank (according to Aequitas.blank?)
end

See Also:

  • (dm-core) for more information.


316
317
318
319
# File 'lib/aequitas/macros.rb', line 316

def validates_presence_of(*attribute_names)
  options = Macros.extract_options(attribute_names)
  validation_rules.add(Rule::Presence::NotBlank, attribute_names, options)
end

#validates_primitive_type_of(*attribute_names) ⇒ Object

Validates that the specified attribute is of the correct primitive type.

Examples:

Usage

require 'virtus'
require 'aequitas'

class Person
  include Virtus
  include Aequitas

  attribute :birth_date, Date

  validates_primitive_type_of :birth_date

  # a call to valid? will return false unless
  # the birth_date is something that can be properly
  # casted into a Date object.
end


340
341
342
343
# File 'lib/aequitas/macros.rb', line 340

def validates_primitive_type_of(*attribute_names)
  options = Macros.extract_options(attribute_names)
  validation_rules.add(Rule::PrimitiveType, attribute_names, options)
end

#validates_value_of(*attribute_names) ⇒ Object



345
346
347
348
# File 'lib/aequitas/macros.rb', line 345

def validates_value_of(*attribute_names)
  options = Macros.extract_options(attribute_names)
  validation_rules.add(Rule::Value, attribute_names, options)
end

#validates_with_block(*attribute_names, &block) ⇒ Object

Validate using the given block. The block given needs to return:

result::<Boolean>, Error Message::<String>

Examples:

Usage
require 'virtus'
require 'aequitas'

class Page
  include Virtus
  include Aequitas

  attribute :zip_code, String

  validates_with_block do
    if @zip_code == "94301"
      true
    else
      [false, "You're in the wrong zip code"]
    end
  end

  # A call to valid? will return false and
  # populate the object's errors with "You're in the
  # wrong zip code" unless zip_code == "94301"

  # You can also specify field:

  validates_with_block :zip_code do
    if @zip_code == "94301"
      true
    else
      [false, "You're in the wrong zip code"]
    end
  end

  # it will add returned error message to :zip_code field
end


420
421
422
423
424
425
426
427
# File 'lib/aequitas/macros.rb', line 420

def validates_with_block(*attribute_names, &block)
  unless block_given?
    raise ArgumentError, 'You need to pass a block to validates_with_block'
  end

  options = Macros.extract_options(attribute_names)
  validation_rules.add(Rule::Block, attribute_names, options, &block)
end

#validates_with_method(*attribute_names) ⇒ Object

Validate using method called on validated object. The method must to return either true, or a pair of [false, error message string], and is specified as a symbol passed with :method option.

This validator does support multiple attribute_names being specified at a time, but we encourage you to use it with one property/method at a time.

Real world experience shows that method validation is often useful when attribute needs to be virtual and not a property name.

Examples:

Usage

require 'virtus'
require 'aequitas'

class Page
  include Virtus
  include Aequita

  attribute :zip_code, String

  validates_with_method :zip_code,
                       :method => :in_the_right_location?

  def in_the_right_location?
    if @zip_code == "94301"
      return true
    else
      return [false, "You're in the wrong zip code"]
    end
  end

  # A call to #valid? will return false and
  # populate the object's errors with "You're in the
  # wrong zip code" unless zip_code == "94301"
end


466
467
468
469
# File 'lib/aequitas/macros.rb', line 466

def validates_with_method(*attribute_names)
  options = Macros.extract_options(attribute_names)
  validation_rules.add(Rule::Method, attribute_names, options)
end

#validates_within(*attribute_names) ⇒ Object

Validates that the value of a field is within a range/set.

This validation is defined by passing a field along with a :set parameter. The :set can be a Range or any object which responds to the #include? method (an array, for example).

Examples:

Usage

require 'virtus'
require 'aequitas'

class Review
  include Virtus
  include Aequitas

  STATES = ['new', 'in_progress', 'published', 'archived']

  attribute :title, String
  attribute :body, String
  attribute :review_state, String
  attribute :rating, Integer

  validates_within :review_state, :set => STATES
  validates_within :rating,       :set => 1..5

  # a call to valid? will return false unless
  # the two properties conform to their sets
end


377
378
379
380
# File 'lib/aequitas/macros.rb', line 377

def validates_within(*attribute_names)
  options = Macros.extract_options(attribute_names)
  validation_rules.add(Rule::Within, attribute_names, options)
end