Module: ContentfulModel::Validations

Included in:
Base
Defined in:
lib/contentful_model/validations/validations.rb

Overview

Defines general validation methods

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



9
10
11
12
13
# File 'lib/contentful_model/validations/validations.rb', line 9

def self.included(base)
  base.extend ClassMethods
  base.include Contentful::Validations::PresenceOf
  attr_reader :errors
end

Instance Method Details

#invalid?(save = false) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/contentful_model/validations/validations.rb', line 19

def invalid?(save = false)
  !valid?(save)
end

#valid?(save = false) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/contentful_model/validations/validations.rb', line 15

def valid?(save = false)
  validate(save)
end

#validate(save = false) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/contentful_model/validations/validations.rb', line 23

def validate(save = false)
  @errors = []
  unless respond_to?(:fields)
    @errors.push("Entity doesn't respond to the fields() method")
    return false
  end

  validation_kinds = [:validations]
  validation_kinds << :save_validations if save

  validation_kinds.each do |validation_kind|
    validations = self.class.send(validation_kind)
    next if validations.nil?

    validations.each do |validation|
      @errors += validation.validate(self)
    end
  end

  @errors.empty?
end