Class: TableWarnings::Scout

Inherits:
Object
  • Object
show all
Defined in:
lib/table_warnings/scout.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table, matcher, options = {}) ⇒ Scout

Returns a new instance of Scout.



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

def initialize(table, matcher, options = {})
  @table = table
  @matcher = matcher
  @positive_query = (options[:negative] != true)
  @conditions_query = case options[:conditions]
  when String
    true
  when Hash, Array
    !options[:conditions].empty?
  else
    false
  end
end

Instance Attribute Details

#conditionsObject (readonly)

Returns the value of attribute conditions.



5
6
7
# File 'lib/table_warnings/scout.rb', line 5

def conditions
  @conditions
end

#matcherObject (readonly)

Returns the value of attribute matcher.



4
5
6
# File 'lib/table_warnings/scout.rb', line 4

def matcher
  @matcher
end

#tableObject (readonly)

Returns the value of attribute table.



3
4
5
# File 'lib/table_warnings/scout.rb', line 3

def table
  @table
end

Instance Method Details

#cover?(column) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/table_warnings/scout.rb', line 36

def cover?(column)
  column_name = column.name
  if association_check?
    associations.any? do |a|
      a.foreign_key == column_name
    end
  else
    case matcher
    when Regexp
      !!(column_name =~ matcher)
    else
      column_name.to_s == matcher.to_s
    end
  end
end

#enable_association_check!Object



52
53
54
# File 'lib/table_warnings/scout.rb', line 52

def enable_association_check!
  @association_check_query = true
end

#exclusive?(column) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/table_warnings/scout.rb', line 21

def exclusive?(column)
  match?(column) and unambiguous?
end

#match?(column) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
28
29
30
31
32
33
34
# File 'lib/table_warnings/scout.rb', line 25

def match?(column)
  if association_check? and not column.association
    return false
  end
  if positive?
    cover? column
  else
    not cover?(column)
  end
end