Class: Ward::DSL::ValidationBlock

Inherits:
Support::BasicObject
Defined in:
lib/ward/dsl/validation_block.rb

Overview

Creates one or more validators using a block.

Examples:


# Builds a validation set with two validators
#
#   * One for "author.name" and the EqualTo matcher.
#   * One for "title" with the Match matcher.

ValidationBlock.new do |object|
  object.author.name.is.equal_to('Michael Scarn')
  object.title.match(/something/)
end

See Also:

Instance Method Summary collapse

Methods inherited from Support::BasicObject

const_missing

Constructor Details

#initialize(set = nil, &block) ⇒ ValidationBlock

Creates a new ValidationBlock instance.

NOTE: Providing an existing ValidatorSet will result in a copy of that set being mutated; the original will not be changed.

Parameters:

  • set (Ward::ValidatorSet) (defaults to: nil)

    A ValidatorSet to which the built validators should be added.



29
30
31
32
# File 'lib/ward/dsl/validation_block.rb', line 29

def initialize(set = nil, &block)
  @set = if set.nil? then Ward::ValidatorSet.new else set.dup end
  run(&block) if block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *extra_args, &block) ⇒ Ward::DSL::ValidationBuilder (private)

Provides the DSL.

Will take the given message and creates a new ValidationBuilder.

Returns:



62
63
64
65
66
67
68
69
# File 'lib/ward/dsl/validation_block.rb', line 62

def method_missing(method, *extra_args, &block)
  raise 'ValidationBlock can only be used when provided ' \
    'with a block' if @builders.nil?

  builder = ValidationBuilder.new.__send__(method, *extra_args, &block)
  @builders.push(builder)
  builder
end

Instance Method Details

#to_validator_setWard::ValidatorSet

Returns the ValidatorSet created by the DSL.

Returns:



38
39
40
# File 'lib/ward/dsl/validation_block.rb', line 38

def to_validator_set
  @set
end