Module: RailsTestServing::ConstantManagement

Extended by:
ConstantManagement
Included in:
Cleaner, ConstantManagement
Defined in:
lib/rails_test_serving/constant_management.rb

Instance Method Summary collapse

Instance Method Details

#constantize(name) ⇒ Object



9
10
11
# File 'lib/rails_test_serving/constant_management.rb', line 9

def constantize(name)
  eval("#{name} if defined? #{name}", TOPLEVEL_BINDING)
end

#constantize!(name) ⇒ Object



13
14
15
# File 'lib/rails_test_serving/constant_management.rb', line 13

def constantize!(name)
  name.to_s.split('::').inject(Object) { |namespace, short| namespace.const_get(short) }
end

#legit?(const) ⇒ Boolean

Returns:

  • (Boolean)


5
6
7
# File 'lib/rails_test_serving/constant_management.rb', line 5

def legit?(const)
  !const.to_s.empty? && constantize(const) == const
end

#remove_constants(*names) ⇒ Object

ActiveSupport’s Module#remove_class doesn’t behave quite the way I would expect it to.



18
19
20
21
22
23
# File 'lib/rails_test_serving/constant_management.rb', line 18

def remove_constants(*names)
  names.map do |name|
    namespace, short = name.to_s =~ /^(.+)::(.+?)$/ ? [$1, $2] : ['Object', name]
    constantize!(namespace).module_eval { remove_const(short) if const_defined?(short) }
  end
end

#subclasses_of(parent, options = {}) ⇒ Object



25
26
27
28
29
# File 'lib/rails_test_serving/constant_management.rb', line 25

def subclasses_of(parent, options={})
  children = []
  ObjectSpace.each_object(Class) { |klass| children << klass if klass < parent && (!options[:legit] || legit?(klass)) }
  children
end