Class: HanswurstValidator

Inherits:
ActiveModel::EachValidator
  • Object
show all
Defined in:
lib/hanswurst/validator.rb

Instance Method Summary collapse

Instance Method Details

#doc_is_valid_klass?(doc, klasses) ⇒ Boolean

Returns:

  • (Boolean)


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

def doc_is_valid_klass?(doc, klasses)
  !(klasses & doc.hanswurst_roles.values).empty?
end

#doc_is_valid_role?(doc, roles) ⇒ Boolean

Returns:

  • (Boolean)


2
3
4
# File 'lib/hanswurst/validator.rb', line 2

def doc_is_valid_role?(doc, roles)
  !(roles & doc.hanswurst_roles.keys).empty?
end

#is_valid_klass?(klasses, value) ⇒ Boolean

Returns:

  • (Boolean)


6
7
8
9
10
11
12
# File 'lib/hanswurst/validator.rb', line 6

def is_valid_klass?(klasses, value)
  valid = false
  klasses.each do |klass|
    valid = true if value.is_a? klass
  end
  valid
end

#validate_each(record, attribute, values) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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
67
68
69
70
71
72
73
# File 'lib/hanswurst/validator.rb', line 19

def validate_each(record, attribute, values)
  #p options
  #p record
  return if values.nil?
  values = values.values if values.class == Hash
  values = [values] unless values.class == Array
  valid = true
  max = options[:max]
  if max && values.size > max
    record.errors.add attribute, "must not have more than #{max} entries"
    return
  end
  fkey = options[:fkey] || false
  if fkey
    values = values.collect do |id|
      CouchPotato.database.load_document id 
    end
  end

  if klass = options[:class]
    klasses = [klass].flatten
    if fkey
      klasses = klasses.collect{ |k| k.to_s }
      values.each do |doc|
        valid = false unless doc && doc.hanswurst_roles && doc_is_valid_klass?(doc, klasses)
      end
      record.errors.add attribute, "must be a doc id of a class #{klasses.join(' or ')}" unless valid
    else
      values.each do |thing|
        valid = false unless is_valid_klass?(klasses, thing)  
      end
      record.errors.add attribute, "must be an instance of #{klasses.join(' or ')}" unless valid
    end
  elsif role = options[:role]
    roles = [role].flatten.collect{ |r| r.to_s }
    if fkey
      values.each do |doc|
        valid = false unless doc && doc.hanswurst_roles && doc_is_valid_role?(doc, roles)
      end
      record.errors.add attribute, "must be a doc id of a role #{roles.join(' or ')}" unless valid
    else
      klasses = roles.collect do |role_alias|
        klass = Hanswurst.role_class(role_alias.to_sym)
        record.errors.add(attribute,  "could not find role #{role_alias} with Hanswurst.role_class. Did you register it with Hanswurst.register_role() ?") if klass.nil?
        klass
      end
      values.each do |thing|
        valid = false unless is_valid_klass?(klasses, thing)  
      end
      record.errors.add attribute, "must be an instance of #{klasses.join(' or ')}" unless valid
    end
    
  end
  #roles = options[:in] || [options[:with]]    
end