Module: Poncho::Validations

Included in:
Method, Resource
Defined in:
lib/poncho/validations.rb,
lib/poncho/params/validations.rb,
lib/poncho/validations/format.rb,
lib/poncho/validations/length.rb,
lib/poncho/validations/presence.rb,
lib/poncho/validations/exclusions.rb,
lib/poncho/validations/inclusions.rb

Overview

Poncho Validations

Provides a full validation framework to your objects.

A minimal implementation could be:

class Person
  include Poncho::Validations

  attr_accessor :first_name, :last_name

  validates_each :first_name, :last_name do |record, attr, value|
    record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
  end
end

Which provides you with the full standard validation stack that you know from Active Record:

person = Person.new
person.valid?                   # => true
person.invalid?                 # => false

person.first_name = 'zoolander'
person.valid?                   # => false
person.invalid?                 # => true
person.errors                   # => #<OrderedHash {:first_name=>["starts with z."]}>

Note that Poncho::Validations automatically adds an errors method to your instances initialized with a new Poncho::Errors object, so there is no need for you to do this manually.

Defined Under Namespace

Modules: ClassMethods, HelperMethods Classes: ExclusionValidator, FormatValidator, InclusionValidator, LengthValidator, ParamValidator, PresenceValidator

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



35
36
37
38
# File 'lib/poncho/validations.rb', line 35

def self.included(base)
  base.extend ClassMethods
  base.extend HelperMethods
end

Instance Method Details

#errorsObject

Returns the Errors object that holds all information about attribute error messages.



163
164
165
# File 'lib/poncho/validations.rb', line 163

def errors
  @errors ||= Errors.new(self)
end

#invalid?Boolean

Performs the opposite of valid?. Returns true if errors were added, false otherwise.

Returns:

  • (Boolean)


177
178
179
# File 'lib/poncho/validations.rb', line 177

def invalid?
  !valid?
end

#valid?(context = nil) ⇒ Boolean

Runs all the specified validations and returns true if no errors were added otherwise false. Context can optionally be supplied to define which callbacks to test against.

Returns:

  • (Boolean)


170
171
172
173
# File 'lib/poncho/validations.rb', line 170

def valid?(context = nil)
  errors.clear
  run_validations!
end