Class: Dao::Validations::Validator

Inherits:
Object
  • Object
show all
Includes:
Common
Defined in:
lib/dao/validations/validator.rb

Constant Summary collapse

NotBlank =
proc{|value| !value.to_s.strip.empty?}
Cleared =
'Cleared'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Common

#validates_all_of, #validates_any_of, #validates_as_email, #validates_as_phone, #validates_as_url, #validates_case_of, #validates_confirmation_of, #validates_inclusion_of, #validates_length_of, #validates_presence_of, #validates_type_of, #validates_value_of, #validates_word_count_of

Constructor Details

#initialize(*args, &block) ⇒ Validator

Returns a new instance of Validator.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/dao/validations/validator.rb', line 37

def initialize(*args, &block)
  @object = args.shift
  @options = Map.options_for(args)

  if args.size == 1 and @object and @object.is_a?(Hash)
    object_keys = @object.keys.map{|key| key.to_s}
    option_keys = %w( object validations errors status )

    object_is_options = !object_keys.empty? && (object_keys - option_keys).empty?

    if object_is_options
      @options = Map.for(@object)
      @object = nil
    end
  end
  
  @object ||= (@options[:object] || Map.new)
  @validations ||= (@options[:validations] || Map.new)
  @errors ||= (@options[:errors] || Errors.new)
  @status ||= (@options[:status] || Status.new)

  unless @object.respond_to?(:validator)
    @object.send(:extend, Dao::Validations)
    @object.validator = self
  end

  @errors.object = @object

  #@object.extend(InstanceExec) unless @object.respond_to?(:instance_exec)
end

Instance Attribute Details

#errorsObject

Returns the value of attribute errors.



29
30
31
# File 'lib/dao/validations/validator.rb', line 29

def errors
  @errors
end

#objectObject

Returns the value of attribute object.



26
27
28
# File 'lib/dao/validations/validator.rb', line 26

def object
  @object
end

#optionsObject

Returns the value of attribute options.



27
28
29
# File 'lib/dao/validations/validator.rb', line 27

def options
  @options
end

#statusObject

Returns the value of attribute status.



30
31
32
# File 'lib/dao/validations/validator.rb', line 30

def status
  @status
end

#validationsObject

Returns the value of attribute validations.



28
29
30
# File 'lib/dao/validations/validator.rb', line 28

def validations
  @validations
end

Class Method Details

.mixin(*args, &block) ⇒ Object



19
20
21
22
23
# File 'lib/dao/validations/validator.rb', line 19

def mixin(*args, &block)
  new(*args, &block).tap do |validator|
    validator.mixin = true
  end
end

Instance Method Details

#_run_validations(errors, list) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/dao/validations/validator.rb', line 229

def _run_validations(errors, list)
  Array(list).each do |validations|
    validations.each do |keys, chain|
      chain.each do |callback|
        next unless callback and callback.respond_to?(:to_proc)

        number_of_errors = errors.size
        value = attributes.get(keys)

        returned =
          catch(:validation) do
            args = Dao.args_for_arity([value, attributes], callback.arity)

            prefixing(keys) do
              object.instance_exec(*args, &callback)
            end
          end

        errors_added = errors.size > number_of_errors

        case returned
          when Hash
            map = Map.for(returned)
            valid = map[:valid]
            message = map[:message]
          when TrueClass, FalseClass
            valid = returned
            message = nil
          else
            valid = !errors_added
            message = nil
        end

        valid = false if errors_added

        message ||= callback.options[:message]
        message ||= (value.to_s.strip.empty? ? 'is blank' : 'is invalid')

        if not valid
          errors.add_from_source(keys, callback, message)
        else
          errors.delete_from_source(keys, callback)
        end
      end
    end
  end
end

#extract_attributes!(object = @object) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/dao/validations/validator.rb', line 68

def extract_attributes!(object = @object)
  attributes =
    catch(:attributes) do
      if object.respond_to?(:attributes)
        throw :attributes, object.attributes
      end
      if object.instance_variable_defined?('@attributes')
        throw :attributes, object.instance_variable_get('@attributes')
      end
      if object.is_a?(Map)
        throw :attributes, object
      end
      if object.respond_to?(:to_map)
        throw :attributes, Map.new(object.to_map)
      end
      if object.is_a?(Hash)
        throw :attributes, Map.new(object)
      end
      if object.respond_to?(:to_hash)
        throw :attributes, Map.new(object.to_hash)
      end
      raise ArgumentError.new("found no attributes on #{ object.inspect }(#{ object.class.name })")
    end

  @attributes =
    case attributes
      when Map
        attributes
      when Hash
        Map.new(attributes)
      else
        raise(ArgumentError.new("#{ attributes.inspect } (#{ attributes.class })"))
    end

  @attributes
end

#forcing_validity!(boolean = true) ⇒ Object



299
300
301
# File 'lib/dao/validations/validator.rb', line 299

def forcing_validity!(boolean = true)
  @forcing_validity = !!boolean
end

#forcing_validity?Boolean

Returns:

  • (Boolean)


295
296
297
# File 'lib/dao/validations/validator.rb', line 295

def forcing_validity?
  defined?(@forcing_validity) and @forcing_validity
end

#get(key) ⇒ Object



165
166
167
# File 'lib/dao/validations/validator.rb', line 165

def get(key)
  attributes.get(key_for(key))
