Module: Mutation::Filtering

Included in:
Record
Defined in:
lib/mutation_set.rb

Instance Method Summary collapse

Instance Method Details

#criteria_failed?(obj, name) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/mutation_set.rb', line 8

def criteria_failed? obj, name
  return nil if !@table.mutation_config
  name = [ name ] if !name.is_a? Array
  crit = name.reduce(@table.mutation_config) do |h,n|
    h.is_a?(Hash) ? h[n] : nil
  end
  return nil if !crit
  crit.each do |attrib,value|
    return true if !criterion_ok? obj, attrib, value
  end
  nil
end

#criterion_ok?(obj, attrib, value) ⇒ Boolean

Returns:

  • (Boolean)


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
74
75
# File 'lib/mutation_set.rb', line 21

def criterion_ok? obj, attrib, value
  case attrib
  when /^min_(.*)/
    v = obj.send($1.to_sym).to_f
    return v >= value.to_f
  when /^max_(.*)/
    return obj.send($1.to_sym).to_f <= value.to_f
  when /^exclude_(.*)/
    v = obj.send($1.to_sym)
    if value.is_a? Array
      return value.none? { |r| v.match(/#{r}/) }
    else
      return v !~ /#{value}/
    end
  when /^has_(.*)/
    v = obj.send($1.to_sym)
    if value.is_a? Array
      return value.include? v
    elsif value == true
      return v && (v.is_a?(String) ? v.size > 0 : v)
    elsif value == false || value == "nil"
      return !v
    else
      return value == v
    end
  when /^include_(.*)/
    v = obj.send($1.to_sym)
    if value.is_a? Array
      return value.any? { |r| v.match(/#{r}/) }
    else
      return v =~ /#{value}/
    end
  when /^either.*/
    v = nil
    value.each do |attrib,val|
      v = true if criterion_ok? obj, attrib, val
    end
    return v
  when /^whitelisted/
    whitelist = @table.whitelist value
    return whitelist.intersect(self)
  when /^blacklisted/
    blacklist = @table.blacklist value
    return !blacklist.intersect(self)
  else
    # send it
    case value
    when "nil", false, nil
    return !obj.send(attrib.to_sym)
    when true
    return obj.send(attrib.to_sym)
    end
  end
  true
end