Module: Strobe::Validations

Extended by:
ActiveSupport::Concern
Includes:
ActiveModel::Validations
Included in:
Resource::Base
Defined in:
lib/strobe/validations.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#read_attribute_for_validation(key) ⇒ Object


122
123
124
# File 'lib/strobe/validations.rb', line 122

def read_attribute_for_validation(key)
  self[key]
end

#valid_attribute?(key, clear = true) ⇒ Boolean

Returns:

  • (Boolean)

93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/strobe/validations.rb', line 93

def valid_attribute?(key, clear = true)
  first, rest = key.to_s.split('.', 2)

  errors.clear if clear

  if self.class.associations.key?(first.to_sym) && rest
    if inst = send(first)
      inst.valid_attribute?(rest)

      inst.errors.each do |k, v|
        errors.add("#{first}.#{k}", v)
      end
    else
      valid_attribute?(first, false)
    end
  end

  validators = self.class.validators_on(key)
  validators.each do |v|
    v.validate(self)
  end

  errors.keys.each do |k|
    errors.delete(k) unless k.to_s == key.to_s
  end

  errors.empty?
end

#valid_for_given_attributes?Boolean

Returns:

  • (Boolean)

67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/strobe/validations.rb', line 67

def valid_for_given_attributes?
  errors.clear

  ran  = []
  keys = []

  _validators.each do |attr, validators|
    validators.each do |v|
      next if !key?(attr) && v.skippable?

      keys |= [attr.to_s]

      next if ran.include?(v)

      v.validate(self)
      ran << v
    end
  end

  errors.keys.each do |k|
    errors.delete(k) unless keys.include?(k.to_s)
  end

  errors.empty?
end