end

#has(key) ⇒ Object Also known as: has?



173
174
175
# File 'lib/dao/validations/validator.rb', line 173

def has(key)
  attributes.has(key_for(key))
end

#key_for(*key) ⇒ Object



161
162
163
# File 'lib/dao/validations/validator.rb', line 161

def key_for(*key)
  prefix + Array(key).flatten.compact
end

#pop_prefixObject



153
154
155
# File 'lib/dao/validations/validator.rb', line 153

def pop_prefix
  stack.prefixes.pop
end

#prefixObject



157
158
159
# File 'lib/dao/validations/validator.rb', line 157

def prefix
  stack.prefixes.flatten.compact
end

#prefixing(*prefix, &block) ⇒ Object Also known as: validating



137
138
139
140
141
142
143
144
145
# File 'lib/dao/validations/validator.rb', line 137

def prefixing(*prefix, &block)
  prefix = Array(prefix).flatten.compact
  push_prefix(prefix)
  begin
    block.call(*[prefix].slice(0, block.arity))
  ensure
    pop_prefix
  end
end

#push_prefix(prefix) ⇒ Object



148
149
150
151
# File 'lib/dao/validations/validator.rb', line 148

def push_prefix(prefix)
  prefix = Array(prefix).flatten.compact
  stack.prefixes.push(prefix)
end

#resetObject



313
314
315
316
317
318
319
# File 'lib/dao/validations/validator.rb', line 313

def reset
  errors.clear!
  status.update(:ok)
  forcing_validity!(false)
  validated!(false)
  self
end

#run_validations(list = validations_list) ⇒ Object Also known as: run_validations!, validate



201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/dao/validations/validator.rb', line 201

def run_validations(list = validations_list)
  loop do
    stack.validations.push(Map.new)

    _run_validations(errors, list)

    added = stack.validations.pop
    break if added.empty?
    list = [added]
  end

  if status.ok? and !errors.empty?
    status.source = errors
    status.update(412)
  end

  if status == 412 and status.source == errors and errors.empty?
    status.update(200)
  end

  errors
ensure
  validated!(true)
end

#set(key, val) ⇒ Object



169
170
171
# File 'lib/dao/validations/validator.rb', line 169

def set(key, val)
  attributes.set(key_for(key), val)
end

#stackObject



133
134
135
# File 'lib/dao/validations/validator.rb', line 133

def stack
  @stack ||= Map[:validations, [], :prefixes, []]
end

#valid!Object



291
292
293
# File 'lib/dao/validations/validator.rb', line 291

def valid!
  @forcing_validity = true
end

#valid?(*args) ⇒ Boolean

Returns:

  • (Boolean)


303
304
305
306
307
308
309
310
311
# File 'lib/dao/validations/validator.rb', line 303

def valid?(*args)
  if forcing_validity?
    true
  else
    options = Map.options_for!(args)
    run_validations
    errors.empty? and status.ok?
  end
end

#validate!Object

Raises:



286
287
288
289
# File 'lib/dao/validations/validator.rb', line 286

def validate!
  raise Error.new("#{ object.class.name } is invalid!") unless valid?
  object
end

#validated!(boolean = true) ⇒ Object



282
283
284
# File 'lib/dao/validations/validator.rb', line 282

def validated!(boolean = true)
  @validated = !!boolean
end

#validated?Boolean

Returns:

  • (Boolean)


277
278
279
280
# File 'lib/dao/validations/validator.rb', line 277

def validated?
  @validated = false unless defined?(@validated)
  @validated
end

#validates(*args, &block) ⇒ Object Also known as: add



105
106
107
108
109
110
111
112
113
114
115
# File 'lib/dao/validations/validator.rb', line 105

def validates(*args, &block)
  block = args.pop if args.last.respond_to?(:call)
  block ||= NotBlank
  callback = Callback.new(options, &block)
  options = Map.options_for!(args)
  key = key_for(args)
  validations = stack.validations.last || self.validations
  validations[key] ||= Callback::Chain.new
  validations[key].add(callback)
  callback
end

#validates_each(*args, &block) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/dao/validations/validator.rb', line 118

def validates_each(*args, &block)
  options = Map.options_for!(args)
  key = key_for(args)

  args.push(options)

  validates(*args) do |list|
    Array(list).each_with_index do |item, index|
      args = Dao.args_for_arity([item], block.arity)
      validates(index, &block)
    end
    true
  end
end

#validations_listObject



197
198
199
# File 'lib/dao/validations/validator.rb', line 197

def validations_list
  validations_search_path.map{|object| object.validator.validations}.uniq
end

#validations_search_pathObject



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/dao/validations/validator.rb', line 179

def validations_search_path
  @validations_search_path ||= (
    if mixin?
      list = [
        object.respond_to?(:validator) ? object : nil,
        object.class.ancestors.map{|ancestor| ancestor.respond_to?(:validator) ? ancestor : nil}
      ]
      list.flatten!
      list.compact!
      list.reverse!
      list.uniq!
      list
    else
      [self]
    end
  )
end

#validatorObject



10
11
12
# File 'lib/dao/validations/validator.rb', line 10

def validator
  self
end

#validator=(validator) ⇒ Object

Raises:

  • (NotImplementedError)


14
15
16
# File 'lib/dao/validations/validator.rb', line 14

def validator=(validator)
  raise NotImplementedError
end