Class: Shoulda::Matchers::ActiveRecord::Uniqueness::Namespace

Inherits:
Object
  • Object
show all
Defined in:
lib/shoulda/matchers/active_record/uniqueness/namespace.rb

Instance Method Summary collapse

Constructor Details

#initialize(constant) ⇒ Namespace

Returns a new instance of Namespace.



8
9
10
# File 'lib/shoulda/matchers/active_record/uniqueness/namespace.rb', line 8

def initialize(constant)
  @constant = constant
end

Instance Method Details

#clearObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/shoulda/matchers/active_record/uniqueness/namespace.rb', line 20

def clear
  test_model_classes = []

  constant.constants.each do |child_constant|
    child = constant.const_get(child_constant)

    if defined?(::ActiveRecord::Base) &&
       child.is_a?(Class) &&
       child < ::ActiveRecord::Base
      test_model_classes << child
    end

    constant.remove_const(child_constant)
  rescue NameError
    # Constant may have been removed elsewhere; ignore
  end

  if defined?(::ActiveSupport::DescendantsTracker) &&
     test_model_classes.any?
    ::ActiveSupport::DescendantsTracker.clear(test_model_classes)
  end
end

#has?(name) ⇒ Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/shoulda/matchers/active_record/uniqueness/namespace.rb', line 12

def has?(name)
  constant.const_defined?(name, false)
end

#set(name, value) ⇒ Object



16
17
18
# File 'lib/shoulda/matchers/active_record/uniqueness/namespace.rb', line 16

def set(name, value)
  constant.const_set(name, value)
end

#to_sObject



43
44
45
# File 'lib/shoulda/matchers/active_record/uniqueness/namespace.rb', line 43

def to_s
  constant.to_s
end