Class: Attribool::Validators::MethodNameValidator

Inherits:
Object
  • Object
show all
Defined in:
lib/attribool/validators/method_name_validator.rb

Overview

Ensures that if multiple attributes are being defined, and method_name is provided, that method_name is a Proc.

Instance Method Summary collapse

Constructor Details

#initialize(method_name, number_of_attributes) ⇒ MethodNameValidator

Construct the validator.

Parameters:



12
13
14
15
# File 'lib/attribool/validators/method_name_validator.rb', line 12

def initialize(method_name, number_of_attributes)
  @method_name = method_name
  @number_of_attributes = number_of_attributes
end

Instance Method Details

#errorArgumentError

The exception to raise if validations fail.

Returns:

  • (ArgumentError)

    the exception with message



27
28
29
# File 'lib/attribool/validators/method_name_validator.rb', line 27

def error
  ArgumentError.new("Must use a Proc when creating multiple methods")
end

#valid?Boolean

Is there either one attribute, or is method_name nil or a Proc?

Returns:

  • (Boolean)


19
20
21
# File 'lib/attribool/validators/method_name_validator.rb', line 19

def valid?
  @number_of_attributes == 1 || nil_or_proc?
end