Class: Predicates::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/predicates/base.rb

Overview

The base class for all predicates. Defines the interface and standard settings.

All predicates that inherit from Base get the following options:

:error_message   Feedback for the user if the validation fails. Remember that Rails will prefix the attribute name.
:validate_if     Restricts when the validation can happen. If it returns false, validation will not happen. May be a proc (with the record object as the argument) or a symbol that names a method on the record to call.
:validate_on     When to do the validation, during :update, :create, or both (default).
:or_empty        Whether to allow empty/nil values during validation (default: true)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute_name, options = {}) ⇒ Base

the initialization method provides quick support for assigning options using existing methods



67
68
69
70
71
72
73
74
# File 'lib/predicates/base.rb', line 67

def initialize(attribute_name, options = {})
  @attribute = attribute_name
  @validate_on = :both
  @or_empty = true
  options.each_pair do |k, v|
    self.send("#{k}=", v)
  end
end

Instance Attribute Details

#error_messageObject

the error string when validation fails



16
17
18
# File 'lib/predicates/base.rb', line 16

def error_message
  @error_message || :invalid
end

#full_messageObject

a message that won’t be pre-interpolated by semantic-attributes, so that it can work with ActiveRecord::Error#generate_full_message’s translation lookup



29
30
31
# File 'lib/predicates/base.rb', line 29

def full_message
  @full_message
end

#or_empty=(value) ⇒ Object (writeonly)

whether to allow empty (and nil) values during validation (default: true)



57
58
59
# File 'lib/predicates/base.rb', line 57

def or_empty=(value)
  @or_empty = value
end

#validate_ifObject

a condition to restrict when validation should occur. if it returns false, the validation will not happen. if the value is a proc, then the proc will be called and the record object passed as the argument if the value is a symbol, then a method by that name will be called on the record



44
45
46
# File 'lib/predicates/base.rb', line 44

def validate_if
  @validate_if
end

#validate_onObject

defines when to do the validation - during :update or :create (default is both, signified by absence of specification) options: :update, :create, and :both



49
50
51
# File 'lib/predicates/base.rb', line 49

def validate_on
  @validate_on
end

Instance Method Details

#allow_empty?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/predicates/base.rb', line 58

def allow_empty?
  @or_empty ? true : false
end

#errorObject



31
32
33
34
35
36
37
38
39
# File 'lib/predicates/base.rb', line 31

def error
  if full_message
    full_message
  elsif error_message.is_a?(Symbol)
    I18n.t(error_message, error_binds.merge(:scope => 'semantic-attributes.errors.messages'))
  else
    error_message
  end
end

#error_bindsObject

available interpolation variables for the error message (see I18n.translate)



23
24
25
# File 'lib/predicates/base.rb', line 23

def error_binds
  {}
end

#normalize(value) ⇒ Object

define this in the concrete class to provide a method for normalizing human inputs. this gives you the ability to be very forgiving of formatting variations in form data.



83
84
85
# File 'lib/predicates/base.rb', line 83

def normalize(value)
  value
end

#to_human(value) ⇒ Object

define this in the concrete class to provide a method for converting from a storage format to a human readable format this is good for presenting your clean, logical data in a way that people like to read.



89
90
91
# File 'lib/predicates/base.rb', line 89

def to_human(value)
  value
end

#validate(value, record) ⇒ Object

define this in the concrete class to provide a validation routine for your predicate

Raises:

  • (NotImplementedError)


77
78
79
# File 'lib/predicates/base.rb', line 77

def validate(value, record)
  raise NotImplementedError
end